import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SweetsGame {
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
		char[][] arr=new char[5][];
		int index=18;
		int mask=0;
		for (int i = 0; i < arr.length; i++) {
			String s=r.readLine().trim().replaceAll("[ ]+", "");

			arr[i]=s.toCharArray();
			for (int j = 0; j < arr[i].length; j++) {
				if(arr[i][j]=='O')
					mask|=(1<<index);
				index--;
			}

		}
		org=mask;
		String[] res= {"Karlsson","Lillebror"};
		System.out.println(res[go(mask,0)]);
		}
	static Integer[][] dp=new Integer[1<<19][2];
	static int org;
	public static char[][] encode(int mask){
		char[][] arr=new char[5][];
		arr[0]=new char[3];
		arr[1]=new char[4];
		arr[2]=new char[5];
		arr[3]=new char[4];
		arr[4]=new char[3];
		int x=0,y=0;
		for(int i=18;i>=0;i--){
			arr[x][y++]=(mask&1<<i)==(1<<i)?'o':'.';
			if(i==16){
				x++;
				y=0;
			}
			if(i==12){
				x++;
				y=0;
			}
			if(i==7){
				x++;
				y=0;
			}
			if(i==3){
				x++;
				y=0;
			}
		}
//		for (int i = 0; i < arr.length; i++) {
//			System.out.println(new String(arr[i]));
//		}
//		System.out.println();
		return arr;
	}
	public static int go(int mask,int turn){
		if(mask==0){
			return 1-turn;
		}
		if(dp[mask][turn]!=null)return dp[mask][turn];
		char[][]arr=encode(mask);
		for(int i=0;i<5;i++){
			for(int j=0;j<arr[i].length;j++){
				if(arr[i][j]=='o'){
					int newMask=mask;
					for(int k=j;k<arr[i].length;k++){
						if(arr[i][k]=='o'){
							int index=get(i,k);
							newMask&=(~(1<<index));
							if(go(newMask,1-turn)==turn){
								return dp[mask][turn]=turn;
							}
						}
						else break;
					}

						newMask=mask;
						int col=j;
						for(int k=i;k<arr.length;k++){
							if(col>=0&&col<arr[k].length&&arr[k][col]=='o'){
								int index=get(k,col);
								newMask&=(~(1<<index));
								if(go(newMask,1-turn)==turn){
									return dp[mask][turn]=turn;
								}
								if(k>=2)col--;
							}
							else break;
						}

						newMask=mask;
					newMask=mask;
					int r=i , c=j;
					while(true){
						if(r<arr.length&&c>=0&&c<arr[r].length&&arr[r][c]=='o'){
							int index=get(r,c);
							newMask&=(~(1<<index));
							if(go(newMask,1-turn)==turn){
								return dp[mask][turn]=turn;
							}
							r++;
							if(r>2);else
							c++;
						}
						else break;
					}
				}
			}
		}
		return dp[mask][turn]=1-turn;
	}
	private static int get(int i, int k) {
		int[][] arr={{18,17,16},{15,14,13,12},{11,10,9,8,7},{6,5,4,3},{2,1,0}};
		return arr[i][k];
	}

}


import java.util.*;
import java.io.*;
import java.math.BigInteger;
import static java.lang.Math.*;

public class Main implements Runnable {
	StringTokenizer tokenizer = new StringTokenizer("");
	BufferedReader in;
	PrintStream out;

	int[] dm;
	int[] d = new int[1 << 19];
	int LEN = 47 * 6;

	public void debug(String s) {
		System.out.println(s);
	}

	public static void main(String[] args) {
		new Thread(new Main()).start();
	}

	int win(int msk) {
		if (d[msk] != -1) {
			return d[msk];
		}
		for (int i = 0; i < LEN; i++) {
			int rmsk = dm[i];
			if (((msk & rmsk) == rmsk) && win(msk & (~rmsk)) == 0)
				return d[msk] = 1;
		}
		return d[msk] = 0;
	}

