// See https://aka.ms/new-console-template for more informatio
//int t, n, m;
//t = Convert.ToInt32(Console.ReadLine());
//for (int t1 = 0; t1 < t; t1++)
//{
//    var s = Console.ReadLine();
//    var endId = 0;
//    for(int i = 0; i < s.Length; i++)
//    {
//        if(s[i] == ' ')
//        {
//            endId = i;
//            break;
//        }
//    }
//    n = Convert.ToInt32(s.Substring(0, endId));
//    m = Convert.ToInt32(s.Substring(endId + 1, s.Length - endId - 1));
//    long res = 0;
//    for (int i = 1; i <= m; i++)
//    {
//        res += i;
//    }
//    for (int i = 1; i < n; i++)
//    {
//        res += i * m + m;
//    }
//    Console.WriteLine(res);
//}
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        int testCase;
        testCase = Convert.ToInt32(Console.ReadLine());
        for (int x = 0; x < testCase; x++)
        {
            var s = Console.ReadLine();
            int sum = 0;
            var endId = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == ' ')
                {
                    endId = i;
                    break;
                }
            }
            sum += Convert.ToInt32(s.Substring(0, endId));
            sum += Convert.ToInt32(s.Substring(endId + 1, s.Length - endId - 1));
            s = Console.ReadLine();
            endId = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == ' ')
                {
                    endId = i;
                    break;
                }
            }
            sum += Convert.ToInt32(s.Substring(0, endId));
            sum += Convert.ToInt32(s.Substring(endId + 1, s.Length - endId - 1));
            int res = 0;
            if (sum == 4)
            {
                res = 2;
            }
            else if (sum > 0)
            {
                res = 1;
            }
            Console.WriteLine(res);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

namespace tes
{
	class contest
	{

		static void Main(string[] args)
		{

   string s = Console.ReadLine();
            string r = "";
            string c = "";
            int len = s.Length;
            for(int i=0; i< len; i++)
            {
                if (i < len - 1) r += s[i];
                else c += s[i];
            }

            var col = new string[]{"f","e","d","a","b","c" };
            long row = long.Parse(r);

            long time = (Array.IndexOf(col,c))+1;

            long a = row/3;
            long b = row % 3;

            long turn = row / 4;
            if (row % 4 > 0) turn++;

            time += (turn+turn-2) * 7;
            time += (turn -1) * 2;

            if (row % 2 == 0)
            {
                time += 7;
            }

            Console.WriteLine(time);

        }

	}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _725B
{
    class Program
    {
        static void Main(string[] args)
        {
            string S = Console.ReadLine();
            int L = S.Length; sbyte A2 = 0;
            char C = char.Parse(S.Substring(L-1));
            string SN = (S.Substring(0, L - 1));
            long N = long.Parse(SN);

            switch (C)
            {
                case 'f':
                    A2 = 1;
                    break;
                case 'e':
                    A2 = 2;
                    break;
                case 'd':
                    A2 = 3;
                    break;
                case 'a':
                    A2 = 4;
                    break;
                case 'b':
                    A2 = 5;
                    break;
                case 'c':
                    A2 = 6;
                    break;

            }

            long A1 = 16 * ((N - 1) / 4);
            sbyte A3 = 0;
            if ((N % 2) == 0) A3 = 7;
            long T = A1 + A2 + A3;

            Console.WriteLine(T);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace CF
{
    public static class FromConsole
    {
        public static T To<T>(this string input) where T : IConvertible
        {
            return (T)Convert.ChangeType(input, typeof(T));
        }

        public static T[] ToArrayOf<T>(this string input, char? delimiter = null) where T : IConvertible
        {
            IEnumerable<string> sequence = delimiter == null ? input.Select(x => x.ToString()) : input.Split(delimiter.Value);

            return sequence.Select(x => x.ToString().To<T>()).ToArray();
        }

        public static T[] ToArrayOf<T>(int count) where T : IConvertible
        {
            T[] output = new T[count];

            for (int i = 0; i < count; i++)
            {
                output[i] = Console.ReadLine().To<T>();
            }

            return output;
        }

        public static T[] ToArrayOf<T>(T value, int count)
        {
            return Enumerable.Repeat(value, count).ToArray();
        }

        public static T[][] ToMatrixOf<T>(int n, char? delimiter = null) where T : IConvertible
        {
            T[][] output = new T[n][];

            for (int i = 0; i < n; i++)
            {
                output[i] = Console.ReadLine().ToArrayOf<T>(delimiter);
            }

            return output;
        }

        public static T[][] ToMatrixOf<T>(T value, int n, int m)
        {
            T[][] output = new T[n][];

            for (int i = 0; i < n; i++)
            {
                output[i] = ToArrayOf(value, m);
            }

            return output;
        }

        public static Tuple<int, int> IndexOf<T>(this T[][] input, params T[] targets)
        {
            for (int i = 0; i < input.Length; i++)
            {
                for (int j = 0; j < input[0].Length; j++)
                {
                    if (targets.Any(x => x.Equals(input[i][j])))
                    {
                        return new Tuple<int, int>(i, j);
                    }
                }
            }

            return new Tuple<int, int>(-1, -1);
        }

        public static void AddToList<T, U>(this Dictionary<T, List<U>> input, T k, U v)
        {
            if (input.ContainsKey(k))
            {
                input[k].Add(v);
            }
            else
            {
                input.Add(k, new List<U>() { v });
            }
        }

        public static void AddAndIncrement<T>(this Dictionary<T, int> input, T k)
        {
            if (input.ContainsKey(k))
            {
                input[k]++;
            }
            else
            {
                input.Add(k, 1);
            }
        }

        public static string ToBinary(this long input)
        {
            return Convert.ToString(input, 2);
        }

        public static long ToDecimal(this string input)
        {
            return Convert.ToInt32(input, 2);
        }

        public static long Binomial(long n, long k)
        {
            long r = 1;
            long d;
            if (k > n) return 0;
            for (d = 1; d <= k; d++)
            {
                r *= n--;
                r /= d;
            }
            return r;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int[] bounds = Console.ReadLine().ToArrayOf<int>(' ');
            int n = bounds[0];
            int x = Math.Min(bounds[1], bounds[2]);
            int y = Math.Max(bounds[1], bounds[2]);

            Node head = new Node(1);
            Node current = head;
            for (int i = 2; i <= n; i++)
            {
                Node next = new Node(i);
                next.Previous = current;
                current.Next = next;
                current = next;
            }

            int round = 1;
            bool stillPossible = true;
            int finalRound = (int)Math.Log(n, 2);
            while (round <= finalRound)
            {
                Node one = head;
                Node two = head.Next;
                while (one != null)
                {
                    int i = one.Value;
                    int j = two.Value;

                    if (i == x && j == y)
                    {
                        stillPossible = false;
                        break;
                    }
                    else
                    {
                        if (i == x || i == y)
                        {
                            one.Next = two.Next;

                            if (two.Next == null)
                            {
                                break;
                            }

                            two.Next.Previous = one;
                            two = one.Next;

                            one = one.Next;
                            two = two.Next;
                        }
                        else
                        {
                            if (one.Previous == null)
                            {
                                head = two;
                                two.Previous = null;
                                one = head;
                                two = one.Next;

                                one = one.Next;
                                two = two.Next;
                            }
                            else
                            {
                                one.Previous.Next = two;
                                two.Previous = one.Previous;
                                one = two;
                                two = one.Next;

                                if (two == null)
                                {
                                    break;
                                }

                                one = one.Next;
                                two = two.Next;
                            }
                        }
                    }
                }

                if (!stillPossible)
                {
                    break;
                }

                round++;
            }

            Console.WriteLine(round == finalRound ? "Final!" : round.ToString());
            //Console.ReadKey();
        }
    }

    class Node
    {
        public int Value { get; set; }
        public Node Previous { get; set; }
        public Node Next { get; set; }

        public Node(int value)
        {
            Value = value;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Codeforces
{
      class Program
      {
            static void Main(string[] args)
            {
                  string[] s = Console.ReadLine().Split(' ');
                  int n = int.Parse(s[0]);
                  int a = int.Parse(s[1]);
                  int b = int.Parse(s[2]);

                  if (a > b)
                  {
                        int x = a;
                        a = b;
                        b = x;
                  }

                  if (a <= n / 2 && b > n / 2)
                        Console.WriteLine("Final!");
                  else
                  {
                        a--;
                        b--;
                        int res;
                        for (res = 0; a != b; res++)
                        {
                              a /= 2;
                              b /= 2;
                        }
                        Console.WriteLine(res);
                  }
            }
      }
}


using System;

namespace CodeForces
{
    internal class ThreeDoors
    {
        public static void Main(string[] args)
        {
            int testcases = int.Parse(Console.ReadLine());

            for (int i = 0; i < testcases; i++)
            {
                int key = int.Parse(Console.ReadLine());

                int[] doors = new int[3];
                string[] split = Console.ReadLine().Split();

                for (int j = 0; j < 3; j++)
                {
                    doors[j] = int.Parse(split[j]);
                }

                TestPossible(key, doors);
            }
        }

        static void TestPossible(int key, int[] doors)
        {
            for (int i = 0; i < 2; i++)
            {
                key = doors[key - 1];
                if (key == 0)
                {
                    Console.WriteLine("no");
                    return;
                }
            }
            Console.WriteLine("yes");
        }
    }
}


using System;
using System.Linq;
using System.Text;
using System.Numerics;
using static System.Console;
using System.Collections.Generic;

using static CompLib.Algorithm;
using static CompLib.EMath;

namespace ProblemSolving.cs
{
    class MainClass
    {
        const int Mod = (int) (1e9 + 7);
        const int Def = (int) 1e9;

        static long[] OneLongLine => ReadLine().Trim().Split(' ').Select(long.Parse).ToArray();
        static int[] OneIntLine => ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
        static int[] OneCharLine => ReadLine().Trim().ToCharArray().Select(v => v - '0').ToArray();
        static long OneLong => long.Parse(ReadLine().Trim());
        static int OneInt => int.Parse(ReadLine().Trim());
        static string OneString => ReadLine().Trim();

        static void Solve()
        {
            int key = OneInt;
            var list = OneIntLine.ToList();
            int idx = list.IndexOf(0) + 1;

            if (list[key - 1] == 0 || list[list[key - 1] - 1] == 0)
            {
                WriteLine("NO");
            }
            else WriteLine("YES");

        }

        static bool Check(int[] arr)
        {
            return true;
        }

        static void Main()
        {
            int cases = OneInt;
            while (--cases >= 0)
            {
                Solve();
            }

            // Solve();
        }
    }
}

namespace CompLib
{
     public static class Algorithm
     {
         public static long LowerBound(long[] arr, long low, long high, long val)
         {
             if (low > high)
             {
                 return low;
             }

             long mid = low + (high - low) / 2;

             if (val <= arr[mid])
             {
                 return LowerBound(arr, low, mid - 1, val);
             }

             return LowerBound(arr, mid + 1, high, val);
         }

         public static long UpperBound(long[] arr, long low, long high, long val)
         {
             if (low > high || low == arr.Length)
             {
                 return low;
             }

             long mid = low + (high - low) / 2;

             if (val >= arr[mid])
             {
                 return UpperBound(arr, mid + 1, high, val);
             }

             return UpperBound(arr, low, mid - 1, val);
         }
     }

    public static class EMath
    {
        public static int Mod(int a, int m)
        {
            return (a % m + m) % m;
        }

        public static long Gcd(long a, long b)
        {
            long r;
            while (b != 0)
            {
                r = a % b;
                a = b;
                b = r;
            }

            return a;
        }

        public static long Lcm(long a, long b)
        {
            if (a > b)
            {
                return (a / Gcd(a, b)) * b;
            }

            return (b / Gcd(a, b)) * a;
        }
    }
}


    using System;
    using System.Linq;
    using System.Collections.Generic;

    namespace Codeforces
    {
        class TaskA
        {
            static void Main()
            {
                var input = Console.ReadLine().Trim().Split(' ');
                var a = Int32.Parse(input[0]);
                var b = Int32.Parse(input[1]);

                var max = Math.Max(a, b);
                var min = Math.Min(a, b);

                long lenMax = 0;
                long nok = (long)a*b;
                for (long i = 1; i <= min; i++)
                {
                    var q = (max * i) % min;
                    if (q == 0)
                    {
                        lenMax += min;
                        nok = i * max;
                    }
                    else
                        lenMax += q;
                }
                if(lenMax*2 == nok)
                    Console.WriteLine("Equal");
                else if (lenMax * 2 < nok)
                {
                    if (max == b)
                        Console.WriteLine("Dasha");
                    else
                        Console.WriteLine("Masha");
                }
                else
                {
                    if (max == a)
                        Console.WriteLine("Dasha");
                    else
                        Console.WriteLine("Masha");
                }

            }
        }
    }


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace CF
{
	internal class Program
	{
		private static void Main(string[] args)
		{
			var sr = new InputReader(Console.In);
			//var sr = new InputReader(new StreamReader("input.txt"));
			var task = new Task();
			using (var sw = Console.Out)
			{
				//using (var sw = new StreamWriter("output.txt")) {
				task.Solve(sr, sw);
				//Console.ReadKey();
			}
		}
	}

	internal class Task
	{
		public void Solve(InputReader sr, TextWriter sw)
		{
			var input = sr.ReadArrayOfInt64();
			var a = input[0];
			var b = input[1];
			var gcd = Gcd((int)a, (int)b);
			var lcm = (a * b) / gcd;
			var dCount = lcm / a;
			var mCount = lcm / b;
			if (a > b)
				dCount++;
			else {
				mCount++;
			}
			if (dCount == mCount)
			{
				sw.WriteLine("Equal");
				return;
			}
			if (dCount > mCount)
			{
				sw.WriteLine("Dasha");
			}
			else {
				sw.WriteLine("Masha");
			}
		}

		private int Gcd(int a, int b)
		{
			if (b == 0)
				return a;

			return Gcd(b, a % b);
		}
	}
}

internal class InputReader : IDisposable
{
	private bool isDispose;
	private readonly TextReader sr;

	public InputReader(TextReader stream)
	{
		sr = stream;
	}

	public void Dispose()
	{
		Dispose(true);
		GC.SuppressFinalize(this);
	}

	public string NextString()
	{
		var result = sr.ReadLine();
		return result;
	}

	public int NextInt32()
	{
		return Int32.Parse(NextString());
	}

	public long NextInt64()
	{
		return Int64.Parse(NextString());
	}

	public string[] NextSplitStrings()
	{
		return NextString()
										.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
	}

	public T[] ReadArray<T>(Func<string, CultureInfo, T> func)
	{
		return NextSplitStrings()
										.Select(s => func(s, CultureInfo.InvariantCulture))
										.ToArray();
	}

	public int[] ReadArrayOfInt32()
	{
		return ReadArray(Int32.Parse);
	}

	public long[] ReadArrayOfInt64()
	{
		return ReadArray(Int64.Parse);
	}

	public T[] ReadArrayFromString<T>(Func<string, CultureInfo, T> func, string str)
	{
		return
										str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
																		.Select(s => func(s, CultureInfo.InvariantCulture))
																		.ToArray();
	}

	protected void Dispose(bool dispose)
	{
		if (!isDispose)
		{
			if (dispose)
				sr.Close();
			isDispose = true;
		}
	}

	~InputReader()
	{
		Dispose(false);
	}
}


using System;
using System.IO;
using System.Text;

namespace Cows_and_Primitive_Roots
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            int p = Next();

            int count = 0;

            for (int i = 1; i < p; i++)
            {
                int k = 1;
                for (int j = 1; j < p; j++)
                {
                    k = (k*i)%p;
                    if (k == 1)
                    {
                        if (j == p - 1)
                        {
                            count++;
                        }
                        break;
                    }
                }
            }

            writer.WriteLine(count);
            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Codeforces174_A
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int p = int.Parse(Console.ReadLine());
            Console.WriteLine(Eiler(Eiler(p)));
        }

        private static int Eiler(int n)
        {
            if (n == 1) return 1;
            int upperBound = (int) Math.Sqrt(n) + 1;
            Dictionary<int, int> canonical_rep = new Dictionary<int, int>();
            for (int i = 2; i <= n; i++)
                while (n%i == 0 && n != 1)
                {
                    if (canonical_rep.ContainsKey(i)) canonical_rep[i]++;
                    else
                        canonical_rep.Add(i, 1);
                    n /= i;
                }
            int result = 1;
            foreach (var divisor in canonical_rep)
            {
                result *= (int) Math.Pow(divisor.Key, divisor.Value) - (int) Math.Pow(divisor.Key, divisor.Value - 1);
            }
            return result;
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution{
	 static void Main(string[] args) {
             var tmp = Console.ReadLine().Split(' ');
            long d = long.Parse(tmp[0]);
            long k = long.Parse(tmp[1]);
            long a = long.Parse(tmp[2]);
            long b = long.Parse(tmp[3]);
            long t = long.Parse(tmp[4]);

            if (d <= k) {
                Console.WriteLine(a * d);
                return;
            }

            long ans = k * a + (d - k) * b;
            long low = 0, high = 1000000000000;
            while (high - low > 1) {
                long mid = (high + low) / 2;

                long center = Calc(d, k, a, b, t, mid);
                long center1 = Calc(d, k, a, b, t, mid + 1);
                long center_1 = Calc(d, k, a, b, t, mid - 1);

                if (center != -1) ans = Math.Min(ans, center);
                if (center1 != -1) ans = Math.Min(ans, center1);
                if (center_1 != -1) ans = Math.Min(ans, center_1);

                if (center1 == -1) {
                    high = mid;
                } else {
                    if (center1 > center) high = mid; else low = mid;
                }
            }
            Console.WriteLine(ans);
	 }
     private static long Calc(long d, long k, long a, long b, long t, long mid) {
            long withCar = k * mid;

            long remaining = d - withCar;
            if (remaining < 0) {
                return -1;
            }

            long time = k * mid * a + Math.Max(0, (mid - 1)) * t;
             if (remaining <= k) return Math.Min(t+time + remaining * a, time + remaining * b);
            return time + remaining * b;
        }
}


using System;
using System.IO;
using System.Text;

namespace Road_to_Post_Office
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            long d = Next(), k = Next(), a = Next(), b = Next(), t = Next();

            long cycle = t + a*k;

            long ans;

            if (d <= k)
            {
                ans = a*d;
            }
            else
            {
                d -= k;
                ans = k*a;
                if (cycle < b*k)
                {
                    ans += cycle*(d/k);
                    d %= k;

                    ans += Math.Min(d*b, t + a*d);
                }
                else
                {
                    ans += b*d;
                }
            }

            writer.WriteLine(ans);
            writer.Flush();
        }

        private static long Next()
        {
            int c;
            long res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.Linq;
using System.Security.Cryptography;
using CompLib.Util;

public class Program
{
    int N, K;
    string S;
    string T;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        K = sc.NextInt();
        S = sc.Next();
        T = sc.Next();

        /*
         * sと2文字t
         *
         * sの任意の文字をえらんで別の文字に置き換える
         *
         * k回まで操作
         * tの出現回数最大
         */

        if (T[0] == T[1])
        {
            int cnt = 0;
            foreach (var c in S)
            {
                if (c == T[0]) cnt++;
            }

            int max = Math.Min(cnt + K, N);

            Console.WriteLine((long)max * (max - 1) / 2);
        }
        else
        {

            // iまで見る
            // T[0]の個数
            // 変更回数
            int[,,] dp = new int[N + 1, N + 1, K + 1];
            for (int i = 0; i <= N; i++)
            {
                for (int j = 0; j <= N; j++)
                {
                    for (int k = 0; k <= K; k++)
                    {
                        dp[i, j, k] = int.MinValue;
                    }
                }
            }

            dp[0, 0, 0] = 0;
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j <= N; j++)
                {
                    for (int k = 0; k <= K; k++)
                    {
                        if (dp[i, j, k] == int.MinValue) continue;
                        if (S[i] == T[0])
                        {
                            dp[i + 1, j + 1, k] = Math.Max(dp[i + 1, j + 1, k], dp[i, j, k]);
                        }
                        else if (k + 1 <= K)
                        {
                            dp[i + 1, j + 1, k + 1] = Math.Max(dp[i + 1, j + 1, k + 1], dp[i, j, k]);
                        }

                        if (S[i] == T[1])
                        {
                            dp[i + 1, j, k] = Math.Max(dp[i + 1, j, k], dp[i, j, k] + j);
                        }
                        else if (k + 1 <= K)
                        {
                            dp[i + 1, j, k + 1] = Math.Max(dp[i + 1, j, k + 1], dp[i, j, k] + j);
                        }

                        dp[i + 1, j, k] = Math.Max(dp[i + 1, j, k], dp[i, j, k]);
                    }
                }
            }

            int ans = int.MinValue;
            for (int j = 0; j <= N; j++)
            {
                for (int k = 0; k <= K; k++)
                {
                    ans = Math.Max(ans, dp[N, j, k]);
                }
            }

            Console.WriteLine(ans);
        }
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static System.Math;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Runtime.CompilerServices;
using Library;

namespace Program
{
    public static class ProblemF
    {
        static bool SAIKI = false;
        static public int numberOfRandomCases = 0;
        static public void MakeTestCase(List<string> _input, List<string> _output, ref Func<string[], bool> _outputChecker)
        {
        }
        static public void Solve()
        {
            var n = NN;
            var k = NN;
            var s = NS;
            var t = NS;
            var dp = new long[n + 1, n + 2, k + 2];
            for (var i = 0; i <= n; i++)
            {
                for (var j = 0; j <= n; j++)
                {
                    for (var x = 0; x <= k; x++)
                    {
                        dp[i, j, x] = long.MinValue;
                    }
                }
            }
            dp[0, 0, 0] = 0;
            var ans = 0L;
            for (var i = 0; i < n; i++)
            {
                for (var j = 0; j <= n; j++)
                {
                    for (var x = 0; x <= k; x++)
                    {
                        if (s[i] == t[0])
                        {
                            dp[i + 1, j + 1, x] = Max(dp[i + 1, j + 1, x], dp[i, j, x]);
                        }
                        if (s[i] == t[1])
                        {
                            dp[i + 1, j, x] = Max(dp[i + 1, j, x], dp[i, j, x] + j);
                        }
                        if (s[i] == t[0] && s[i] == t[1])
                        {
                            dp[i + 1, j + 1, x] = Max(dp[i + 1, j + 1, x], dp[i, j, x] + j);
                        }
                        if (t[0] == t[1])
                        {
                            dp[i + 1, j + 1, x + 1] = Max(dp[i + 1, j + 1, x + 1], dp[i, j, x] + j);
                        }
                        dp[i + 1, j, x + 1] = Max(dp[i + 1, j, x + 1], dp[i, j, x] + j);
                        dp[i + 1, j + 1, x + 1] = Max(dp[i + 1, j + 1, x + 1], dp[i, j, x]);
                        dp[i + 1, j, x] = Max(dp[i + 1, j, x], dp[i, j, x]);
                    }
                }
            }
            for (var j = 0; j <= n + 1; j++)
            {
                for (var x = 0; x <= k; x++)
                {
                    ans = Max(ans, dp[n, j, x]);
                }
            }
            Console.WriteLine(ans);
        }
        class Printer : StreamWriter
        {
            public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
            public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { base.AutoFlush = false; }
            public Printer(Stream stream, Encoding encoding) : base(stream, encoding) { base.AutoFlush = false; }
        }
        static LIB_FastIO fastio = new LIB_FastIODebug();
        static public void Main(string[] args) { if (args.Length == 0) { fastio = new LIB_FastIO(); Console.SetOut(new Printer(Console.OpenStandardOutput())); } if (SAIKI) { var t = new Thread(Solve, 134217728); t.Start(); t.Join(); } else Solve(); Console.Out.Flush(); }
        static long NN => fastio.Long();
        static double ND => fastio.Double();
        static string NS => fastio.Scan();
        static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray();
        static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray();
        static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray();
        static long Count<T>(this IEnumerable<T> x, Func<T, bool> pred) => Enumerable.Count(x, pred);
        static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n);
        static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c);
        static IOrderedEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => Enumerable.OrderBy(x, _ => xorshift);
        static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> x) => Enumerable.OrderBy(x.OrderByRand(), e => e);
        static IOrderedEnumerable<T1> OrderBy<T1, T2>(this IEnumerable<T1> x, Func<T1, T2> selector) => Enumerable.OrderBy(x.OrderByRand(), selector);
        static IOrderedEnumerable<T> OrderByDescending<T>(this IEnumerable<T> x) => Enumerable.OrderByDescending(x.OrderByRand(), e => e);
        static IOrderedEnumerable<T1> OrderByDescending<T1, T2>(this IEnumerable<T1> x, Func<T1, T2> selector) => Enumerable.OrderByDescending(x.OrderByRand(), selector);
        static IOrderedEnumerable<string> OrderBy(this IEnumerable<string> x) => x.OrderByRand().OrderBy(e => e, StringComparer.OrdinalIgnoreCase);
        static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> x, Func<T, string> selector) => x.OrderByRand().OrderBy(selector, StringComparer.OrdinalIgnoreCase);
        static IOrderedEnumerable<string> OrderByDescending(this IEnumerable<string> x) => x.OrderByRand().OrderByDescending(e => e, StringComparer.OrdinalIgnoreCase);
        static IOrderedEnumerable<T> OrderByDescending<T>(this IEnumerable<T> x, Func<T, string> selector) => x.OrderByRand().OrderByDescending(selector, StringComparer.OrdinalIgnoreCase);
        static string Join<T>(this IEnumerable<T> x, string separator = "") => string.Join(separator, x);
        static uint xorshift { get { _xsi.MoveNext(); return _xsi.Current; } }
        static IEnumerator<uint> _xsi = _xsc();
        static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff); while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } }
    }
}
namespace Library {
    class LIB_FastIO
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_FastIO() { str = Console.OpenStandardInput(); }
        readonly Stream str;
        readonly byte[] buf = new byte[1024];
        int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream
        {
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            get { return isEof; }
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        byte read()
        {
            if (isEof) throw new EndOfStreamException();
            if (ptr >= len)
            {
                ptr = 0;
                if ((len = str.Read(buf, 0, 1024)) <= 0)
                {
                    isEof = true;
                    return 0;
                }
            }
            return buf[ptr++];
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        char Char()
        {
            byte b = 0;
            do b = read();
            while (b < 33 || 126 < b);
            return (char)b;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        virtual public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        virtual public long Long()
        {
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != '-' && (b < '0' || '9' < b));
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        virtual public double Double() { return double.Parse(Scan(), CultureInfo.InvariantCulture); }
    }
    class LIB_FastIODebug : LIB_FastIO
    {
        Queue<string> param = new Queue<string>();
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        string NextString() { if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item); return param.Dequeue(); }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_FastIODebug() { }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override string Scan() => NextString();
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override long Long() => long.Parse(NextString());
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override double Double() => double.Parse(NextString());
    }
}


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Schema;

namespace Codeforces
{
    internal static class Program
    {
        private const string Problem = "c";