	public void run() {
		try {
			Locale.setDefault(Locale.US);
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintStream(System.out);

			Arrays.fill(d, -1);
			d[0] = 0;

			dm = new int[LEN];

			int[][] a = new int[][] { new int[] { 0, 1, 2 },
					new int[] { 3, 4, 5, 6 }, new int[] { 7, 8, 9, 10, 11 },
					new int[] { 12, 13, 14, 15 }, new int[] { 16, 17, 18 } };
			int[] p = new int[19];

			p[0] = 2;
			p[2] = 11;
			p[11] = 18;
			p[18] = 16;
			p[16] = 7;
			p[7] = 0;

			p[9] = 9;

			p[4] = 5;
			p[5] = 10;
			p[10] = 14;
			p[14] = 13;
			p[13] = 8;
			p[8] = 4;

			p[1] = 6;
			p[6] = 15;
			p[15] = 17;
			p[17] = 12;
			p[12] = 3;
			p[3] = 1;

			int[][] w = new int[6][19];
			for (int i = 0; i < 19; i++)
				w[0][i] = i;
			for (int rot = 1; rot < 6; rot++) {
				for (int i = 0; i < 19; i++) {
					w[rot][i] = p[w[rot - 1][i]];
				}
			}

			int cnt = 0;
			for (int rot = 0; rot < 6; rot++) {
				for (int row = 0; row < 5; row++) {
					for (int l = 0; l < a[row].length; l++) {
						for (int r = l; r < a[row].length; r++) {
							int msk = 0;
							for (int t = l; t <= r; t++)
								msk |= (1 << w[rot][a[row][t]]);
							dm[cnt++] = msk;
						}
					}
				}
			}

			//for (int i = 0; i < dm.length; i++) out.println(dm[i]);

			int pos = 0;

			String s = "";
			for (int i = 0; i < 5; i++) s += in.readLine();
			s = s.replace(" ", "");
			//System.out.println(s);
			for (int i = 0; i < s.length(); i++) if (s.charAt(i) == 'O') pos |= (1 << i);

			out.println(win(pos) == 1 ? "Karlsson" : "Lillebror");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	boolean seekForToken() {
		while (!tokenizer.hasMoreTokens()) {
			String s = null;
			try {
				s = in.readLine();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (s == null)
				return false;
			tokenizer = new StringTokenizer(s);
		}
		return true;
	}

	String nextToken() {
		return seekForToken() ? tokenizer.nextToken() : null;
	}

	int nextInt() {
		return Integer.parseInt(nextToken());
	}

	long nextLong() {
		return Long.parseLong(nextToken());
	}

	double nextDouble() {
		return Double.parseDouble(nextToken());
	}

	BigInteger nextBig() {
		return new BigInteger(nextToken());
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class D {

	static ArrayList<Integer> cuts = new ArrayList<Integer>(102);

	static Boolean[] memo;

	static boolean win(int msk)
	{
		if(msk == 0)
			return false;
		if(memo[msk] != null)
			return memo[msk];
		boolean win = false;
		for(int cut: cuts)
			if((msk & cut) == cut)
				win |= !win(msk ^ cut);
		return memo[msk] = win;
	}

	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		for(int i = 0; i < 19; ++i)
			cuts.add(1<<i);
		int[][] a = {{0, 1, 2}, {3, 4, 5, 6}, {7, 8, 9, 10, 11}, {12, 13, 14, 15}, {16, 17, 18}};
		int[][] b = {{0, 3, 7}, {1, 4, 8, 12}, {2, 5, 9, 13, 16}, {6, 10, 14, 17}, {11, 15, 18}};
		int[][] c = {{2, 6, 11}, {1, 5, 10, 15}, {0, 4, 9, 14, 18}, {3, 8, 13, 17}, {7, 12, 16}};
		addCuts(cuts, a);
		addCuts(cuts, b);
		addCuts(cuts, c);

		int msk = 0;
		for(int i = 0; i < 19; ++i)
			if(sc.next().charAt(0) == 'O')
				msk |= 1<<i;
		memo = new Boolean[1<<19];
		out.println(win(msk)?"Karlsson":"Lillebror");
		out.flush();
		out.close();
	}

	static void addCuts(ArrayList<Integer> cuts, int[][] x)
	{
		for(int[] y: x)
			for(int len = 2; len <= y.length; ++len)
				for(int i = 0; i + len <= y.length; ++i)
				{
					int msk = 0;
					for(int j = i; j < i + len; ++j)
						msk |= 1<<y[j];
					cuts.add(msk);
				}
	}

	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {return Integer.parseInt(next());}

		public long nextLong() throws IOException {return Long.parseLong(next());}

		public String nextLine() throws IOException {return br.readLine();}

		public double nextDouble() throws IOException
		{
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if(x.charAt(0) == '-')
			{
				neg = true;
				start++;
			}
			for(int i = start; i < x.length(); i++)
				if(x.charAt(i) == '.')
				{
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				}
				else
				{
					sb.append(x.charAt(i));
					if(dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg?-1:1);
		}

		public boolean ready() throws IOException {return br.ready();}

	}
}


import java.io.*;
import java.util.*;

public class E {

    int[][] f;
    int[] d;

    void solve() throws IOException {
        in("__std"); out("__std");

        f = new int[5][];
        int k = 0;
        for (int i = 0; i < 5; ++i) {
            char[] l = readLine().toCharArray();
            f[i] = new int[l.length];
            Arrays.fill(f[i], -1);
            for (int j = 0; j < l.length; ++j) {
                if (l[j] == 'O') {
                    f[i][j] = k++;
                }
            }
        }
        int c = 1 << k;
        d = new int[c];
        if (win(c - 1)) {
            println("Karlsson");
        } else {
            println("Lillebror");
        }

        exit();
    }

    boolean win(int c) {
        if (d[c] == 0) {
            int r = 2;
            for (int i = 0; i < 5 && r == 2; ++i) {
                for (int j = 0; j < f[i].length && r == 2; ++j) {
                    if (f[i][j] != -1 && (c & (1 << f[i][j])) != 0) {
                        //right
                        int cc = 0;
                        int ii = i;
                        int jj = j;
                        while (jj < f[ii].length && r == 2 && f[ii][jj] != -1 && (c & (1 << f[ii][jj])) != 0) {
                            cc |= 1 << f[ii][jj];
                            if (!win(c & (~cc))) r = 1;
                            jj += 2;
                        }
                        if (r == 2) {
                            //diag left
                            cc = 0;
                            ii = i;
                            jj = j;
                            while (ii < 5 && jj >= 0 && r == 2 && f[ii][jj] != -1 && (c & (1 << f[ii][jj])) != 0) {
                                cc |= 1 << f[ii][jj];
                                if (!win(c & (~cc))) r = 1;
                                ++ii;
                                --jj;
                            }
                            if (r == 2) {
                                //diag right
                                cc = 0;
                                ii = i;
                                jj = j;
                                while (ii < 5 && jj < f[ii].length && r == 2 && f[ii][jj] != -1 && (c & (1 << f[ii][jj])) != 0) {
                                    cc |= 1 << f[ii][jj];
                                    if (!win(c & (~cc))) r = 1;
                                    ++ii;
                                    ++jj;
                                }
                            }
                        }
                    }
                }
            }
            d[c] = r;
        }
        return d[c] == 1;
    }

    void in(String name) throws IOException {
        if (name.equals("__std")) {
            in = new BufferedReader(new InputStreamReader(System.in));
        } else {
            in = new BufferedReader(new FileReader(name));
        }
    }

    void out(String name) throws IOException {
        if (name.equals("__std")) {
            out = new PrintWriter(System.out);
        } else {
            out = new PrintWriter(name);
        }
    }

    void exit() {
        out.close();
        System.exit(0);
    }

    int readInt() throws IOException {
        return Integer.parseInt(readToken());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readToken());
    }

    String readLine() throws IOException {
        st = null;
        return in.readLine();
    }

    String readToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    boolean eof() throws IOException {
        return !in.ready();
    }

    void print(String format, Object ... args) {
        out.println(new Formatter().format(format, args));
    }

    void println(String format, Object ... args) {
        out.println(new Formatter().format(format, args));
    }

    void print(Object value) {
        out.print(value);
    }

    void println(Object value) {
        out.println(value);
    }

    void println() {
        out.println();
    }

    StringTokenizer st;

    BufferedReader in;
    PrintWriter out;

    public static void main(String[] args) throws IOException {
        new E().solve();
    }
}


import static java.util.Arrays.*;
import static java.lang.Math.*;
import static java.math.BigInteger.*;
import java.util.*;
import java.math.*;
import java.io.*;

public class E implements Runnable
{
    String file = "input";

    boolean TEST = false;

    int[] mapCoord = new int[100];
    int[] mapBit = new int[100];
    char[][] cs;
    int rn = 5, cn = 9;

    void solve() throws IOException
    {
        cs = new char[rn][cn];
        int num = 0, mask = 0;
        fill(mapCoord, -1);

        for(int i = 0; i < 5; i++)
        {
            cs[i] = input.readLine().toCharArray();
            //print(i, cs[i]);
            for(int j = abs(2 - i), count = 0; count < 5 - abs(2 - i); count++, j += 2)
            {
                if(cs[i][j] == 'O') mask |= 1 << num;
                mapCoord[i * cn + j] = num;
                mapBit[num] = i * cn + j;
                //print(i, j, num);
                num++;
            }
        }
        //print(num, mask, mapBit);
        calc = new boolean[1 << 19];
        win = new boolean[1 << 19];
        if(can(mask)) out.println("Karlsson");
        else out.println("Lillebror");
    }

    int[] dy = {-1, 1, 2, 1, -1, -2};
    int[] dx = {-1, -1, 0, 1, 1, 0};
    boolean[] calc, win;

    boolean can(int mask)
    {
        //print(mask);
        if(mask == 0) return false;

        if(calc[mask]) return win[mask];
        calc[mask] = true;

        for(int i = 0; i < 19; i++)
        {
            if((mask >> i & 1) == 0) continue;
            int cur = mask ^ (1 << i);
            if(!can(cur)) return win[mask] = true;

            for(int k = 0; k < 6; k++)
            {
                cur = mask ^ (1 << i);

                int x = mapBit[i] / cn;
                int y = mapBit[i] % cn;

                while(x + dx[k] >= 0 && x + dx[k] < rn && y + dy[k] >= 0 && y + dy[k] < cs[x + dx[k]].length)
                {
                    x += dx[k];
                    y += dy[k];

                    int j = mapCoord[x * cn + y];
                    if(j == - 1 || (cur >> j & 1) == 0) break;

                    cur ^= 1 << j;
                    if(!can(cur)) return win[mask] = true;
                }
            }
        }
        return win[mask] = false;
    }

    String next() throws IOException
    {
        while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine());
        return st.nextToken();
    }

    int nextInt() throws IOException
    {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException
    {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException
    {
        return Double.parseDouble(next());
    }

    void print(Object... o)
    {
        System.out.println(deepToString(o));
    }

    void gcj(Object o)
    {
        String s = String.valueOf(o);
        out.println("Case #" + test + ": " + s);
        System.out.println("Case #" + test + ": " + s);
    }

    BufferedReader input;
    PrintWriter out;
    StringTokenizer st;
    int test;

    void init() throws IOException
    {
        if(TEST) input = new BufferedReader(new FileReader(file + ".in"));
        else input = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new BufferedOutputStream(System.out));
    }

    public static void main(String[] args) throws IOException
    {
        new Thread(null, new E(), "", 1 << 22).start();
    }

    public void run()
    {
        try
        {
            init();
            if(TEST)
            {
                int runs = nextInt();
                for(int i = 0; i < runs; i++) solve();
            }
            else solve();
            out.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }
}


/**
 * Created by IntelliJ IDEA.
 * User: aircube
 * Date: 28.02.11
 * Time: 20:43
 * To change this template use File | Settings | File Templates.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class TaskD {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;

    public static void main(String[] args) throws IOException {
        new TaskD().run();
    }

    void run() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        st = null;
        out = new PrintWriter(System.out);
        solve();
        br.close();
        out.close();
    }

    int a[][] ;
    int dm[][];
    void solve() throws IOException {
        a = new int[15][];
        a[0] = new int[] {1, 2, 3};
        a[1] = new int[] {17, 18, 19};
        a[2] = new int[] {8, 13, 17};
        a[3] = new int[] {3, 7, 12};
        a[4] = new int[] {1, 4, 8};
        a[5] = new int[] {12, 16, 19};
        a[6] = new int[] {7, 11, 15, 18};
        a[7] = new int[] {2, 5, 9, 13};
        a[8] = new int[] {2, 6, 11, 16};
        a[9] = new int[] {13, 14, 15, 16};
        a[10] = new int[] {4, 9, 14, 18};
        a[11] = new int[] {4, 5, 6, 7};
        a[12] = new int[] {8, 9, 10, 11, 12};
        a[13] = new int[] {1, 5, 10, 15, 19};
        a[14] = new int[] {3, 6, 10, 14, 17};

        dm = new int[1 << 19][2];
        for (int i = 0; i < dm.length; ++i)
            dm[i][0] = dm[i][1] = -1;

        for (int i = 0; i < a.length; ++i)
            for (int j = 0; j < a[i].length; ++j)
                a[i][j] -- ;

        int mask = 0;
        for (int t = 0; t < 19; ++t) {
            String str = nextToken();
            if (str.compareTo("O") == 0) mask |= (1 << t) ;;
        }
        int res = brute(mask, 0);
        out.print(res == 1 ? "Karlsson" : "Lillebror");

    }
    int brute(int mask, int turn) {
        if (mask == 0) {
            return turn;
        }
        if (dm[mask][turn] != -1) return dm[mask][turn];
        if (dm[mask][turn ^ 1] != -1) return dm[mask][turn ^ 1] ^ 1;

        for(int v = 0; v < a.length; ++v) {

            for (int i = 0; i < a[v].length; ++i) {
                for(int j = i; j < a[v].length; ++j) {
                    boolean ok = true;
                    for (int u = i; ok && u <= j; ++u) {
                        if ((mask & (1 << a[v][u])) == 0) ok = false;
                    }
                    if (ok) {
                        for (int I = i; I <= j; ++I)
                            for(int J = I; J <= j; ++J) {
                                int cm = 0;
                                for (int k = I; k <= J; ++k) {
                                    cm |= (1 << a[v][k]);
                                }
                                int d = brute(mask ^ cm, turn ^ 1);
                                if (turn == 0 && d == 1)  return dm[mask][turn] = 1;
                                if (turn == 1 && d == 0) return dm[mask][turn] = 0;
                            }
                    }
                }
            }
        }

        return dm[mask][turn] = turn;
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}


import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

public class P063E {

	Scanner sc = new Scanner(System.in);

	int INF = 1 << 28;
	double EPS = 1e-9;

	int m = 19;
	int n = 5;
	int now;

	void run() {
		for (int i = 0; i < m; i++) {
			now |= sc.next().charAt(0) == 'O' ? (1 << i) : 0;
		}
		solve();
	}

	int[][] map;
	int[] x, y;

	int[] win;

	void solve() {
		map = new int[][] { { 0, 1, 2, -1, -1 }, { 3, 4, 5, 6, -1 },
				{ 7, 8, 9, 10, 11 }, { -1, 12, 13, 14, 15 },
				{ -1, -1, 16, 17, 18 } };
		x = new int[m];
		y = new int[m];
		for (int j = 0; j < n; j++) {
			for (int i = 0; i < n; i++) {
				if (map[j][i] != -1) {
					x[map[j][i]] = i;
					y[map[j][i]] = j;
				}
			}
		}

		win = new int[1 << m];
		fill(win, -1);

		dfs(now);

		// debug(win[now]);
		println(new String[] { "Lillebror", "Karlsson" }[win[now]]);
	}

	int[] dx = { 1, 0, 1 };
	int[] dy = { 0, 1, 1 };

	int dfs(int state) {
		if (win[state] >= 0) {
			return win[state];
		}
		if (state == 0) {
			return win[state] = 0;
		}
		for (int j = 0; j < n; j++) {
			for (int i = 0; i < n; i++) {
				for (int k = 0; k < 3; k++) {
					for (int x = i, y = j, bit = 0; x < n && y < n
							&& map[y][x] != -1 && (state >> map[y][x] & 1) == 1; x += dx[k], y += dy[k]) {
						bit |= 1 << map[y][x];
						if (dfs(state ^ bit) == 0) {
							win[state] = 1;
							return win[state];
						}
					}
				}
			}
		}
		return win[state] = 0;
	}

	void println(String s) {
		System.out.println(s);
	}

	void print(String s) {
		System.out.print(s);
	}

	void debug(Object... os) {
		System.err.println(deepToString(os));
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.US);
		new P063E().run();
	}

}


import java.util.*;

public class E {

    int p2[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
        2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144};

    boolean bitSetted(int code, int bitNo) {
        return (code & (1 << bitNo)) != 0;
    }

    int clearBit(int code, int bitNo) {
        return code - p2[bitNo];
    }

    public static void main(String[] args) {
        new E().main();
    }
    int[][] gameLines = {
        // horizontal
        {0, 1, 2},
        {3, 4, 5, 6},
        {7, 8, 9, 10, 11},
        {12, 13, 14, 15},
        {16, 17, 18},
        // /-diag
        {7, 3, 0},
        {12, 8, 4, 1},
        {16, 13, 9, 5, 2},
        {17, 14, 10, 6},
        {18, 15, 11},
        // \-diag
        {2, 6, 11},
        {1, 5, 10, 15},
        {0, 4, 9, 14, 18},
        {3, 8, 13, 17},
        {7, 12, 16}
    };
    int sg_values[] = new int[530000];

    boolean canDoMove(int position, int gameLineNo, int left, int right) {
        for (int i = left; i <= right; i++) {
            if (!bitSetted(position,gameLines[gameLineNo][i])) {
                return false;
            }
        }
        return true;
    }

    int doMove(int position, int gameLineNo, int left, int right) {
        int res = position;
        for (int i = left; i <= right; i++) {
            res = clearBit(res, gameLines[gameLineNo][i]);
        }
        return res;
    }

    ArrayList<Integer> inOneMove(Integer position) {
        ArrayList<Integer> res = new ArrayList<Integer>();

        for (int i = 0; i < gameLines.length; i++) {
            for (int left = 0; left < gameLines[i].length; left++) {
                for (int right = left; right < gameLines[i].length; right++) {
                    if (canDoMove(position, i, left, right)) {
                        res.add(doMove(position, i, left, right));
                    }
                }
            }
        }
        return res;
    }

    // false, если позиция проигрышная
    // true,  если выигрышная
    int SG(Integer position) {
        if (sg_values[position] != 0) {
            return sg_values[position];
        }
        //System.out.println("Calculating SG for " + position);
        int res = -1;
        for (int x : inOneMove(position)) {
            if (SG(x) == -1) {
                res = 1;
                break;
            }
        }
        sg_values[position] = res;
        return res;
    }

    void main() {
        String spos = "";
        Scanner in = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            String line = in.nextLine();
            for (char c : line.toCharArray()) {
                if (c == '.' || c == 'O') {
                    spos += c;
                }
            }
        }
        int now = 0;
        for (int i = 0; i < spos.length(); i++) {
            if (spos.charAt(i) == 'O') {
                now += p2[i];
            }
        }
        sg_values[0] = -1;
        if (SG(now) == -1) {
            System.out.println("Lillebror");
        } else {
            System.out.println("Karlsson");
        }
    }
}


import java.io.*;
import java.util.*;

public class Igraskonfetami {
	static BufferedReader br = new BufferedReader(new InputStreamReader(
			System.in));

	static StringTokenizer in;

	static PrintWriter out = new PrintWriter(System.out);

	public static String nextToken() throws IOException {
		while (in == null || !in.hasMoreTokens()) {
			in = new StringTokenizer(br.readLine());
		}

		return in.nextToken();
	}

	public static int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public static double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	static int[][] pos = new int[][] { { 0, 0, 1, 0, 2, 0, 3, 0, 0 },
			{ 0, 4, 0, 5, 0, 6, 0, 7, 0 }, { 8, 0, 9, 0, 10, 0, 11, 0, 12 },
			{ 0, 13, 0, 14, 0, 15, 0, 16, 0 }, { 0, 0, 17, 0, 18, 0, 19, 0, 0 } };

	static int[][] sweet = new int[][] { { 0, 2 }, { 0, 4 }, { 0, 6 },
			{ 1, 1 }, { 1, 3 }, { 1, 5 }, { 1, 7 }, { 2, 0 }, { 2, 2 },
			{ 2, 4 }, { 2, 6 }, { 2, 8 }, { 3, 1 }, { 3, 3 }, { 3, 5 },
			{ 3, 7 }, { 4, 2 }, { 4, 4 }, { 4, 6 } };

	static boolean[] ans;

	public static int get(boolean[][] c) {
		int mask = 0;
		for (int i = 0; i < c.length; i++) {
			for (int j = 0; j < c[0].length; j++) {
				if (c[i][j])
					mask += (1 << (pos[i][j] - 1));
			}
		}

		return mask;
	}

	static boolean win(int mask) {
		boolean[][] state = new boolean[5][9];

		for (int i = 0; i < 19; i++) {
			if ((mask & (1 << i)) > 0)
				state[sweet[i][0]][sweet[i][1]] = true;
		}

		boolean win = false;
		for (int i = 0; i < 19; i++) {
			int x = sweet[i][0];
			int y = sweet[i][1];

			if (!state[x][y])
				continue;

			// left
			int m = mask;
			for (int j = y; j < 9 && state[x][j]; j += 2) {
				m -= (1 << (pos[x][j] - 1));
				win = win | !ans[m];
				if (win)
					break;
			}

			// leftdiag
			m = mask;
			for (int k = x, j = y; k < 5 && j >= 0 && state[k][j]; k++, j--) {
				m -= (1 << (pos[k][j] - 1));
				win = win | !ans[m];
				if (win)
					break;
			}

			// rightdiag
			m = mask;
			for (int k = x, j = y; k < 5 && j < 9 && state[k][j]; k++, j++) {
				m -= (1 << (pos[k][j] - 1));

				win = win | !ans[m];
				if (win)
					break;
			}

			if (win)
				break;
		}

		return win;
	}

	public static void main(String[] args) throws IOException {
		ans = new boolean[1 << 19];

		ans[0] = false;

		for (int i = 1; i < ans.length; i++) {
			if (i == 281){
				int k = 0;
				k++;
			}

			ans[i] = win(i);
		}

		boolean[][] state = new boolean[5][9];

		for (int i = 0; i < state.length; i++) {
			String S = br.readLine();

			for (int j = 0; j < S.length(); j++) {
				if (S.charAt(j) == 'O')
					state[i][j] = true;
			}
		}

		int mask = get(state);

		if (ans[mask])
			System.out.println("Karlsson");
		else
			System.out.println("Lillebror");

		out.close();
	}
}


/**
 * Created by IntelliJ IDEA.
 * User: aircube
 * Date: 28.02.11
 * Time: 20:43
 * To change this template use File | Settings | File Templates.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class TaskD {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;

    public static void main(String[] args) throws IOException {
        new TaskD().run();
    }

    void run() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        st = null;
        out = new PrintWriter(System.out);
        solve();
        br.close();
        out.close();
    }

    int a[][] ;
    int dm[];
    void solve() throws IOException {
        a = new int[15][];
        a[0] = new int[] {1, 2, 3};
        a[1] = new int[] {17, 18, 19};
        a[2] = new int[] {8, 13, 17};
        a[3] = new int[] {3, 7, 12};
        a[4] = new int[] {1, 4, 8};
        a[5] = new int[] {12, 16, 19};
        a[6] = new int[] {7, 11, 15, 18};
        a[7] = new int[] {2, 5, 9, 13};
        a[8] = new int[] {2, 6, 11, 16};
        a[9] = new int[] {13, 14, 15, 16};
        a[10] = new int[] {4, 9, 14, 18};
        a[11] = new int[] {4, 5, 6, 7};
        a[12] = new int[] {8, 9, 10, 11, 12};
        a[13] = new int[] {1, 5, 10, 15, 19};
        a[14] = new int[] {3, 6, 10, 14, 17};

        dm = new int[1 << 19];
        Arrays.fill(dm, -1);

        for (int i = 0; i < a.length; ++i)
            for (int j = 0; j < a[i].length; ++j)
                a[i][j] -- ;

        int mask = 0;
        for (int t = 0; t < 19; ++t) {
            String str = nextToken();
            if (str.compareTo("O") == 0) mask |= (1 << t) ;;
        }
        int res = brute(mask);
        out.print(res == 1 ? "Karlsson" : "Lillebror");

    }
    int brute(int mask) {
        if (mask == 0) {
            return 0;
        }
        if (dm[mask] != -1) return dm[mask];

        for(int v = 0; v < a.length; ++v) {

            for (int i = 0; i < a[v].length; ++i) {
                for(int j = i; j < a[v].length; ++j) {
                    boolean ok = true;
                    for (int u = i; ok && u <= j; ++u) {
                        if ((mask & (1 << a[v][u])) == 0) ok = false;
                    }
                    if (ok) {
                        for (int I = i; I <= j; ++I)
                            for(int J = I; J <= j; ++J) {
                                int cm = 0;
                                for (int k = I; k <= J; ++k) {
                                    cm |= (1 << a[v][k]);
                                }
                                int d = brute(mask ^ cm);
                                if (d == 0) return dm[mask] = 1;
                            }
                    }
                }
            }
        }
        return dm[mask] = 0;
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}


import java.io.*;
import java.util.*;

public class E {
	int[]len=new int[]{3,4,5,4,3};
	int[][]getGet;
	boolean[]cached;
	boolean[]answer;
	void solve() throws Exception {
		cached=new boolean[1<<19];
		answer=new boolean[1<<19];
		getGet=new int[5][5];
		for(int r=0;r<5;r++)
			for(int c=0;c<5;c++)
				getGet[r][c]=get(r,c);
		int set=0;
		for(int r=0;r<5;r++)
		{
			String s=nextLine();
			s=s.replaceAll(" ", "");
			for(int j=0;j<len[r];j++)
				if((s.charAt(j))=='O')
					set|=1<<get(r,j);
		}
		if(rec(set))
			System.out.println("Karlsson");
		else
			System.out.println("Lillebror");
	}
	boolean rec(int set)
	{
		if(cached[set])
			return answer[set];
		boolean res=false;
		for(int r=0;r<5;r++)
			for(int c=0;c<len[r];c++)
			{
				int num=get(r,c);
				if((set&(1<<num))>0)
				{
					int cr=r;
					int cc=c;
					int cset=set;
					while(cr>=0 && cr<5 && cc>=0 && cc<len[cr] && (cset&(1<<getGet[cr][cc]))>0)
					{
						cset^=(1<<getGet[cr][cc]);
						res|=!rec(cset);
						cc++;
					}
					cr=r;
					cc=c;
					cset=set;
					while(cr>=0 && cr<5 && cc>=0 && cc<len[cr] && (cset&(1<<getGet[cr][cc]))>0)
					{
						cset^=(1<<getGet[cr][cc]);
						res|=!rec(cset);
						if(cr==2 || cr==3)
							cc--;
						cr++;
					}
					cr=r;
					cc=c;
					cset=set;
					while(cr>=0 && cr<5 && cc>=0 && cc<len[cr] && (cset&(1<<getGet[cr][cc]))>0)
					{
						cset^=(1<<getGet[cr][cc]);
						res|=!rec(cset);
						if(cr==0 || cr==1)
							cc++;
						cr++;
					}

				}
			}
		cached[set]=true;
		answer[set]=res;
		return res;

	}
	int get(int r,int c)
	{
		int res=0;
		for(int i=0;i<r;i++)
			res+=len[i];
		return res+c;
	}

	BufferedReader reader;
	PrintWriter writer;
	StringTokenizer stk;

	void run() throws Exception {
		reader = new BufferedReader(new InputStreamReader(System.in));
		stk = null;
		writer = new PrintWriter(System.out);
		solve();
		reader.close();
		writer.close();
	}

	int nextInt() throws Exception {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws Exception {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws Exception {
		return Double.parseDouble(nextToken());
	}

	String nextString() throws Exception {
		return nextToken();
	}

	String nextLine() throws Exception {
		return reader.readLine();
	}

	String nextToken() throws Exception {
		if (stk == null || !stk.hasMoreTokens()) {
			stk = new StringTokenizer(nextLine());
			return nextToken();
		}
		return stk.nextToken();
	}

	public static void main(String[] a) throws Exception {
		new E().run();
	}

}


import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

// Sweets Game
// 2012/11/03
public class P063E{
	Scanner sc=new Scanner(System.in);

	int m=19, n=5;
	int now;

	void run(){
		for(int i=0; i<m; i++){
			now|=sc.next().charAt(0)=='O'?(1<<i):0;
		}
		solve();
	}

	int[][] map;
	int[] win;

	void solve(){
		map=new int[][]{{0, 1, 2, -1, -1}, {3, 4, 5, 6, -1}, {7, 8, 9, 10, 11},
				{-1, 12, 13, 14, 15}, {-1, -1, 16, 17, 18}};
		win=new int[1<<m];
		fill(win, -1);
		println(new String[]{"Lillebror", "Karlsson"}[dfs(now)]);
	}

	int[] dx={1, 0, 1};
	int[] dy={0, 1, 1};

	int dfs(int state){
		if(win[state]>=0){
			return win[state];
		}
		if(state==0){
			return win[state]=0;
		}
		for(int j=0; j<n; j++){
			for(int i=0; i<n; i++){
				for(int k=0; k<3; k++){
					for(int x=i, y=j, bit=0; x<n&&y<n&&map[y][x]!=-1
							&&(state>>map[y][x]&1)==1; x+=dx[k], y+=dy[k]){
						bit|=1<<map[y][x];
						if(dfs(state^bit)==0){
							win[state]=1;
							return win[state];
						}
					}
				}
			}
		}
		return win[state]=0;
	}

	void println(String s){
		System.out.println(s);
	}

	void print(String s){
		System.out.print(s);
	}

	public static void main(String[] args){
		Locale.setDefault(Locale.US);
		new P063E().run();
	}
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Solution implements Runnable {

    public static void main(String[] args) {
        (new Thread(new Solution())).start();
    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st;

    String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String r = in.readLine();
            if (r == null) return null;
            st = new StringTokenizer(r);
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    int[][] a = new int[5][5];

    void uncode(int q) {
        int t = 24;
        for (int i = 0; i < 19; i++) {
            while (a[t / 5][t % 5] == -1) t--;
            a[t / 5][t % 5] = q % 2;
            t--; q /= 2;
        }
    }

    int code() {
        int t = 0;
        int res = 0;
        for (int i = 0; i < 19; i++) {
            while (a[t / 5][t % 5] == -1) t++;
            res *= 2;
            res += a[t / 5][t % 5];
            t++;
        }
        return res;
    }

    void clone(int[][] a, int[][] b) {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                b[i][j] = a[i][j];
            }
        }
    }

    int[] ans = new int[1000000];

    boolean valid(int q) {
        if (q == 0) return false;
        while (q % 2 == 0) q /= 2;
        return (q & (q - 1)) == 0;
    }

    boolean rek(int q) {
        if (ans[q] != -1) return ans[q] == 1;
        boolean f = false;
        for (int i = 0; i < tl; i++) {
            if ((q | turn[i]) == q) {
                f |= !rek(q ^ turn[i]);
            }
        }
        ans[q] = f? 1 : 0;
        return f;
    }

    int[] turn = new int[25 * 25 * 25];
    int tl = 0;

    void solve() throws Exception {
        Arrays.fill(ans, -1);
        a[0][3] = a[0][4] = a[1][4] = -1;
        a[4][0] = a[4][1] = a[3][0] = -1;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                for (int k = 0; i + k < 5 && a[i + k][j] != -1; k++) {
                    int[][] b = new int[5][5];
                    clone(a, b);
                    for (int kk = 0; kk <= k; kk++) a[i + kk][j] = 1;
                    turn[tl++] = code();
                    clone(b, a);
                }
                for (int k = 0; j + k < 5 && a[i][j + k] != -1; k++) {
                    int[][] b = new int[5][5];
                    clone(a, b);
                    for (int kk = 0; kk <= k; kk++) a[i][j + kk] = 1;
                    turn[tl++] = code();
                    clone(b, a);
                }
                for (int k = 0; i + k < 5 && j + k < 5 && a[i + k][j + k] != -1; k++) {
                    int[][] b = new int[5][5];
                    clone(a, b);
                    for (int kk = 0; kk <= k; kk++) a[i + kk][j + kk] = 1;
                    turn[tl++] = code();
                    clone(b, a);
                }
            }
        }
        Arrays.sort(turn, 0, tl);
        int ii = 1;
        for (int i = 1; i < tl; i++) {
            if (turn[i] != turn[i - 1]) turn[ii++] = turn[i];
        }
        tl = ii;
        int res = 0;
        for (int i = 0; i < 19; i++) {
            res *= 2;
            if (nextToken().charAt(0) != '.') res++;
        }
        if (rek(res)) out.println("Karlsson"); else out.println("Lillebror");
    }

    public void run() {
        Locale.setDefault(Locale.UK);
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
            solve();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        } finally {
            out.flush();
        }
    }

}


import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class Main{
    public static void main(String[] args) throws Exception{
        new Main().run();
    }

    int[] dp = new int[1<<19];
    final int[] dh = new int[]{0, 3, 7, 12, 16};
    final int[] dw = new int[]{3, 4, 5, 4, 3};
    boolean firstWin(int mask){
        if(dp[mask] != 0)return dp[mask] == 1;
        boolean ok = true;
        loop:
        for(int i = 0; i < 5; i++){
            for(int j = 0; j < dw[i]; j++){
                int k = dh[i] + j;
                if(((mask >> k) & 1) == 0)continue;
                int v = mask;
                int cy = i, cx = j;
                for(;;){
                    int ind = dh[cy] + cx;
                    v -= 1 << ind;
                    boolean ret = firstWin(v);
                    if(!ret){
                        ok = false;
                        break loop;
                    }
                    cy++;
                    if(cy > 2)cx--;
                    if(cy >= 5 || cx < 0 || cx >= dw[cy] || ((v >> (dh[cy] + cx)) & 1) == 0)break;
                }

                cy = i; cx = j;
                v = mask - (1 << (dh[cy] + cx));
                for(;;){
                    cy++;
                    if(cy < 3)cx++;
                    if(cy >= 5 || cx < 0 || cx >= dw[cy] || ((v >> (dh[cy] + cx)) & 1) == 0)break;
                    int ind = dh[cy] + cx;
                    v -= 1 << ind;
                    boolean ret = firstWin(v);
                    if(!ret){
                        ok = false;
                        break loop;
                    }
                }

                cy = i; cx = j;
                v = mask - (1 << (dh[cy] + cx));
                for(;;){
                    cx++;
                    if(cy >= 5 || cx < 0 || cx >= dw[cy] || ((v >> (dh[cy] + cx)) & 1) == 0)break;
                    int ind = dh[cy] + cx;
                    v -= 1 << ind;
                    boolean ret = firstWin(v);
                    if(!ret){
                        ok = false;
                        break loop;
                    }
                }

            }
        }
        if(ok){
            dp[mask] = -1;
            return false;
        }else{
            dp[mask] = 1;
            return true;
        }
    }

    int toi(boolean[] u){
        int ret = 0;
        for(int i = 0; i < u.length; i++)if(u[i])ret += 1 << i;
        return ret;
    }

    void run() throws Exception{
        Scanner sc = new Scanner(System.in);
        //BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        // only sc.readLine() is available
        boolean[] u = new boolean[19];
        for(int i = 0; i < 19; i++)u[i] = sc.next().equals("O");
        boolean ret = firstWin(toi(u));
        System.out.println(ret ? "Karlsson" : "Lillebror");
    }
}


import java.util.*;

public class SweetsGame {
	static int[] memo;
	static int[] sj = new int[]{0,0,0,1,2};
	static int[] dj = new int[]{3,4,5,5,5};
	static int[] dx = new int[]{0,1,1};
	static int[] dy = new int[]{1,1,0};
	public static void main(String[] args){
		Scanner reader = new Scanner(System.in);
		int[][] d = new int[5][5];
		for(int i = 0; i < 5; i++){
			for(int j = sj[i]; j < dj[i]; j++){
				d[i][j] = reader.next().equals(".")?0:1;
			}
		}

		memo = new int[(1<<19)];
		System.out.println(f(e(d))>0?"Karlsson":"Lillebror");
	}

	public static void print(int n, int[][] d){
		System.out.println("At " + n + ", processing " + e(d));
		for(int i = 0; i < 5; i++)
			System.out.println(Arrays.toString(d[i]));
	}

	public static int f(int n){
		//System.out.println(Integer.toString(n,2));
		if(memo[n] == 0){
			if(n == 0){
				memo[n] = -1;
			}else{
				int[][] d = d(n);
				int canwin = -1;
//				print(n, d);
				for(int i = 0; i < 5 && canwin < 0; i++){
					for(int j = sj[i]; j < dj[i] && canwin < 0; j++){
						//Proceed in each direction
						for(int k = 0; k < 3 && canwin < 0; k++){
							int L = 0;
							while(nx(i,k,L) >= 0 && nx(i,k,L) < 5 && ny(j,k,L) >= sj[nx(i,k,L)] && ny(j,k,L) < dj[nx(i,k,L)] && d[nx(i,k,L)][ny(j,k,L)] > 0 && canwin < 0){
								//System.out.println("Killing " + i + " " + j + " " + L + " from " + " " + n);
								d[nx(i,k,L)][ny(j,k,L)] = 0;
//								print(n, d);
								canwin = Math.max(canwin, -f(e(d)));
								L++;
							}

							//Reset bitmask
							for(int z = 0; z < L; z++)
								d[nx(i,k,z)][ny(j,k,z)] = 1;
						}
					}
				}

				memo[n] = canwin;
			}
		}
//		System.out.println(n + " " + memo[n]);
		return memo[n];
	}

	public static int nx(int i, int k, int L){
		return i + dx[k]*L;
	}

	public static int ny(int j, int k, int L){
		return j + dy[k]*L;
	}

	public static int[][] d(int e){
		int[][] d = new int[5][5];

		for(int i = 4; i >= 0; i--){
			for(int j = dj[i]-1; j >= sj[i]; j--){
				d[i][j] = (e&1);
				e >>= 1;
			}
		}

		return d;
	}

	public static int e(int[][] d){
		int e = 0;
		for(int i = 0; i < 5; i++){
			for(int j = sj[i]; j < dj[i]; j++){
				e <<= 1;
				e |= d[i][j];
			}
		}
		return e;
	}
}


/**
 * Created by IntelliJ IDEA.
 * User: aircube
 * Date: 28.02.11
 * Time: 20:43
 * To change this template use File | Settings | File Templates.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class TaskD {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;

    public static void main(String[] args) throws IOException {
        new TaskD().run();
    }

    void run() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        st = null;
        out = new PrintWriter(System.out);
        solve();
        br.close();
        out.close();
    }

    int a[][] ;
    int dm[][];
    void solve() throws IOException {
        a = new int[15][];
        a[0] = new int[] {1, 2, 3};
        a[1] = new int[] {4, 5, 6, 7};
        a[2] = new int[] {8, 9, 10, 11, 12};
        a[3] = new int[] {13, 14, 15, 16};
        a[4] = new int[] {17, 18, 19};
        a[5] = new int[] {8, 13, 17};
        a[6] = new int[] {4, 9, 14, 18};
        a[7] = new int[] {1, 5, 10, 15, 19};
        a[8] = new int[] {2, 6, 11, 16};
        a[9] = new int[] {3, 7, 12};
        a[10] = new int[] {1, 4, 8};
        a[11] = new int[] {2, 5, 9, 13};
        a[12] = new int[] {3, 6, 10, 14, 17};
        a[13] = new int[] {7, 11, 15, 18};
        a[14] = new int[] {12, 16, 19};

        dm = new int[1 << 19][2];
        for (int i = 0; i < dm.length; ++i)
            dm[i][0] = dm[i][1] = -1;

        for (int i = 0; i < a.length; ++i)
            for (int j = 0; j < a[i].length; ++j)
                a[i][j] -- ;

        int mask = 0;
        for (int t = 0; t < 19; ++t) {
            String str = nextToken();
            if (str.compareTo("O") == 0) mask |= (1 << t) ;;
        }
        int res = brute(mask, 0);
        out.print(res == 1 ? "Karlsson" : "Lillebror");

    }
    int brute(int mask, int turn) {
        if (mask == 0) {
            return turn;
        }
        if (dm[mask][turn] != -1) return dm[mask][turn];

        for(int v = 0; v < a.length; ++v) {

            for (int i = 0; i < a[v].length; ++i) {
                for(int j = i; j < a[v].length; ++j) {
                    boolean ok = true;
                    for (int u = i; ok && u <= j; ++u) {
                        if ((mask & (1 << a[v][u])) == 0) ok = false;
                    }
                    if (ok) {
                        for (int I = i; I <= j; ++I)
                            for(int J = I; J <= j; ++J) {
                                int cm = 0;
                                for (int k = I; k <= J; ++k) {
                                    cm |= (1 << a[v][k]);
                                }
                                int d = brute(mask ^ cm, turn ^ 1);
                                if (turn == 0 && d == 1)  return dm[mask][turn] = 1;
                                if (turn == 1 && d == 0) return dm[mask][turn] = 0;
                            }
                    }
                }
            }
        }

        return dm[mask][turn] = turn;
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}


/**
 * Created by IntelliJ IDEA.
 * User: Taras_Brzezinsky
 * Date: 8/24/11
 * Time: 8:54 PM
 * To change this template use File | Settings | File Templates.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.io.IOException;
import java.util.Arrays;

public class TaskD extends Thread {

    public TaskD() {
        this.input = new BufferedReader(new InputStreamReader(System.in));
        this.output = new PrintWriter(System.out);
        this.setPriority(Thread.MAX_PRIORITY);
    }

    static class Matrix {
        long[][] grid;
        long[][] helper;

        public Matrix(int size, boolean E) {
            this.grid = new long[size][size];
            this.helper = new long[size][size];
            if (E) {
                for (int i = 0; i < grid.length; ++i) {
                    grid[i][i] = 1;
                }
            }
        }

        public Matrix pow(long power) {
            Matrix result = new Matrix(grid.length, true);
            Matrix stat = new Matrix(grid.length, false);
            for (int i = 0; i < grid.length; ++i) {
                stat.grid[i] = this.grid[i].clone();
            }
            while (power != 0) {
                if ((power & 1) == 1) {
                    result.multiply(stat);
                    --power;
                } else {
                    stat.multiply(stat);
                    power >>= 1;
                }
            }
            return result;
        }

        private void multiply(Matrix another) {
            for (long[] current : helper) {
                Arrays.fill(current, 0);
            }
            for (int i = 0; i < grid.length; ++i) {
                for (int j = 0; j < grid.length; ++j) {
                    for (int k = 0; k < grid.length; ++k) {
                        helper[i][j] += (grid[i][k] * another.grid[k][j]) % MODULO;
                    }
                    helper[i][j] %= MODULO;
                }
            }
            for (int i = 0; i < grid.length; ++i) {
                System.arraycopy(helper[i], 0, grid[i], 0, grid.length);
            }
        }
    }

    static class Rule {
        char letter;
        char value;

        public Rule(char letter, char value) {
            this.letter = letter;
            this.value = value;
        }
    }

    private void generate(int index) {
        if (index == initial.length) {
            states[SIZE++] = initial.clone();
        } else {
            for (char value = 0; value < rules[index].value; ++value) {
                initial[index] = value;
                generate(index + 1);
            }
        }
    }

    private boolean isFinal(int index) {
        for (char letter = 'A'; letter <= 'Z'; ++letter) {
            boolean was = false, isGood = false;
            for (int i = 0; i < rules.length; ++i) {
                if (rules[i].letter == letter) {
                    was = true;
                    isGood |= states[index][i] == 0;
                }
            }
            if (was && !isGood) {
                return false;
            }
        }
        return true;
    }

    private void solve() throws Throwable {
        long n = nextLong();
        int c = nextInt();
        int s = c;
        c = 0;
        rules = new Rule[s];
        int total = 1;
        for (int i = 0; i < s; ++ i) {
            rules[c] = new Rule(nextToken().charAt(0), (char) (nextInt()));
            boolean was = false;
            for (int q = 0; q < c; ++q) {
                if (rules[q].letter == rules[c].letter && rules[q].value == rules[c].value) {
                    was = true;
                    break;
                }
            }
            if (!was){
                total *= rules[c++].value;
            }
        }
        rules = Arrays.copyOf(rules, c);
        initial = new char[c];
        states = new char[total][];
        generate(0);
        Matrix operator = new Matrix(total, false);
        for (int from = 0; from < total; ++from) {
            for (char letter = 'A'; letter <= 'Z'; ++letter) {
                boolean isPresent = false;
                char []helper = states[from].clone();
                for (int i = 0; i < c; ++i) {
                    if (rules[i].letter == letter) {
                        isPresent = true;
                        helper[i] = (char)((helper[i] + 1) % rules[i].value);
                    }
                }
                if (isPresent) {
                    for (int to = 0; to < states.length; ++to) {
                        boolean ok = true;
                        for (int index = 0; index < c; ++index) {
                            ok &= states[to][index] == helper[index];
                        }
                        if (ok) {
                            operator.grid[from][to]++;
                            break;
                        }
                    }
                }
            }
        }

        Matrix finish = operator.pow(n);
        long result = 0;
        for (int current = 0; current < total; ++current) {
            if (isFinal(current)) {
                result = (result + finish.grid[0][current]) % MODULO;
            }
        }
        output.println(result);
    }

    public void run() {
        try {
            solve();
        } catch (Throwable e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            System.exit(666);
        } finally {
            output.flush();
            output.close();
        }
    }

    public static void main(String[] args) {
        new TaskD().start();
    }

    private String nextToken() throws IOException {
        while (tokens == null || !tokens.hasMoreTokens()) {
            tokens = new StringTokenizer(input.readLine());
        }
        return tokens.nextToken();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    private long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    private int SIZE = 0;
    private static final int MODULO = 12345;
    private char[][] states;
    private char[] initial;
    private Rule[] rules;
    private BufferedReader input;
    private PrintWriter output;
    private StringTokenizer tokens = null;
}


import java.io.*;
import java.util.*;

public class cfRound83_D {

    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;

    final int MOD = 12345;

    int[][] mul(int[][] a, int[][] b) {
        int[][] res = new int[sz][sz];
        for (int i = 0; i < sz; i++)
            for (int j = 0; j < sz; j++)
                for (int k = 0; k < sz; k++) {
                    res[i][j] += a[i][k] * b[k][j];
                    res[i][j] %= MOD;
                }

        return res;
    }

    int[][] power(int[][] a, long b) {
        if (b == 0) {
            int[][] res = new int[sz][sz];
            for (int i = 0; i < sz; i++)
                res[i][i] = 1;
            return res;
        }

        if ((b & 1) == 1)
            return mul(a, power(a, b - 1));

        int[][] tmp = power(a, b >> 1);
            return mul(tmp, tmp);

    }

    int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }

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

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

    int sz;

    void solve() throws IOException {
        long n = nextLong();

        ArrayList<Integer>[] list = new ArrayList[26];
        for (int i = 0; i < 26; i++)
            list[i] = new ArrayList<Integer>();

        int c = nextInt();

        for (int i = 0; i < c; i++) {
            String s = nextToken();
            int x = nextInt();
            list[s.charAt(0) - 'A'].add(x);
        }

        ArrayList<Integer> can = new ArrayList<Integer>();
        ArrayList<Integer> mult = new ArrayList<Integer>();
        ArrayList<Integer> lcm = new ArrayList<Integer>();

        sz = 1;

        for (int i = 0; i < 26; i++)
            if (!list[i].isEmpty()) {
                int curLcm = 0;
                for (int x : list[i])
                    curLcm = lcm(curLcm, x);
                can.add(i);
                mult.add(sz);
                sz *= curLcm;
                lcm.add(curLcm);
            }

        mult.add(sz);

//      System.err.println(can);
//      System.err.println(mult);
//      System.err.println(lcm);

        int[][] matr = new int[sz][sz];

        for (int i = 0; i < sz; i++) {
            for (int j = 0; j < can.size(); j++) {
                int oldRem = i % mult.get(j + 1) / mult.get(j);
                int newRem = (oldRem + 1) % lcm.get(j);
                matr[i - oldRem * mult.get(j) + newRem * mult.get(j)][i]++;
            }
        }

        matr = power(matr, n);

        int ans = 0;

        for (int i = 0; i < sz; i++) {
            boolean goodTotalRem = true;
            for (int j = 0; j < can.size(); j++) {
                int curRem = i % mult.get(j + 1) / mult.get(j);
                boolean goodRem = false;
                for (int good : list[can.get(j)])
                    if (curRem % good == 0) {
                        goodRem = true;
                        break;
                    }
                if (!goodRem) {
                    goodTotalRem = false;
                    break;
                }
            }
            if (goodTotalRem) {
                ans += matr[i][0];
                if (ans >= MOD)
                    ans -= MOD;
            }
        }

        out.println(ans);
    }

    void inp() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new cfRound83_D().inp();
    }

    String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return "0";
            }
        }
        return st.nextToken();
    }

    String nextString() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return "0";
            }
        }
        return st.nextToken("\n");
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
}


/**
 * Created by IntelliJ IDEA.
 * User: Taras_Brzezinsky
 * Date: 8/24/11
 * Time: 8:54 PM
 * To change this template use File | Settings | File Templates.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.util.Arrays;

public class TaskD extends Thread {

    public TaskD() {
        this.input = new BufferedReader(new InputStreamReader(System.in));
        this.output = new PrintWriter(System.out);
        this.setPriority(Thread.MAX_PRIORITY);
    }

    private int getIndex(int[][] data, int[] values) {
        for (int i = 0; i < data.length; ++i) {
            if (Arrays.equals(data[i], values)) {
                return i;
            }
        }
        throw new IllegalArgumentException();
    }

    private void generate(int index) {
        if (index == helper.length) {
            states[SIZE++] = helper.clone();
        } else {
            for (int value = 0; value < v[index]; ++value) {
                helper[index] = value;
                generate(index + 1);
            }
        }
    }

    private int[][] pow(int[][] src, long power) {
        int[][] result = new int[src.length][src.length];
        for (int i = 0; i < result.length; ++i) {
            result[i][i] = 1;
        }
        for (; power != 0; power >>= 1) {
            if ((power & 1) == 1) {
                result = multiply(result, src);
            }
            src = multiply(src, src);
        }
        return result;
    }

    private int[][] multiply(int[][] left, int[][] right) {
        int[][] result = new int[left.length][left.length];
        for (int i = 0; i < left.length; ++i) {
            for (int j = 0; j < left.length; ++j) {
                for (int k = 0; k < left.length; ++k) {
                    result[i][j] += (left[i][k] * right[k][j]) % MODULO;
                }
                result[i][j] %= MODULO;
            }
        }
        return result;
    }

    private void solve() throws Throwable {
        long n = nextLong();
        int c = nextInt();
        l = new int[c];
        v = new int[c];
        int total = 1;
        for (int i = 0; i < c; ++i) {
            l[i] = nextToken().charAt(0) - 'A';
            used[l[i]] = true;
            total *= (v[i] = nextInt());
        }
        states = new int[total][];
        helper = new int[c];
        generate(0);
        int[][] grid = new int[total][total];
        for (int current = 0; current < total; ++current) {
            for (int i = 0; i < AlPHABET_SIZE; ++i) {
                helper = states[current].clone();
                if (used[i]) {
                    for (int ind = 0; ind < c; ++ind) {
                        if (l[ind] == i) {
                            helper[ind] = (helper[ind] + 1) % v[ind];
                        }
                    }
                    grid[current][getIndex(states, helper)]++;
                }
            }
        }
        int[][] result = pow(grid, n);
        int answer = 0;
        for (int currentState = 0; currentState < total; ++currentState) {
            Arrays.fill(good, false);
            for (int i = 0; i < c; ++i) {
                good[l[i]] |= states[currentState][i] == 0;
            }
            boolean can = true;
            for (int i = 0; i < AlPHABET_SIZE; ++i) {
                if (used[i] && !good[i]) {
                    can = false;
                    break;
                }
            }
            if (can) {
                answer = (answer + result[0][currentState]) % MODULO;
            }
        }
        output.println(answer);
    }

    public void run() {
        try {
            solve();
        } catch (Throwable e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            System.exit(666);
        } finally {
            output.flush();
            output.close();
        }
    }

    public static void main(String[] args) {
        new TaskD().start();
    }

    private String nextToken() throws IOException {
        while (tokens == null || !tokens.hasMoreTokens()) {
            tokens = new StringTokenizer(input.readLine());
        }
        return tokens.nextToken();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    private long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    private static int[][] states;
    static private int[] helper, l, v;
    private static int SIZE = 0;
    private static final int MODULO = 12345, AlPHABET_SIZE = 26;
    static boolean[] used = new boolean[AlPHABET_SIZE], good = new boolean[AlPHABET_SIZE];
    private BufferedReader input;
    private PrintWriter output;
    private StringTokenizer tokens = null;
}


import java.io.*;
import java.util.*;

public class cfRound83_D {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	final int MOD = 12345;

	int[][] mul(int[][] a, int[][] b) {
		int[][] res = new int[sz][sz];
		for (int i = 0; i < sz; i++)
			for (int j = 0; j < sz; j++)
				for (int k = 0; k < sz; k++) {
					res[i][j] += a[i][k] * b[k][j];
					res[i][j] %= MOD;
				}

		return res;
	}

	int[][] power(int[][] a, long b) {
		if (b == 0) {
			int[][] res = new int[sz][sz];
			for (int i = 0; i < sz; i++)
				res[i][i] = 1;
			return res;
		}

		if ((b & 1) == 1)
			return mul(a, power(a, b - 1));

		int[][] tmp = power(a, b >> 1);
			return mul(tmp, tmp);

	}

	int gcd(int a, int b) {
		return b == 0 ? a : gcd(b, a % b);
	}

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

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

	int sz;

	void solve() throws IOException {
		long n = nextLong();

		ArrayList<Integer>[] list = new ArrayList[26];
		for (int i = 0; i < 26; i++)
			list[i] = new ArrayList<Integer>();

		int c = nextInt();

		for (int i = 0; i < c; i++) {
			String s = nextToken();
			int x = nextInt();
			list[s.charAt(0) - 'A'].add(x);
		}

		ArrayList<Integer> can = new ArrayList<Integer>();
		ArrayList<Integer> mult = new ArrayList<Integer>();
		ArrayList<Integer> lcm = new ArrayList<Integer>();

		sz = 1;

		for (int i = 0; i < 26; i++)
			if (!list[i].isEmpty()) {
				int curLcm = 0;
				for (int x : list[i])
					curLcm = lcm(curLcm, x);
				can.add(i);
				mult.add(sz);
				sz *= curLcm;
				lcm.add(curLcm);
			}

		mult.add(sz);

//		System.err.println(can);
//		System.err.println(mult);
//		System.err.println(lcm);

		int[][] matr = new int[sz][sz];

		for (int i = 0; i < sz; i++) {
			for (int j = 0; j < can.size(); j++) {
				int oldRem = i % mult.get(j + 1) / mult.get(j);
				int newRem = (oldRem + 1) % lcm.get(j);
				matr[i - oldRem * mult.get(j) + newRem * mult.get(j)][i]++;
			}
		}

		matr = power(matr, n);

		int ans = 0;

		for (int i = 0; i < sz; i++) {
			boolean goodTotalRem = true;
			for (int j = 0; j < can.size(); j++) {
				int curRem = i % mult.get(j + 1) / mult.get(j);
				boolean goodRem = false;
				for (int good : list[can.get(j)])
					if (curRem % good == 0) {
						goodRem = true;
						break;
					}
				if (!goodRem) {
					goodTotalRem = false;
					break;
				}
			}
			if (goodTotalRem) {
				ans += matr[0][i];
				if (ans >= MOD)
					ans -= MOD;
			}
		}

		out.println(ans);
	}

	void inp() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new cfRound83_D().inp();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return "0";
			}
		}
		return st.nextToken();
	}

	String nextString() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return "0";
			}
		}
		return st.nextToken("\n");
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}


import java.awt.Point;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.io.*;
import java.math.BigInteger;
import static java.math.BigInteger.*;
import java.util.*;

public class D{

    int mod=12345;
    void solve()throws Exception
    {
        int ones=0;
        HashMap<Integer,ArrayList<Integer>>map=new HashMap<Integer,ArrayList<Integer>>();
        long n=nextLong();
        int c=nextInt();
        int p=1;
        for(int i=0;i<c;i++)
        {
            int t=nextString().charAt(0)-'A';
            int m=nextInt();
            if(!map.containsKey(t))
                map.put(t,new ArrayList<Integer>());
            map.get(t).add(m);
        }
        int[][]ar=new int[map.size()][];
        int[]l=new int[ar.length];

        int ind=0;
        for(Integer x: map.keySet())
            {
                ArrayList<Integer>t=map.get(x);
                ar[ind]=new int[t.size()];
                int ind2=0;
                l[ind]=1;
                for(int i=0;i<t.size();i++)
                {
                    ar[ind][ind2]=t.get(i);
                    l[ind]=getLcm(l[ind],ar[ind][ind2]);
                    ind2++;
                }
                p*=l[ind];
                ind++;
            }
        long[][]mat=new long[p][p];
        for(int i=0;i<p;i++)
        {
            int[]state=new int[ar.length];
            int cur=i;
            for(int j=0;j<state.length;j++)
            {
                state[j]=cur%l[j];
                cur/=l[j];
            }
            for(int j=0;j<state.length;j++)
            {
                int[]ns=state.clone();
                ns[j]=(ns[j]+1)%l[j];
                cur=0;
                for(int k=state.length-1;k>=0;k--)
                {
                    cur=cur*l[k]+ns[k];
                }
                mat[i][cur]++;
            }

        }
        long[][]res=pow(mat,n);
        long sum=0;
        for(int i=0;i<p;i++)
        {
            int[]state=new int[ar.length];
            int cur=i;
            boolean ok=true;
            for(int j=0;j<state.length;j++)
            {
                state[j]=cur%l[j];
                cur/=l[j];
                boolean  any=false;
                for(int x: ar[j])
                    if(state[j]%x==0)
                        any=true;
                if(!any)
                    ok=false;

            }
            if(ok)
                sum=(sum+res[0][i])%mod;

        }
        System.out.println(sum);

    }

    private int getLcm(int a, int b) {
        return a/BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue()*b;
    }

    private long[][] pow(long[][] mat, long p) {
        if(p==0)
        {
            long[][]res=new long[mat.length][mat.length];
            for(int i=0;i<res.length;i++)
                res[i][i]=1;
            return res;
        }
        long[][]res=pow(mat,p/2);
        res=mul(res,res);
        if(p%2==1)
            res=mul(res,mat);
        return res;
    }

    private long[][] mul(long[][] a, long[][] b) {
        long[][]c=new long[a.length][a.length];
        for(int i=0;i<c.length;i++)
            for(int j=0;j<c.length;j++)
                for(int k=0;k<c.length;k++)
                    c[i][j]=(a[i][k]*b[k][j]+c[i][j])%mod;
        return c;
    }

    ////////////
    BufferedReader reader;
    PrintWriter writer;
    StringTokenizer stk;
    void run()throws Exception
    {
        reader=new BufferedReader(new InputStreamReader(System.in));
        stk=null;
        writer=new PrintWriter(System.out);
        solve();
        reader.close();
        writer.close();
    }
    int nextInt()throws Exception
    {
        return Integer.parseInt(nextToken());
    }

    long nextLong()throws Exception
    {
        return Long.parseLong(nextToken());

    }
    double nextDouble()throws Exception
    {
        return Double.parseDouble(nextToken());

    }

    String nextString()throws Exception
    {
        return nextToken();
    }
    String nextLine()throws Exception
    {
        return reader.readLine();
    }
    String nextToken()throws Exception
    {
        if(stk==null || !stk.hasMoreTokens())
        {
            stk=new StringTokenizer(nextLine());
            return nextToken();

        }
        return stk.nextToken();
    }

    public static void main(String[]args) throws Exception
    {
        new D().run();
    }

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: piyushd
 * Date: 3/29/11
 * Time: 7:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class TaskD {

    int[] product;
    int[][] I;
    final int MOD = 12345;

    int[] decode(int code) {
        int[] a = new int[26];
        for(int i = 0; i < 26; i++) {
            a[i] = code % product[i];
            code /= product[i];
        }
        return a;
    }

    int encode(int[] a) {
        int code = 0;
        for(int i = 25; i >= 0; i--) {
            code = code * product[i] + a[i];
        }
        return code;
    }

    int[][] matrixMult(int[][] a, int[][] b) {
        int[][] c = new int[a.length][a.length];
        for(int k = 0; k < a.length; k++) {
            for(int i = 0; i < a.length; i++) {
                for(int j = 0; j < a.length; j++) {
                    c[i][j] += a[i][k] * b[k][j];
                    c[i][j] %= MOD;
                }
            }
        }
        return c;
    }

    int[][] matrixExponentiation(int[][] a, long N) {
        if(N == 0) return this.I;

        if(N % 2 == 0) {
            int[][] x = matrixExponentiation(a, N / 2);
            return matrixMult(x, x);
        }

        return matrixMult(a, matrixExponentiation(a, N - 1));
    }

    void run() {
        long N = nextLong();
        int c = nextInt();

        ArrayList<Integer>[] multiplicities = new ArrayList[26];
        for(int i = 0; i < multiplicities.length; i++) multiplicities[i] = new ArrayList<Integer>();

        for(int i = 0; i < c; i++) {
            String character = next();
            int multiple = nextInt();
            multiplicities[character.charAt(0) - 'A'].add(multiple);
        }

        int M = 1;
        this.product = new int[26];
        for (int i = 0; i < 26; i++) {
            product[i] = 1;
            for (int x : multiplicities[i])
                product[i] *= x;
            M *= product[i];
        }

        int[][] a = new int[M][M];
        for(int i = 0; i < M; i++) {
            for(int j = 0; j < 26; j++) if(multiplicities[j].size() > 0) {
                int[] d = decode(i);
                d[j]++;
                d[j] %= product[j];
                int code = encode(d);
                a[i][code]++;
            }
        }

        this.I = new int[M][M];
        for(int i = 0; i < M; i++) I[i][i] = 1;

        int[][] na = matrixExponentiation(a, N);

        int ans = 0;
        for(int code = 0; code < M; code++) {
            int[] d = decode(code);

            boolean ok = true;
            for(int j = 0; j < 26 && ok; j++) if(multiplicities[j].size() > 0) {
                boolean got = false;
                for(int x : multiplicities[j])
                    if(d[j] % x == 0) got = true;
                ok = got;
            }

            if(ok) {
                ans = (ans + na[0][code]) % MOD;
            }
        }

        System.out.println(ans);
    }

    int nextInt(){
        try{
            int c = System.in.read();
            if(c == -1) return c;
            while(c != '-' && (c < '0' || '9' < c)){
                c = System.in.read();
                if(c == -1) return c;
            }
            if(c == '-') return -nextInt();
            int res = 0;
            do{
                res *= 10;
                res += c - '0';
                c = System.in.read();
            }while('0' <= c && c <= '9');
            return res;
        }catch(Exception e){
            return -1;
        }
    }

    long nextLong(){
        try{
            int c = System.in.read();
            if(c == -1) return -1;
            while(c != '-' && (c < '0' || '9' < c)){
                c = System.in.read();
                if(c == -1) return -1;
            }
            if(c == '-') return -nextLong();
            long res = 0;
            do{
                res *= 10;
                res += c-'0';
                c = System.in.read();
            }while('0' <= c && c <= '9');
            return res;
        }catch(Exception e){
            return -1;
        }
    }

    double nextDouble(){
        return Double.parseDouble(next());
    }

    String next(){
        try{
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while(Character.isWhitespace(c))
                c = System.in.read();
            do{
                res.append((char)c);
            }while(!Character.isWhitespace(c=System.in.read()));
            return res.toString();
        }catch(Exception e){
            return null;
        }
    }

    String nextLine(){
        try{
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while(c == '\r' || c == '\n')
                c = System.in.read();
            do{
                res.append((char)c);
                c = System.in.read();
            }while(c != '\r' && c != '\n');
            return res.toString();
        }catch(Exception e){
            return null;
        }
    }

    public static void main(String[] args){
        new TaskD().run();
    }
}


//package round83;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;

public class D3 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		long n = nl();
		int m = ni();
		int[] alp = new int[m];
		int[] val = new int[m];
		int r = 1;
		for(int i = 0;i < m;i++){
			alp[i] = nc()-'A';
			val[i] = ni();
			r *= val[i];
		}
		int[][] g = packD(26, alp, val);
		int[] mul = new int[26];
		for(int i = 0;i < 26;i++){
			mul[i] = 1;
			for(int j : g[i]){
				mul[i] *= j;
			}
		}

		int[][] M = new int[r][r];
		for(int i = 0;i < r;i++){
			int[] a = dec(i, mul);
			for(int j = 0;j < 26;j++){
				if(g[j].length > 0){
					a[j] = (a[j]+1)%mul[j];
					M[enc(a, mul)][i]++;
					a[j] = (a[j]+mul[j]-1)%mul[j];
				}
			}
		}

		int[] v = new int[r];
		v[0] = 1;
		int[] rv = pow(M, v, n);
		int ret = 0;
		outer:
		for(int i = 0;i < r;i++){
			int[] a = dec(i, mul);
			for(int j = 0;j < 26;j++){
				if(g[j].length > 0){
					boolean ok = false;
					for(int k : g[j]){
						if(a[j] % k == 0){
							ok = true;
							break;
						}
					}
					if(!ok)continue outer;
				}
			}
			ret += rv[i];
		}
		out.println(ret % MOD);
	}

	int[] dec(int a, int[] mul)
	{
		int n = mul.length;
		int[] ret = new int[n];
		for(int i = 0;i < n;i++){
			ret[i] = a % mul[i];
			a /= mul[i];
		}
		return ret;
	}

	int enc(int[] a, int[] mul)
	{
		int n = a.length;
		int ret = 0;
		for(int i = n-1;i >= 0;i--){
			ret = ret * mul[i]+a[i];
		}
		return ret;
	}

	public static int MOD = 12345;

	// int行列*ベクトル
	public static int[] mul(int[][] A, int[] v)
	{
		int m = A.length;
		int n = v.length;
		int[] w = new int[m];
		for(int i = 0;i < m;i++){
			long sum = 0;
			for(int k = 0;k < n;k++){
				sum += (long)A[i][k] * v[k];
				sum %= MOD;
			}
			w[i] = (int)sum;
		}
		return w;
	}

	// int行列の2乗
	public static int[][] p2(int[][] A)
	{
		int n = A.length;
		int[][] C = new int[n][n];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				long sum = 0;
				for(int k = 0;k < n;k++){
					sum += (long)A[i][k] * A[k][j];
					sum %= MOD;
				}
				C[i][j] = (int)sum;
			}
		}
		return C;
	}

	// A^e*v
	public static int[] pow(int[][] A, int[] v, long e)
	{
		int[][] MUL = A;
		for(;e > 0;e>>>=1) {
			if((e&1)==1)v = mul(MUL, v);
			MUL = p2(MUL);
		}
		return v;
	}

	public static int[][] packD(int n, int[] from, int[] to)
	{
		int[][] g = new int[n][];
		int[] p = new int[n];
		for(int f : from)p[f]++;
		for(int i = 0;i < n;i++)g[i] = new int[p[i]];
		for(int i = 0;i < from.length;i++){
			g[from[i]][--p[from[i]]] = to[i];
		}
		return g;
	}

	void run() throws Exception
	{
//		long n = 1000000000000000000L;
//		int c = 1000;
//		Random gen = new Random();
//		StringBuilder sb = new StringBuilder();
//		sb.append(n + " ");
//		sb.append(c + " ");
//		for (int i = 0; i < c-2; i++) {
//			sb.append((char)(gen.nextInt(26)+'A') + " " + 1 + " ");
//		}
//		sb.append((char)(gen.nextInt(26)+'A') + " " + 3 + " ");
//		sb.append((char)(gen.nextInt(26)+'A') + " " + 41 + " ");
//		INPUT = sb.toString();

		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}

	public static void main(String[] args) throws Exception
	{
		new D3().run();
	}

	char nc() {
		try {
			int b;
			while ((b = is.read()) != -1
					&& (b == ' ' || b == '\r' || b == '\n'))
				;
			if (b == -1)
				return 0;
			return (char) b;
		} catch (IOException e) {
		}
		return 0;
	}

	public int ni()
	{
		try {
			int num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}

			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	public long nl()
	{
		try {
			long num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}

			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	public String ns()
	{
		try{
			int b = 0;
			StringBuilder sb = new StringBuilder();
			while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
			if(b == -1)return "";
			sb.append((char)b);
			while(true){
				b = is.read();
				if(b == -1)return sb.toString();
				if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
				sb.append((char)b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public char[] ns(int n)
	{
		char[] buf = new char[n];
		try{
			int b = 0, p = 0;
			while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
			if(b == -1)return null;
			buf[p++] = (char)b;
			while(p < n){
				b = is.read();
				if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
				buf[p++] = (char)b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	double nd() { return Double.parseDouble(ns()); }
	boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}


import java.io.*;
import java.util.*;

public class B {

	static int n, t[], g[], MOD = (int) 1e9 + 7;
	static int[][][] memo1, memo2[], memo3[];

	static int dp1(int idx, int remCnt, int remSum) {
		if (idx == n)
			return remSum == 0 && remCnt == 0 ? 1 : 0;
		if (remCnt < 0 || remSum < 0)
			return 0;
		if (memo1[idx][remCnt][remSum] != -1)
			return memo1[idx][remCnt][remSum];
		int ans = dp1(idx + 1, remCnt, remSum);
		if (g[idx] == 0) {
			ans += dp1(idx + 1, remCnt - 1, remSum - t[idx]);
			if (ans >= MOD)
				ans -= MOD;
		}
		return memo1[idx][remCnt][remSum] = ans;
	}

	static int dp2(int idx, int remCnt1, int remCnt2, int remSum) {

		if (idx == n)
			return remSum == 0 && remCnt1 == 0 && remCnt2 == 0 ? 1 : 0;
		if (remSum < 0 || remCnt1 < 0 || remCnt2 < 0)
			return 0;
		if (memo2[idx][remCnt1][remCnt2][remSum] != -1)
			return memo2[idx][remCnt1][remCnt2][remSum];
		int ans = dp2(idx + 1, remCnt1, remCnt2, remSum);

		if (g[idx] == 1)
			ans += dp2(idx + 1, remCnt1 - 1, remCnt2, remSum - t[idx]);
		else if (g[idx] == 2)
			ans += dp2(idx + 1, remCnt1, remCnt2 - 1, remSum - t[idx]);

		return memo2[idx][remCnt1][remCnt2][remSum] = ans;
	}

	private static int dp3(int cnt0, int cnt1, int cnt2, int last) {
		if (cnt0 + cnt1 + cnt2 == 0)
			return 1;
		if (memo3[last][cnt0][cnt1][cnt2] != -1)
			return memo3[last][cnt0][cnt1][cnt2];
		long ans = 0;
		if (cnt0 > 0 && last != 0)
			ans += dp3(cnt0 - 1, cnt1, cnt2, 0);
		if (cnt1 > 0 && last != 1)
			ans += dp3(cnt0, cnt1 - 1, cnt2, 1);
		if (cnt2 > 0 && last != 2)
			ans += dp3(cnt0, cnt1, cnt2 - 1, 2);
		return memo3[last][cnt0][cnt1][cnt2] = (int) (ans % MOD);

	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		n = sc.nextInt();
		int[] fac = new int[n + 1];
		t = new int[n];
		g = new int[n];
		int[] cnt = new int[3];
		fac[0] = 1;
		for (int i = 1; i <= n; i++)
			fac[i] = (int) (i * 1L * fac[i - 1] % MOD);
		int T = sc.nextInt();
		for (int i = 0; i < n; i++) {
			t[i] = sc.nextInt();
			g[i] = sc.nextInt() - 1;
			cnt[g[i]]++;

		}
		memo1 = new int[n][cnt[0] + 1][T + 1];
		memo2 = new int[n][cnt[1] + 1][cnt[2] + 1][T + 1];
		memo3 = new int[4][cnt[0] + 1][cnt[1] + 1][cnt[2] + 1];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= cnt[0]; j++)
				Arrays.fill(memo1[i][j], -1);
			for (int j = 0; j <= cnt[1]; j++)
				for (int k = 0; k <= cnt[2]; k++)
					Arrays.fill(memo2[i][j][k], -1);

		}
		for (int i = 0; i < 4; i++)
			for (int j = 0; j <= cnt[0]; j++)
				for (int k = 0; k <= cnt[1]; k++)
					Arrays.fill(memo3[i][j][k], -1);
		int ans = 0;
		for (int cnt0 = 0; cnt0 <= cnt[0]; cnt0++)
			for (int sum0 = 0; sum0 <= T; sum0++)
				for (int cnt1 = 0; cnt1 <= cnt[1]; cnt1++)
					for (int cnt2 = 0; cnt2 <= cnt[2]; cnt2++) {
						long ways = dp1(0, cnt0, sum0) * 1L * dp2(0, cnt1, cnt2, T - sum0) % MOD;
						ways = ways * dp3(cnt0, cnt1, cnt2, 3) % MOD;
						ways *= fac[cnt0];
						ways %= MOD;
						ways *= fac[cnt1];
						ways %= MOD;
						ways *= fac[cnt2];
						ways %= MOD;
						ans += ways;
						if (ans >= MOD)
							ans -= MOD;
					}
		out.println(ans);
		out.close();

	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		Scanner(String fileName) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(fileName));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		String nextLine() throws IOException {
			return br.readLine();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}

		boolean ready() throws IOException {
			return br.ready();
		}

	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskE solver = new TaskE();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskE {
    final int mod = (int) 1e9 + 7;
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.readInt();
        int k = in.readInt();
        int[][] c = Combinatorics.GetCombinations(n, n, mod);
        int[][][][] dp = new int[n + 2][n + 2][2][2];
        dp[0][0][0][1] = 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= i; ++j) {
                for (int first = 0; first < 2; ++first) {
                    for (int second = 0; second < 2; ++second) {
                        if (first == 1) {
                            if (j > 0) {
                                dp[i][j][second][1] += dp[i - 1][j - 1][first][second];
                                dp[i][j][second][1] %= mod;
                            }
                        }
                        if (i < n && j > 0) {
                            dp[i][j][second][0] += dp[i - 1][j - 1][first][second];
                            dp[i][j][second][0] %= mod;
                        }
                        dp[i][j][second][1] += dp[i - 1][j][first][second];
                        dp[i][j][second][1] %= mod;
                    }
                }
            }
        }
        long[] fact = new long[n + 5];
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) {
            fact[i] = fact[i - 1] * i;
            fact[i] %= mod;
        }
        long[] f = new long[n + 1];
        for (int i = 0; i <= n; ++i) {
            f[i] = 0;
            for (int first = 0; first < 2; ++first) {
                for (int second = 0; second < 2; ++second) {
                    f[i] += dp[n][i][first][second];
                    f[i] %= mod;
                }
            }
            f[i] *= fact[n - i];
            f[i] %= mod;
        }

        for (int i = n; i >= 0; --i) {
            for (int j = i + 1; j <= n; ++j) {
                f[i] -= (f[j] * c[j][i]) % mod;
                f[i] += mod;
                f[i] %= mod;
            }
        }
        out.println(f[k]);
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        // InputMismatchException -> UnknownError
        if (numChars == -1)
            throw new UnknownError();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new UnknownError();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int readInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        } else if (c == '+') {
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public static boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    }

class Combinatorics {
    public static int[][] GetCombinations(int n, int m, int MOD) {
        int[][] c = new int[n + 1][m + 1];
        c[0][0] = 1;
        for (int i = 1; i <= n; ++i) {
            c[i][0] = 1;
            for (int j = 1; j < i && j <= m; ++j) {
                c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
                if (c[i][j] >= MOD) {
                    c[i][j] -= MOD;
                }
            }
            c[i][i] = 1;
        }
        return c;
    }

    }


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;

public class myFile
{
	public static void main(String args[])
	{
	   Scanner sc = new Scanner(System.in);
	   int i,n,k;
	   while(sc.hasNextInt())
	   {
		   n = sc.nextInt();
		   k = sc.nextInt();
		   sc.nextLine();
		   String str = sc.nextLine();
		   if(n==1 && k==1)
		   {
			   System.out.println("0");
			   continue;
		   }
		   StringBuilder sb = new StringBuilder(str);
		   if(str.charAt(0)!='1' && k>0)
		   {
			   sb.setCharAt(0, '1');
			   k--;
		   }
		   for(i=1;k>0 && i<str.length();i++)
		   {
			   if(str.charAt(i)!='0') {
				   k--;
				   sb.setCharAt(i, '0');
			   }
		   }
		   System.out.println(sb);
	   }

	}

}


import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	final static int PERIOD = 60;
	final static int POWER = 13;

	long fib(long n, long mod) {
		long[][] mat = new long[2][2];
		mat[0][1] = mat[1][0] = mat[1][1] = 1;
		Matrix v = new Matrix(mat);
		v = v.power(n, mod);
		return v.mat[0][1];
	}

	void run() {
		InputReader in = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);

		long num = in.nextLong(), mod = 10, per = PERIOD;
		ArrayList<Long> valid = new ArrayList<Long>();
		for (int i = 0; i < PERIOD; ++i)
			if (fib(i, mod) == num % mod)
				valid.add((long)i);

		for (int i = 1; i < POWER; ++i) {
			mod *= 10;
			long rem = num % mod;
			ArrayList<Long> nv = new ArrayList<Long>();
			for (int j = 0; j < 10; ++j)
				for (int k = 0; k < valid.size(); ++k) {
					if (fib(j * per + valid.get(k), mod) == rem)
						nv.add(j * per + valid.get(k));
				}
			valid = nv;
			per *= 10;
		}

		if (valid.size() == 0)
			out.println(-1);
		else
			out.println(valid.get(0));
		out.close();
	}
}

class Matrix {
	long[][] mat;
	final static Matrix ONE = new Matrix(new long[][] {{1, 0}, {0, 1}});
	Matrix(long[][] mat) {
		this.mat = mat;
	}
	long add(long a, long b, long mod) {
		if (a + b >= mod)
			return a + b - mod;
		return a + b;
	}
	long multiply(long a, long b, long mod) {
		long ret = 0;
		while (a > 0) {
			if ((a & 1) == 1)
				ret = add(ret, b, mod);
			b = add(b, b, mod);
			a >>= 1;
		}
		return ret;
	}
	Matrix multiply(Matrix o, long mod) {
		long[][] ret = new long[2][2];
		for (int i = 0; i < 2; ++i)
			for (int j = 0; j < 2; ++j)
				for (int k = 0; k < 2; ++k)
					ret[i][j] = add(ret[i][j], multiply(mat[i][k], o.mat[k][j], mod), mod);
		return new Matrix(ret);
	}
	Matrix power(long p, long mod) {
		if (p == 0) {
			return Matrix.ONE;
		}
		Matrix ret = power(p / 2, mod);
		ret = ret.multiply(ret, mod);
		if ((p & 1) == 1)
			ret = ret.multiply(this, mod);
		return ret;
	}
}

class InputReader {
	BufferedReader buff;
	StringTokenizer tokenizer;

	InputReader(InputStream stream) {
		buff = new BufferedReader(new InputStreamReader(stream));
		tokenizer = null;
	}
	boolean hasNext() {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			try {
				tokenizer = new StringTokenizer(buff.readLine());
			}
			catch (Exception e) {
				return false;
			}
		return true;
	}
	String next() {
		if (!hasNext())
			throw new RuntimeException();
		return tokenizer.nextToken();
	}
	int nextInt() { return Integer.parseInt(next()); }
	long nextLong() { return Long.parseLong(next()); }
}


import java.io.*;
import java.math.*;
import java.util.*;

public class Solution {
    final static long MOD = 10000000000000L;

    long multiply(long a, long b) {
        return BigInteger.valueOf(a).multiply(BigInteger.valueOf(b)).remainder(BigInteger.valueOf(MOD)).longValue();
    }

    long[][] multiply(long[][] a, long[][] b) {
        long[][] c = new long[2][2];
        for (int i = 0; i < 2; ++ i) {
            for (int j = 0; j < 2; ++ j) {
                c[i][j] = (multiply(a[i][0], b[0][j]) + multiply(a[i][1], b[1][j])) % MOD;
            }
        }
        return c;
    }

    long[][] pow(long[][] a, long n) {
        long[][] ret = new long[][] {{1, 0}, {0, 1}};
        while (n > 0) {
            if (n % 2 == 1) {
                ret = multiply(ret, a);
            }
            a = multiply(a, a);
            n >>= 1;
        }
        return ret;
    }

    long fibonacci(long n) {
        return multiply(new long[][] {{0, 1}, {0, 0}}, pow(new long[][]{{0, 1}, {1, 1}}, n))[0][0];
    }

    public void run() {
        try {
            long m = reader.nextLong();
            ArrayList <Long> indices = new ArrayList <Long>();
            indices.add(0L);
            long period = 1;
            for (long mod = 10; mod <= MOD; mod *= 10) {
                ArrayList <Long> newIndices = new ArrayList <Long>();
                int times = mod == 10 ? 60 : mod <= 1000 ? 5 : 10;
                for (long iter : indices) {
                    long now = iter;
                    for (int i = 0; i < times; ++ i) {
                        if (fibonacci(now) % mod == m % mod) {
                            newIndices.add(now);
                        }
                        now += period;
                    }
                }
                period *= times;
                indices = newIndices;
            }
            Collections.sort(indices);
            writer.println(indices.isEmpty() ? -1 : indices.get(0));
        } catch (IOException ex) {
        }
        writer.close();
    }

    InputReader reader;
    PrintWriter writer;

    Solution() {
        reader = new InputReader();
        writer = new PrintWriter(System.out);
    }

    public static void main(String[] args) {
        new Solution().run();
    }

}

class InputReader {
    BufferedReader reader;
    StringTokenizer tokenizer;

    InputReader() {
        reader = new BufferedReader(new InputStreamReader(System.in));
        tokenizer = new StringTokenizer("");
    }

    String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    Integer nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    Long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}