        private static void Solve()
        {
            int n, k, d;
            Read(out n, out k, out d);
            var mod = 1000000007;
            var dp = new int[n + 1, 2];
            dp[0, 0] = 1;

            for (int i = 1; i <= n; i++)
            {
                var maxJ = Math.Min(k, i);
                for (int j = 1; j <= maxJ; j++)
                {
                    dp[i, 0] += dp[i - j, 0]%mod;
                    dp[i, 0] %= mod;
                    if (j >= d)
                    {
                        dp[i, 1] += dp[i - j, 0]%mod;
                        dp[i, 1] %= mod;
                    }
                    else
                    {
                        dp[i, 1] += dp[i - j, 1] % mod;
                        dp[i, 1] %= mod;
                    }
                }
            }
            WriteLine(dp[n, 1]);
        }

        private static void Main()
        {
            var isDebbuger = Debugger.IsAttached;
            if (isDebbuger)
            {
                var projectPath = Directory.GetParent(Environment.CurrentDirectory).Parent;
                var testsPath = Path.Combine(projectPath != null? projectPath.FullName: "", "tests");
                var files = Directory.GetFiles(testsPath, string.Format("{0}*.in", Problem));

                foreach (var file in files)
                {
                    Console.SetIn(new StreamReader(file));
                    Console.WriteLine(Path.GetFileNameWithoutExtension(file));
                    Solve();

                    Console.WriteLine();

                    Console.In.Close();
                    Console.SetIn(new StreamReader(Console.OpenStandardInput()));
                }

                Console.ReadLine();
            }
            else
                Solve();
        }

        private static int[] SortIndexs<T>(this IEnumerable<T> source) where T : IComparable
        {
            var array = source == null ? new List<T>() : source.ToList();
            var n = array.Count;
            var res = Enumerable.Range(0, n).ToArray();
            Array.Sort(res, (a, b) =>
            {
                var compare = array[a].CompareTo(array[b]);
                return compare == 0 ? a.CompareTo(b) : compare;
            });
            return res;
        }

        #region Reader

        private static string Read()
        {
            return Console.ReadLine();
        }

        private static void Read<T>(out T a1, out T a2)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3, out T a4)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
            a4 = (T)Convert.ChangeType(input[3], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3, out T a4, out T a5)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
            a4 = (T)Convert.ChangeType(input[3], typeof(T));
            a5 = (T)Convert.ChangeType(input[4], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3, out T a4, out T a5, out T a6)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
            a4 = (T)Convert.ChangeType(input[3], typeof(T));
            a5 = (T)Convert.ChangeType(input[4], typeof(T));
            a6 = (T)Convert.ChangeType(input[5], typeof(T));
        }

        private static int ReadInt()
        {
            return Int32.Parse(Read());
        }

        private static long ReadLong()
        {
            return long.Parse(Read());
        }

        private static double ReadDouble()
        {
            return double.Parse(Read(), CultureInfo.InvariantCulture);
        }

        private static T Read<T>()
        {
            return (T)Convert.ChangeType(Read(), typeof(T));
        }

        private static string[] ReadArray()
        {
            var readLine = Console.ReadLine();
            if (readLine != null)
                return readLine.Split(' ');

            throw new ArgumentException();
        }

        private static List<int> ReadIntArray()
        {
            return ReadArray().Select(Int32.Parse).ToList();
        }

        private static List<long> ReadLongArray()
        {
            return ReadArray().Select(long.Parse).ToList();
        }

        private static List<double> ReadDoubleArray()
        {
            return ReadArray().Select(d => double.Parse(d, CultureInfo.InvariantCulture)).ToList();
        }

        private static List<T> ReadArray<T>()
        {
            return ReadArray().Select(x => (T)Convert.ChangeType(x, typeof(T))).ToList();
        }

        private static void WriteLine(double value)
        {
            Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void WriteLine(double value, string stringFormat)
        {
            Console.WriteLine(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void WriteLine<T>(T value)
        {
            Console.WriteLine(value);
        }

        private static void Write(double value)
        {
            Console.Write(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void Write(double value, string stringFormat)
        {
            Console.Write(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void Write<T>(T value)
        {
            Console.Write(value);
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication3
{
    internal class Program
    {
        private static void Main()
        {
            var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var n = input[0];
            var k = input[1];
            var d = input[2];

            Console.WriteLine(Solve(n, k, d, false, new Cache()));
        }

        private static int Solve(int n, int k, int d, bool hasD, Cache cache)
        {
            var key = new Tuple<int, bool>(n, hasD);
            if (cache.ContainsKey(key))
                return cache[key];
            if (n == 0)
                return hasD ? 1 : 0;

            var res = 0;
            for (var i = 1; i <= Math.Min(n, k); i++)
                res = (res + Solve(n - i, k, d, hasD | i >= d, cache))%1000000007;

            return cache[key] = res;
        }

        class Cache : Dictionary<Tuple<int, bool>, int>
        {

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Codeforces_247_2
{
    class Program
    {
        static long[,] cache;
        static long Count(int n, int k)
        {
            long ret = cache[n, k];
            if (ret < 0)
            {
                ret = 0;
                for (int i = 1; i <= k; ++i) {
                    if (n > i) {
                        ret = (ret + Count(n - i, k)) % 1000000007;
                    }
                    else if (n == i) {
                        ret = ret + 1;
                    }
                }
                cache[n, k] = ret;
            }
            return ret;
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            int n = int.Parse(s[0]);
            int k = int.Parse(s[1]);
            int d = int.Parse(s[2]);
            cache = new long[n + 1, k + 1];
            for (int i = 0; i <= n; ++i) for (int j = 0; j <= k; ++j) cache[i, j] = -1;
            Console.WriteLine((1000000007 + Count(n, k) - Count(n, d - 1)) % 1000000007);
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace k_Tree
{
    internal class Program
    {
        private const long TTT = 1000000007;

        private static readonly StreamReader reader =
            new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);

        private static readonly StreamWriter writer =
            new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string[] ss = reader.ReadLine().Split(' ');
            int n = int.Parse(ss[0]);
            int k = int.Parse(ss[1]);
            int d = int.Parse(ss[2]);

            var kgood = new long[n + 1];
            var kbad = new long[n + 1];
            long sum = 0;
            for (int i = 1; i <= k && i <= n; i++)
            {
                if (i >= d)
                {
                    kgood[i] = 1;
                }
                else
                    kbad[i] = 1;
            }
            sum = (sum + kgood[n])%TTT;

            for (int j = 1; j <= n; j++)
            {
                var kgood1 = new long[n + 1];
                var kbad1 = new long[n + 1];

                for (int i = 1; i <= n; i++)
                {
                    for (int l = 1; l <= k; l++)
                    {
                        if (i + l > n)
                            break;

                        kgood1[l + i] += kgood[i];
                        if (l >= d)
                        {
                            kgood1[l + i] += kbad[i];
                        }
                        else
                        {
                            kbad1[l + i] += kbad[i];
                        }
                    }
                }
                sum = (sum + kgood1[n])%TTT;
                for (int i = 1; i <= n; i++)
                {
                    kbad[i] = kbad1[i]%TTT;
                    kgood[i] = kgood1[i]%TTT;
                }
            }

            writer.WriteLine("{0}", sum);
            writer.Flush();
        }
    }
}


using System;

class C431 {
    const int M = 1000000007;

    public static void Main() {
        var line = Console.ReadLine().Split();
        var n = int.Parse(line[0]);
        var k = int.Parse(line[1]);
        var d = int.Parse(line[2]);
        var a = new long[n + 1];
        var b = new long[n + 1];
        a[0] = 1L;
        b[0] = 0L;
        for (int i = 1; i <= n; ++i) {
            var a1 = i - k >= 1 ? a[i - k - 1] : 0L;
            var a2 = i >= d ? a[i - d] : 0L;
            var b2 = i >= d ? b[i - d] : 0L;
            a[i] = (a[i - 1] + (a[i - 1] + M - a1)) % M;
            b[i] = (b[i - 1] + (b[i - 1] + M - b2) + (a2 + M - a1)) % M;
        }
        Console.WriteLine((b[n] + M - b[n - 1]) % M);
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication34
{

    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            string[] arr = Console.ReadLine().Split();
            int a = int.Parse(arr[0]);
            int b = int.Parse(arr[1]);
            a = a * 100;
            b = b * 10;
            while (true)
            {

                if (i % 2 != 0)
                {
                    if (a >= 200)
                    {
                        if (b >= 20)
                        {
                            a -= 200;
                            b -= 20;
                        }
                        else
                        {
                            Console.WriteLine("Hanako");return;
                        }
                    }
                    else if (a == 100)
                    {

                        if (b >= 120)
                        {
                            a -= 100;
                            b -= 120;
                        }
                        else
                        {
                            Console.WriteLine("Hanako"); return;
                        }
                    }
                    else if (a < 100)
                    {
                        if (b >= 220)
                            b -= 220;
                        else
                        {
                            Console.WriteLine("Hanako"); return;
                        }
                    }
                }

                if (i % 2 == 0)
                {
                    if (b >= 220)
                    {
                        b -= 220;
                    }
                    else if (b < 220 && b >= 120)
                    {
                        if (a >= 100)
                        {
                            a -= 100;
                            b -= 120;
                        }
                        else
                        {
                            Console.WriteLine("Ciel"); return;
                        }
                    }
                    else if (b >= 20)
                    {
                        if (a >= 200)
                        {
                            a -= 200;
                            b -= 20;
                        }
                        else
                        {
                            Console.WriteLine("Ciel"); return;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Ciel"); return;
                    }
                }
                i++;
            }

        }

    }
 }


using System;
using System.Collections.Generic;
using System.Text;

namespace A.Theatre_Square
{
    class Program
    {

        static void Main(string[] args)
        {
            int[,] step = { { 2, 2 }, { 1, 12 }, { 0, 22 } };
            string[] data = Console.ReadLine().Split(' ');
            int n = Int32.Parse(data[0]);
            int m = Int32.Parse(data[1]);
            bool isEnd = false;
            bool turn = false;
            while (!isEnd)
            {
                if (!turn)
                {
                    for (int i = 0; i < 3; i++)
                    {

                        if (n >= step[i, 0] && m >= step[i, 1])
                        {
                            n -= step[i, 0];
                            m -= step[i, 1];
                            goto next;
                        }

                    }
                    isEnd = true;
                }
                else
                {
                    for (int i = 2; i >=0; i--)
                    {

                        if (n >= step[i, 0] && m >= step[i, 1])
                        {
                            n -= step[i, 0];
                            m -= step[i, 1];
                            goto next;
                        }

                    }
                    isEnd = true;
                }
            next: turn = !turn;
            }

            if (turn)
            {
                Console.WriteLine("Hanako");
            }
            else
            {
                Console.WriteLine("Ciel");
            }
            Console.ReadKey();
        }

    }
}


using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    ReadData re;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        re = new ReadData();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        long H = re.l();
        long W = re.l();
        if(H == 1 || W == 1){
            H = Math.Max(H,W);
            long count = H / 6 * 6;
            if(H % 6 == 4){
                count += 2;
            }
            if(H % 6 == 5){
                count += 4;
            }
            sb.Append(count + "\n");
            return;
        }
        if(H >= 3 && W >= 3){
            sb.Append(H*W/2*2+"\n");
            return;
        }
        if(H != 2){
            W = H;
            H = 2;
        }
        if(W == 2){
            sb.Append("0\n");
            return;
        }
        else if(W == 3){
            sb.Append("4\n");
            return;
        }
        else if(W == 7){
            sb.Append("12\n");
            return;
        }
        sb.Append((W*2)+"\n");
    }
}
class PrimeFactor{
    int[] LowestPrimeFactor;
    public int[] Prime;
    int N;
    public PrimeFactor(int N0){
        N = N0;
        N++;
        LowestPrimeFactor = new int[N];
        List<int> prime = new List<int>();
        for(int i=1;i<N;i++){
            LowestPrimeFactor[i] = i;
        }
        for(int i=2;i<N;i++){
            if(LowestPrimeFactor[i] == i){
                prime.Add(i);
            }
            for(int j=0;j<prime.Count;j++){
                if(prime[j]*i < N && prime[j] <= LowestPrimeFactor[i]){
                    LowestPrimeFactor[prime[j]*i] = prime[j];
                }
                else{
                    break;
                }
            }
        }
        Prime = prime.ToArray();
    }
    public int[] PrimeDivide(int X){
        List<int> ans = new List<int>();
        while(X != 1){
            ans.Add(LowestPrimeFactor[X]);
            X /= LowestPrimeFactor[X];
        }
        return ans.ToArray();
    }
    public int[] PrimeDivide(long X0){
        List<int> ans = new List<int>();
        int X = (int)X0;
        int i=0;
        while(X >= N){
            if(X % Prime[i] == 0){
                ans.Add(Prime[i]);
                X /= Prime[i];
            }
            else{
                i++;
                if(i == Prime.Length){
                    ans.Add(X);
                    X = 1;
                }
            }
        }
        while(X != 1){
            ans.Add(LowestPrimeFactor[X]);
            X /= LowestPrimeFactor[X];
        }
        return ans.ToArray();
    }
    public int[] Divisor(int X){
        List<int> ans = new List<int>();
        ans.Add(1);
        while(X != 1){
            int p = LowestPrimeFactor[X];
            int c = 0;
            while(X % p == 0){
                X /= p;
                c++;
            }
            int l = ans.Count;
            int d = p;
            for(int i=1;i<=c;i++){
                for(int j=0;j<l;j++){
                    ans.Add(d*ans[j]);
                }
                d *= p;
            }
        }
        return ans.ToArray();
    }
    public int[] Divisor(long X0){
        List<int> ans = new List<int>();
        int X = (int)X0;
        ans.Add(1);
        int i=0;
        while(X >= N){
            if(X % Prime[i] == 0){
                int p = Prime[i];
                int c = 0;
                while(X % p == 0){
                    X /= p;
                    c++;
                }
                int l = ans.Count;
                int d = p;
                for(int k=1;k<=c;k++){
                    for(int j=0;j<l;j++){
                        ans.Add(d*ans[j]);
                    }
                    d *= p;
                }
            }
            else{
                i++;
                if(i == Prime.Length){
                    int l = ans.Count;
                    for(int j=0;j<l;j++){
                        ans.Add(X*ans[j]);
                    }
                    X = 1;
                }
            }
        }
        while(X != 1){
            int p = LowestPrimeFactor[X];
            int c = 0;
            while(X % p == 0){
                X /= p;
                c++;
            }
            int l = ans.Count;
            int d = p;
            for(int k=1;k<=c;k++){
                for(int j=0;j<l;j++){
                    ans.Add(d*ans[j]);
                }
                d *= p;
            }
        }
        return ans.ToArray();
    }
    public int Phi(int X){
        int ans = X;
        while(X != 1){
            int p = LowestPrimeFactor[X];
            while(X % p == 0){
                X /= p;
            }
            ans /= p;
            ans *= p-1;
        }
        return ans;
    }
    public int Phi(long X0){
        int ans = (int)X0;
        int X = (int)X0;
        int i=0;
        while(X >= N){
            if(X % Prime[i] == 0){
                int p = Prime[i];
                while(X % p == 0){
                    X /= p;
                }
                ans /= p;
                ans *= p-1;
            }
            else{
                i++;
                if(i == Prime.Length){
                    int p = X;
                    ans /= p;
                    ans *= p-1;
                    X = 1;
                }
            }
        }
        while(X != 1){
            int p = LowestPrimeFactor[X];
            while(X % p == 0){
                X /= p;
            }
            ans /= p;
            ans *= p-1;
        }
        return ans;
    }
}
class ReadData{
    string[] str;
    int counter;
    public ReadData(){
        counter = 0;
    }
    public string s(){
        if(counter == 0){
            str = Console.ReadLine().Split(' ');
            counter = str.Length;
        }
        counter--;
        return str[str.Length-counter-1];
    }
    public int i(){
        return int.Parse(s());
    }
    public long l(){
        return long.Parse(s());
    }
    public double d(){
        return double.Parse(s());
    }
    public int[] ia(int N){
        int[] ans = new int[N];
        for(int j=0;j<N;j++){
            ans[j] = i();
        }
        return ans;
    }
    public int[] ia(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        int[] ans = new int[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = int.Parse(str[j]);
        }
        return ans;
    }
    public long[] la(int N){
        long[] ans = new long[N];
        for(int j=0;j<N;j++){
            ans[j] = l();
        }
        return ans;
    }
    public long[] la(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        long[] ans = new long[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = long.Parse(str[j]);
        }
        return ans;
    }
    public double[] da(int N){
        double[] ans = new double[N];
        for(int j=0;j<N;j++){
            ans[j] = d();
        }
        return ans;
    }
    public double[] da(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        double[] ans = new double[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = double.Parse(str[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int[] f,int[] t){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<f.Length;j++){
            ans[f[j]].Add(t[j]);
            ans[t[j]].Add(f[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int M){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
    public List<int>[] g(){
        int N = i();
        int M = i();
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
}
public static class Define{
    public const long mod = 1000000007;
}
public static class Debug{
    public static void Print(double[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(long[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(int[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.IO;
using System.Linq;
using System.Threading;

class Program
{
    string[] all;
    string dna = "ACGT";
    int[, ,] best;
    int[, ,] best2;
    long[, , ,] dp;
    int mod = 1000000009;
    int[, ,] p;
    void solve()
    {
        int n = nextInt();
        int m = nextInt();
        all = new string[m];
        for (int i = 0; i < m; i++)
            all[i] = nextString();
        int maxL = 0;
        for (int i = 0; i < m; i++)
            maxL = Math.Max(maxL, all[i].Length);
        best = new int[m, maxL + 1, 4];
        best2 = new int[m, maxL + 1, 4];

        p = new int[m, maxL + 1, 4];
        for (int i = 0; i < m; i++)
            for (int j = 0; j <= all[i].Length; j++)
                for (int k = 0; k < 4; k++)
                {
                    string cur = all[i].Substring(0, j) + dna[k];
                    for (int bestL = 0; bestL <= cur.Length; bestL++)
                    {
                        for (int r = 0; r < all.Length; r++)
                        {
                            if (all[r].StartsWith(cur.Substring(cur.Length - bestL)))
                            {
                                best[i, j, k] = r;
                                best2[i, j, k] = bestL;
                            }
                            if (all[r] == cur.Substring(cur.Length - bestL))
                                p[i, j, k] = bestL;
                        }
                    }
                }

        dp = new long[n + 1, m, maxL + 1, maxL + 1];
        for (int i = 0; i <= n; i++)
            for (int j = 0; j < m; j++)
                for (int k = 0; k <= maxL; k++)
                    for (int oks = 0; oks <= maxL; oks++)
                        dp[i, j, k, oks] = -1;
        long res = go(n, 0, 0, 0);
        println(res);

    }

    private long go(int rem, int who, int len, int oks)
    {
        if (dp[rem, who, len, oks] != -1)
            return dp[rem, who, len, oks];
        long res = 0;
        if (rem == 0)
        {
            if (oks == len)
                res = 1;

            return dp[rem, who, len, oks] = res;
        }
        for (int i = 0; i < 4; i++)
        {
            int nwho = best[who, len, i];
            int nlen = best2[who, len, i];
            if (p[who, len, i] + oks >= len + 1)
            {
                res += go(rem - 1, nwho, nlen, nlen);
            }
            else
            {
                if (nlen + oks < len + 1)
                    continue;
                res += go(rem - 1, nwho, nlen, oks - (len + 1 - nlen));
            }
            if (res >= mod)
                res -= mod;
        }
        return dp[rem, who, len, oks] = res;
    }

    ////////////
    private void println(int[] ar)
    {
        for (int i = 0; i < ar.Length; i++)
        {
            if (i == ar.Length - 1)
                println(ar[i]);
            else
                print(ar[i] + " ");
        }
    }
    private void println(int[] ar, bool add)
    {
        int A = 0;
        if (add)
            A++;
        for (int i = 0; i < ar.Length; i++)
        {
            if (i == ar.Length - 1)
                println(ar[i] + A);
            else
                print((ar[i] + A) + " ");
        }
    }

    private void println(string Stringst)
    {
        Console.WriteLine(Stringst);
    }
    private void println(char charnum)
    {
        Console.WriteLine(charnum);
    }
    private void println(int Intnum)
    {
        Console.WriteLine(Intnum);
    }
    private void println(long Longnum)
    {
        Console.WriteLine(Longnum);
    }
    private void println(double Doublenum)
    {
        string s = Doublenum.ToString(CultureInfo.InvariantCulture);
        Console.WriteLine(s);
    }

    private void print(string Stringst)
    {
        Console.Write(Stringst);
    }
    private void print(int Intnum)
    {
        Console.Write(Intnum);
    }
    private void print(char charnum)
    {
        Console.Write(charnum);
    }
    private void print(long Longnum)
    {
        Console.Write(Longnum);
    }
    private void print(double Doublenum)
    {
        Console.Write(Doublenum);
    }

    string[] inputLine = new string[0];
    int inputInd = 0;
    string nextLine()
    {
        return Console.ReadLine();
    }
    void readInput()
    {
        if (inputInd != inputLine.Length)
            throw new Exception();
        inputInd = 0;
        inputLine = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        if (inputLine.Length == 0)
            readInput();

    }
    int nextInt()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return int.Parse(inputLine[inputInd++]);
    }
    long nextLong()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return long.Parse(inputLine[inputInd++]);
    }
    double nextDouble()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return double.Parse(inputLine[inputInd++]);
    }
    string nextString()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return inputLine[inputInd++];
    }
    static void Main(string[] args)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
        new Program().solve();
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApp12
{
    class Program
    {
        static int[] SAI(string input, char sep = ' ')
        {
            return input.Split(sep).Select(x => int.Parse(x)).ToArray();
        }

        static int[] SAI(string input, string sep)
        {
            return new Regex(sep).Split(input).Select(x => int.Parse(x)).ToArray();
        }

        static long[] AI(char sep = ' ')
        {
            return Console.ReadLine().Split(sep).Select(x => long.Parse(x)).ToArray();
        }

        static uint[] AI(string sep)
        {
            return new Regex(sep).Split(Console.ReadLine()).Select(x => uint.Parse(x)).ToArray();
        }

        static List<int> LI(char sep = ' ')
        {
            return Console.ReadLine().Split(sep).Select(x => int.Parse(x)).ToList();
        }

        static Int64[] AI64(char sep = ' ')
        {
            return Console.ReadLine().Split(sep).Select(x => Int64.Parse(x)).ToArray();
        }

        static List<Int64> LI64(char sep = ' ')
        {
            return Console.ReadLine().Split(sep).Select(x => Int64.Parse(x)).ToList();
        }

        static void Main(string[] args)
        {
            var a = AI();
            if (a[1] == 1)
            {
                Console.Write(a[0]);
                return;
            }
            long result = 0;
            while (a[0] > 0)
            {
                result = result <<  1;
                ++result;
                a[0] = a[0] >> 1;
            }
            Console.WriteLine(result);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;

namespace CodeforcesTemplate
{
    public static class Permutation
    {
        private static bool Next(ref int[] arr)
        {
            int k, j, l;
            for (j = arr.Length - 2; (j >= 0) && (arr[j] >= arr[j + 1]); j--) { }
            if (j == -1)
            {
                arr = arr.OrderBy(c => c).ToArray();
                return false;
            }
            for (l = arr.Length - 1; (arr[j] >= arr[l]) && (l >= 0); l--) { }
            var tmp = arr[j];
            arr[j] = arr[l];
            arr[l] = tmp;
            for (k = j + 1, l = arr.Length - 1; k < l; k++, l--)
            {
                tmp = arr[k];
                arr[k] = arr[l];
                arr[l] = tmp;
            }
            return true;
        }
        public static IEnumerable<int[]> AllPermutations(int[] arr)
        {
            do yield return arr;
            while (Next(ref arr));
        }
    }

    class Program
    {
        private const string FILENAME = "distance4";

        private const string INPUT = FILENAME + ".in";

        private const string OUTPUT = FILENAME + ".out";

        private static Stopwatch _stopwatch = new Stopwatch();

        private static StreamReader _reader = null;
        private static StreamWriter _writer = null;

        private static string[] _curLine;
        private static int _curTokenIdx;

        private static char[] _whiteSpaces = new char[] { ' ', '\t', '\r', '\n' };

        private static string ReadNextToken()
        {
            if (_curTokenIdx >= _curLine.Length)
            {
                //Read next line
                string line = _reader.ReadLine();
                if (line != null)
                    _curLine = line.Split(_whiteSpaces, StringSplitOptions.RemoveEmptyEntries);
                else
                    _curLine = new string[] { };
                _curTokenIdx = 0;
            }

            if (_curTokenIdx >= _curLine.Length)
                return null;

            return _curLine[_curTokenIdx++];
        }

        private static int ReadNextInt()
        {
            return int.Parse(ReadNextToken());
        }

        private static void RunTimer()
        {
#if (DEBUG)

            _stopwatch.Start();

#endif
        }

        private static void Main(string[] args)
        {

#if (DEBUG)
            double before = GC.GetTotalMemory(false);
#endif

            long[] arr = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();

            long n = arr[0];

            long k = arr[1];

            if (k == 1)
            {
                Console.WriteLine(n);
                return;
            }
            long answer = 1;

            while (answer <= n)
            {
                answer <<= 1;
            }

            Console.WriteLine(answer - 1);

#if (DEBUG)
            _stopwatch.Stop();

            double after = GC.GetTotalMemory(false);

            double consumedInMegabytes = (after - before) / (1024 * 1024);

            Console.WriteLine($"Time elapsed: {_stopwatch.Elapsed}");

            Console.WriteLine($"Consumed memory (MB): {consumedInMegabytes}");

            Console.ReadKey();
#endif

        }

        private static int CountLeadZeros(string source)
        {
            int countZero = 0;

            for (int i = source.Length - 1; i >= 0; i--)
            {
                if (source[i] == '0')
                {
                    countZero++;
                }

                else
                {
                    break;
                }

            }

            return countZero;
        }

        private static string ReverseString(string source)
        {
            char[] reverseArray = source.ToCharArray();

            Array.Reverse(reverseArray);

            string reversedString = new string(reverseArray);

            return reversedString;
        }

        private static int[] CopyOfRange(int[] src, int start, int end)
        {
            int len = end - start;
            int[] dest = new int[len];
            Array.Copy(src, start, dest, 0, len);
            return dest;
        }

        private static string StreamReader()
        {

            if (_reader == null)
                _reader = new StreamReader(INPUT);
            string response = _reader.ReadLine();
            if (_reader.EndOfStream)
                _reader.Close();
            return response;

        }

        private static void StreamWriter(string text)
        {

            if (_writer == null)
                _writer = new StreamWriter(OUTPUT);
            _writer.WriteLine(text);

        }

        private static int BFS(int start, int end, int[,] arr)
        {
            Queue<KeyValuePair<int, int>> queue = new Queue<KeyValuePair<int, int>>();
            List<bool> visited = new List<bool>();
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                visited.Add(false);
            }

            queue.Enqueue(new KeyValuePair<int, int>(start, 0));
            KeyValuePair<int, int> node;
            int level = 0;
            bool flag = false;

            while (queue.Count != 0)
            {
                node = queue.Dequeue();
                if (node.Key == end)
                {
                    return node.Value;
                }
                flag = false;
                for (int i = 0; i < arr.GetLength(0); i++)
                {

                    if (arr[node.Key, i] == 1)
                    {
                        if (visited[i] == false)
                        {
                            if (!flag)
                            {
                                level = node.Value + 1;
                                flag = true;
                            }
                            queue.Enqueue(new KeyValuePair<int, int>(i, level));
                            visited[i] = true;
                        }
                    }
                }

            }
            return 0;

        }

        private static void Write(string source)
        {
            File.WriteAllText(OUTPUT, source);
        }

        private static string ReadString()
        {
            return File.ReadAllText(INPUT);
        }

        private static void WriteStringArray(string[] source)
        {
            File.WriteAllLines(OUTPUT, source);
        }

        private static string[] ReadStringArray()
        {
            return File.ReadAllLines(INPUT);
        }

        private static int[] GetIntArray()
        {
            return ReadString().Split(' ').Select(int.Parse).ToArray();
        }

        private static double[] GetDoubleArray()
        {
            return ReadString().Split(' ').Select(double.Parse).ToArray();
        }

        private static int[,] ReadINT2XArray(List<string> lines)
        {
            int[,] arr = new int[lines.Count, lines.Count];

            for (int i = 0; i < lines.Count; i++)
            {
                int[] row = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

                for (int j = 0; j < lines.Count; j++)
                {
                    arr[i, j] = row[j];
                }
            }

            return arr;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.IO;
using System.Globalization;
using System.Collections;

using Pair = System.Collections.Generic.KeyValuePair<int, long>;

namespace Task
{
    internal class Program
    {
        int cnt = 0;
        Dictionary<Pair, long> data = new Dictionary<Pair, long>();
        bool[,] was = new bool[100, 100];

        long xfx(int x, int y, long z)
        {
            if (x == -1)
                return 0;
            if (z == 0)
                return 0;
            if (y == 1)
            {
                if (x == 0)
                    return 1;
                return 0;
            }

            var key = new Pair((x << 16) | y, z);

            if (data.ContainsKey(key))
                return data[key];

            long hsize = getSize(y - 1);

            long ans = 0;

            if (z >= hsize)
            {
                long temp1 = xfx(x, y - 1, hsize);
                long temp2 = xfx(x - 1, y - 1, z - hsize);
                ans += temp1 + temp2;
            }
            else
            {
                long temp = xfx(x, y - 1, z);
                ans += temp;
            }

            data[key] = ans;
            return ans;
        }

        long getSize(int y)
        {
            return 1L << (y - 1);
        }

        long get(int x, long z)
        {
            long ans = 0;

            int num = 1;
            while (z > 0)
            {
                long size = getSize(num);
                long temp = xfx(x, num, Math.Min(size, z));

                z -= size;
                ans += temp;

                num++;
            }

            return ans;

        }

        private void Solve()
        {
            long n = io.NextLong();
            long t = io.NextLong();

            long x = 1;
            int p = 0;

            while (x < t)
            {
                x <<= 1;
                p++;
            }

            if (x != t)
            {
                io.Print(0);
                return;
            }

            long ans = get(p, n + 1);
            if (t == 1)
                ans--;
            io.Print(ans);

        }

        #region Program

        private readonly MyIo io;

        public Program(MyIo io)
        {
            this.io = io;
        }

        public static void Main(string[] args)
        {
            using (MyIo io = new MyIo())
            {
                new Program(io).Solve();
            }
        }

        #endregion
    }

    #region MyIo

    internal class MyIo : IDisposable
    {
        private readonly TextReader inputStream;
        private readonly TextWriter outputStream = Console.Out;

        private string[] tokens;
        private int pointer;

        public MyIo()
#if  LOCAL_TEST
            : this(new StreamReader("..\\..\\input.txt"))
#else
            : this(System.Console.In)
#endif
        {

        }

        public MyIo(TextReader inputStream)
        {
            this.inputStream = inputStream;
        }

        public string NextLine()
        {
            try
            {
                return inputStream.ReadLine();
            }
            catch (IOException)
            {
                return null;
            }
        }

        public string NextString()
        {
            try
            {
                while (tokens == null || pointer >= tokens.Length)
                {
                    tokens = NextLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    pointer = 0;
                }
                return tokens[pointer++];
            }
            catch (IOException)
            {
                return null;
            }
        }

        public int NextInt()
        {
            return Next<int>();
        }

        public long NextLong()
        {
            return Next<long>();
        }

        public T Next<T>()
        {
            return (T)Convert.ChangeType(NextString(), typeof(T));
        }

        public IEnumerable<T> NextSeq<T>(int n)
        {
            for (int i = 0; i < n; i++)
                yield return Next<T>();
        }

        public void Print(string format, params object[] args)
        {
            outputStream.Write(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public void PrintLine(string format, params object[] args)
        {
            Print(format, args);

            outputStream.WriteLine();
        }

        public void Print(bool o)
        {
            Print(o ? "YES" : "NO");
        }

        public void PrintLine(bool o)
        {
            PrintLine(o ? "YES" : "NO");
        }

        public void Print<T>(T o)
        {
            outputStream.Write(o);
        }

        public void PrintLine<T>(T o)
        {
            outputStream.WriteLine(o);
        }

        public void Close()
        {
            inputStream.Close();
            outputStream.Close();
        }

        void IDisposable.Dispose()
        {
            Close();

#if  LOCAL_TEST
            Console.ReadLine();
#endif
        }
    }

    #endregion
}


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Threading;

namespace First
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = Convert.ToInt32(Console.ReadLine());
            string[] s = Console.ReadLine().Split(' ');
            int[] days = new int[n];

            for (int i = 0; i < n; i++)
            {
                days[i] = Convert.ToInt32(s[i]);
            }

            List<int> normalYear = new List<int>()
                {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            List<int> leapYear = new List<int>()
                {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

            List<int> template = new List<int>();

            template.AddRange(normalYear);
            template.AddRange(normalYear);
            template.AddRange(leapYear);
            template.AddRange(normalYear);
            template.AddRange(normalYear);

            for (int i = 0; i < template.Count; i++)
            {
                if (template[i] == days[0])
                {
                    bool ok = true;
                    for (int j = 1; j < n; j++)
                    {
                        if ((i + j) >= template.Count || template[i + j] != days[j])
                        {
                            ok = false;
                            break;
                        }
                    }

                    if (ok)
                    {
                        Console.WriteLine("Yes");
                        return;
                    }
                }
            }

            Console.WriteLine("No");

        }
    }
}


using System;

namespace Olymp
{
    class Program
    {

        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            //int n = 763116;
            if (n == 1) Console.WriteLine(1);
            else if (n == 2) Console.WriteLine(2);
            else{

                Console.WriteLine(Numb(n));
            }
        }

        static public long Numb(int n)
        {
            long max = n * (n - 1) * (n - 2) / GCD(n, n - 2);
            for (int q = 10;  q > 0; q--)
            {
                long t = n * (n - 1) * (n - 2) / GCD(n, n - 2);
                for (long i = n; i > n - 10 && i > 2; i--)
                {
                    for (long j = i - 1; j > i - 11 && j > 1; j--)
                    {
                        for (long k = j - 1; k > i - 12 && k > 0; k--)
                        {
                            if ((i * j * k) / GCD(i, k) > t)
                                t = i * j * k / GCD(i, k);
                        }
                    }
                }
                if (t > max) max = t;
            }
            return max;
        }
        public static long GCD(long a, long b)
        {
            if (b == 0) return a;
            else return GCD(b, a % b);
        }
    }
}


using System;

namespace Olymp
{
    class Program
    {

        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            if (n == 1) Console.WriteLine(1);
            else if (n == 2) Console.WriteLine(2);
            else
                Console.WriteLine(Numb(n));
        }

        static public long Numb(int n)
        {
            long max = n * (n - 1) * (n - 2) / GCD(n, n - 2);
            for (int q = 10;  q > 0; q--)
            {
                long t = n * (n - 1) * (n - 2) / GCD(n, n - 2);
                for (long i = n; i > n - 10 && i > 2; i--)

                    for (long j = i - 1; j > i - 10 && j > 1; j--)

                        for (long k = j - 1; k > i - 11 && k > 0; k--)

                            if ((i * j * k) / GCD(i, k) > t)
                                t = i * j * k / GCD(i, k);

                if (t > max) max = t;
            }
            return max;
        }
        public static long GCD(long a, long b)
        {
            if (b == 0) return a;
            else return GCD(b, a % b);
        }
    }
}


using System;
using System.IO;
using System.Linq;
using System.Text;

namespace Fox_Dividing_Cheese
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            int a = Next();
            int b = Next();

            var divs = new[] {2, 3, 5};
            var na = new int[divs.Length];
            var nb = new int[divs.Length];
            for (int i = 0; i < divs.Length; i++)
            {
                while (a%divs[i] == 0)
                {
                    a /= divs[i];
                    na[i]++;
                }
                while (b%divs[i] == 0)
                {
                    b /= divs[i];
                    nb[i]++;
                }
            }
            if (a == b)
            {
                writer.WriteLine(na.Select((t, i) => Math.Abs(t - nb[i])).Sum());
            }
            else
            {
                writer.WriteLine("-1");
            }

            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharp
{
    class _371B
    {
        public static void Main()
        {
            var tokens = Console.ReadLine().Split();

            var a = Operations(int.Parse(tokens[0]));
            var b = Operations(int.Parse(tokens[1]));

            Console.WriteLine(a.Keys.Intersect(b.Keys).Select(key => a[key] + b[key]).DefaultIfEmpty(-1).Min());
        }

        private static Dictionary<int, int> Operations(int n)
        {
            var operations = new Dictionary<int, int> { { n, 0 } };
            var queue = new Queue<int>(new int[] { n });

            while (queue.Any())
            {
                int x = queue.Dequeue();

                if (x % 2 == 0 && !operations.ContainsKey(x / 2))
                {
                    operations.Add(x / 2, operations[x] + 1);
                    queue.Enqueue(x / 2);
                }

                if (x % 3 == 0 && !operations.ContainsKey(x / 3))
                {
                    operations.Add(x / 3, operations[x] + 1);
                    queue.Enqueue(x / 3);
                }

                if (x % 5 == 0 && !operations.ContainsKey(x / 5))
                {
                    operations.Add(x / 5, operations[x] + 1);
                    queue.Enqueue(x / 5);
                }
            }

            return operations;
        }
    }
}