import java.util.*;
import java.io.*;
public final class Main {
    static long pow(long n, long x, long mod) {
        long res = 1;
        while(x>0) {
            if(x%2==1)
                res = (res*n)%mod;
            x>>=1;
            n = (n*n)%mod;
        }
        return res;
    }
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long m = sc.nextLong();
        long ans = pow(3, n, m) - 1;
        if(ans<0)
            ans+=m;
        System.out.println(ans);
    }
}


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

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

    static class TaskC {
        public int check(int n, Integer x, int len) {
            int ans = 0;
            while (n > 0 && x > 0) {
                if (n % 10 == x % 10) {
                    ans++;
                    x /= 10;
                }

                n /= 10;
            }

            if (x == 0) return len - ans;
            else return 100;

        }

        public void solve(int testNumber, Scanner in, PrintWriter out) {
            Integer n = in.nextInt();
            int len = n.toString().length();
            int ans = 100;
            for (int i = 1; i * i <= n; i++) {
                ans = Math.min(check(n, i * i, len), ans);
            }

            if (ans != 100) out.println(ans);
            else out.println(-1);
        }

    }
}


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

public class Main {
    public static void main(String[] args) throws IOException {
        //Code starts

        FastReader ip = new FastReader();
        OutputStream output = System.out;
        PrintWriter out = new PrintWriter(output);
        String s=ip.nextLine();
        int arr[]=new int[6];
        int sum1=0;
        int sum2=0;
        for(int i=0;i<6;i++){
            arr[i]=s.charAt(i)-'0';
            if(i<3){
                sum1+=arr[i];
            }else{
                sum2+=arr[i];
            }
        }

        if(sum1==sum2){
            out.println(0);
        }else {
            int m1 = 0, m2 = 0, m3 = 0, M1 = 0, M2 = 0, M3 = 0;
            int diff=0;
            if (sum2 > sum1) {
                diff=sum2-sum1;
                m1 = Math.min(arr[0], Math.min(arr[1], arr[2]));
                m3 = Math.max(arr[0], Math.max(arr[1], arr[2]));
                m2 = (sum1 - m1) - m3;
                M1 = Math.min(arr[3], Math.min(arr[4], arr[5]));
                M3 = Math.max(arr[3], Math.max(arr[4], arr[5]));
                M2 = (sum2 - M1) - M3;
            } else {
                diff=sum1-sum2;
                m1 = Math.min(arr[3], Math.min(arr[4], arr[5]));
                m3 = Math.max(arr[3], Math.max(arr[4], arr[5]));
                m2 = (sum2 - m1) - m3;
                M1 = Math.min(arr[0], Math.min(arr[1], arr[2]));
                M3 = Math.max(arr[0], Math.max(arr[1], arr[2]));
                M2 = (sum1 - M1) - M3;
            }
            int c = 0;
            if (M3 > (9 - m1)) {
                if (diff > M3) {
                    diff -= M3;
                    c++;
                    if (M2 > (9 - m1)) {
                        if (diff > M2) {
                            c++;
                        }
                    } else {
                        if (diff > (9 - m1)) {
                            c++;
                        }
                    }
                }
            } else {
                if (diff > (9 - m1)) {
                    diff -= (9 - m1);
                    c++;
                    if (M3 > (9 - m2)) {
                        if (diff > M3) {
                            c++;
                        }
                    } else {
                        if (diff > (9 - m2)) {
                            c++;
                        }
                    }
                }

            }

         out.println(c+1);
        }
        out.close();
        //Code ends
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
        FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); }
        String next() { while (st == null || !st.hasMoreElements()) {
            try { st = new StringTokenizer(br.readLine()); }
            catch (IOException e) { e.printStackTrace(); } }
            return st.nextToken();
        }

        int nextInt() { return Integer.parseInt(next()); }
        long nextLong() { return Long.parseLong(next()); }
        double nextDouble() { return Double.parseDouble(next()); }

        String nextLine() {
            String str = "";
            try { str = br.readLine(); }
            catch (IOException e) { e.printStackTrace(); }
            return str;
        }
    }

}


public class RosettaProblem {
	public static void main(String[] args) {
		String s = Integer.toString(Integer.parseInt(new java.util.Scanner(System.in).nextLine()), 8);
		int res = 0;
		for(int i = 0 ; i < s.length() ; i++)
			res+=s.charAt(i) == '1'?1:0;
		System.out.println(res);
	}
}


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

public class santaandcandies2
{
	static int[] arr;
	static int inser_p;
	static int n;
	public static void main(String[] args)throws IOException {
		InputReader ir=new InputReader(System.in);
		OutputWriter ow=new OutputWriter(System.out);
		precompute();
		inser_p=((inser_p=Arrays.binarySearch(arr,n=ir.readInt()))<0?(abs(inser_p)-2):inser_p);
		ow.printLine(inser_p);
		int i=1;
		for(;i<inser_p;++i)
			ow.print(i+" ");
		ow.printLine(i+(n-arr[inser_p]));
		ow.flush();
		ow.close();
	}
	static void precompute()
	{
		int mul=0,n=1;
		arr=new int[45];
       while((mul=(n*(n+1)/2))<=1000)
       	 arr[n++]=mul;
	}
	  static  class InputReader {
    	private InputStream stream;
    	private byte[] buf = new byte[1024];
    	private int curChar;
    	private int numChars;
    	private SpaceCharFilter filter;

    	public InputReader(InputStream stream) {
    		this.stream = stream;
    		}
    	public int read() {
    		if (numChars == -1)
    		    throw new InputMismatchException();
    		if (curChar >= numChars) {
    			curChar = 0;
    		try {
    				numChars = stream.read(buf);
    			} catch (IOException e) {
    				throw new InputMismatchException();
    			}
    		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();
    	}
    			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 String readString() {
    			int c = read();
    			while (isSpaceChar(c))
    				c = read();
    			StringBuilder res = new StringBuilder();
    			do {
    				res.appendCodePoint(c);
    				c = read();
    			} while (!isSpaceChar(c));
    			return res.toString();
    		}
    		public boolean isSpaceChar(int c) {
    			if (filter != null)
    				return filter.isSpaceChar(c);
    			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    		}
    		public String next() {
    			return readString();
    		}
    		public interface SpaceCharFilter {
    			public boolean isSpaceChar(int ch);
    		}
    	}
   static class OutputWriter {
    		private final PrintWriter writer;
    		public OutputWriter(OutputStream outputStream) {
    			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    		}
    		public OutputWriter(Writer writer) {
    			this.writer = new PrintWriter(writer);
    		}
    		public void print(Object...objects) {
    			for (int i = 0; i < objects.length; i++) {
    				if (i != 0)
    					writer.print(' ');
    				writer.print(objects[i]);
    			}
    		}
    		public void printLine(Object...objects) {
    			print(objects);
    			writer.println();
    		}
    		public void close() {
    			writer.close();
    		}
    		public void flush() {
    			writer.flush();
    		}
    		}
}


import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Q3E {

public static int gcd (int a,int b)
{
    if (b==0)
        return a;
    return gcd(b,a%b);
}
    public static void main(String[] args) throws FileNotFoundException
    {
        Scanner in = new Scanner(System.in);

        int n =  in.nextInt();

        int count = 0;

        for (int i = 1; i < n; i++) {

            int commondivisor = gcd(n-1, i);
            if (commondivisor == 1)
                count ++;
        }
        System.out.println(count);
    }
}


import java.util.Scanner;

/**
 * Created by PQZR3864 on 2018-10-04.
 */
public class PhoneNumber {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int count = 0;
        int n = input.nextInt();
        String digits = input.next();

        for(int i=0 ; i<digits.length() ;i++){
            if(digits.charAt(i) == '8') count++;
        }
        count = count > (n/11) ? (n/11) : count ;
        System.out.println(count);
        input.close();
    }
}


import java.math.BigInteger;
import java.util.*;

public class Main {
    static final int MOD = 1000000007;
    public static int count(int height , int width) {
        boolean[][] field = new boolean[height][width];
        int[][][][] res = new int[2][2][width][width];
        int totalRes = 0;
        for (int row = 0; row < height; ++row) {
            int[][][][] newRes = new int[2][2][width][width];
            for (int fD = 0; fD < 2; ++fD)
                for (int lD = 0; lD < 2; ++lD)
                    for (int fC = 0; fC < width; ++fC)
                        for (int lC = fC; lC < width; ++lC) {
                            int cur = res[fD][lD][fC][lC];
                            if (cur == 0) continue;
                            for (int newfD = fD; newfD < 2; ++newfD)
                                for (int newlD = lD; newlD < 2; ++newlD) {
                                    int newfC;
                                    if (newfD == 0)
                                        newfC = fC;
                                    else if (fD == 0)
                                        newfC = fC + 1;
                                    else
                                        newfC = fC;
                                    int newlC;
                                    if (newlD == 0)
                                        newlC = lC;
                                    else if (lD == 0)
                                        newlC = lC - 1;
                                    else
                                        newlC = lC;
                                    if (newfC > newlC)
                                        continue;
                                    int[] arr = newRes[newfD][newlD][newfC];
                                    arr[newlC] += cur;
                                    if (arr[newlC] >= MOD) arr[newlC] -= MOD;
                                }
                        }

            for (int fD = 0; fD < 2; ++fD)
                for (int lD = 0; lD < 2; ++lD) {
                    int[][] arr = newRes[fD][lD];
                    if (fD == 0 && lD == 1) {
                        {
                            int lastDelta = lD == 0 ? 1 : -1;
                            int lastMin = lastDelta == 1 ? 0 : width - 1;
                            int lastMax = lastDelta == 1 ? width : -1;
                            for (int lC = lastMin; lC != lastMax; lC += lastDelta) {
                                for (int fC = 0; fC <= lC; ++fC) {
                                    int cur = arr[fC][lC];
                                    if (cur == 0) continue;
                                    int newlC = lC + lastDelta;
                                    if (newlC >= fC && newlC < width) {
                                        arr[fC][newlC] += cur;
                                        if (arr[fC][newlC] >= MOD) arr[fC][newlC] -= MOD;
                                    }
                                }
                            }
                        }
                        {
                            int firstDelta = fD == 0 ? -1 : 1;
                            int firstMin = firstDelta == 1 ? 0 : width - 1;
                            int firstMax = firstDelta == 1 ? width : -1;
                            for (int fC = firstMin; fC != firstMax; fC += firstDelta) {
                                for (int lC = fC; lC < width; ++lC) {
                                    int cur = arr[fC][lC];
                                    if (cur == 0) continue;
                                    int newfC = fC + firstDelta;
                                    if (newfC >= 0 && newfC <= lC) {
                                        arr[newfC][lC] += cur;
                                        if (arr[newfC][lC] >= MOD) arr[newfC][lC] -= MOD;
                                    }
                                }
                            }
                        }
                    } else {
                        {
                            int firstDelta = fD == 0 ? -1 : 1;
                            int firstMin = firstDelta == 1 ? 0 : width - 1;
                            int firstMax = firstDelta == 1 ? width : -1;
                            for (int fC = firstMin; fC != firstMax; fC += firstDelta) {
                                for (int lC = fC; lC < width; ++lC) {
                                    int cur = arr[fC][lC];
                                    if (cur == 0) continue;
                                    int newfC = fC + firstDelta;
                                    if (newfC >= 0 && newfC <= lC) {
                                        arr[newfC][lC] += cur;
                                        if (arr[newfC][lC] >= MOD) arr[newfC][lC] -= MOD;
                                    }
                                }
                            }
                        }
                        {
                            int lastDelta = lD == 0 ? 1 : -1;
                            int lastMin = lastDelta == 1 ? 0 : width - 1;
                            int lastMax = lastDelta == 1 ? width : -1;
                            for (int lC = lastMin; lC != lastMax; lC += lastDelta) {
                                for (int fC = 0; fC <= lC; ++fC) {
                                    int cur = arr[fC][lC];
                                    if (cur == 0) continue;
                                    int newlC = lC + lastDelta;
                                    if (newlC >= fC && newlC < width) {
                                        arr[fC][newlC] += cur;
                                        if (arr[fC][newlC] >= MOD) arr[fC][newlC] -= MOD;
                                    }
                                }
                            }
                        }
                    }
                }

            for (int fC = 0; fC < width; ++fC) {
                boolean ok = true;
                for (int lC = fC; lC < width; ++lC) {
                    if (field[row][lC])
                        ok = false;
                    if (!ok) {
                        for (int fD = 0; fD < 2; ++fD)
                            for (int lD = 0; lD < 2; ++lD)
                                newRes[fD][lD][fC][lC] = 0;
                   } else {
                        newRes[0][0][fC][lC] += 1;
                        if (newRes[0][0][fC][lC] >= MOD) newRes[0][0][fC][lC] -= MOD;
                    }
                }
            }

            res = newRes;
            for (int fD = 0; fD < 2; ++fD)
                for (int lD = 0; lD < 2; ++lD)
                    for (int fC = 0; fC < width; ++fC)
                        for (int lC = fC; lC < width; ++lC) {
                            totalRes += res[fD][lD][fC][lC];
                            if (totalRes >= MOD) totalRes -= MOD;
                        }
        }
        return totalRes;
    }
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt() , m = cin.nextInt();
        System.out.println(count(n, m));
    }
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args){
		String[] table = {"",
				"Washington",
				"Adams",
				"Jefferson",
				"Madison",
				"Monroe",
				"Adams",
				"Jackson",
				"Van Buren",
				"Harrison",
				"Tyler",
				"Polk",
				"Taylor",
				"Fillmore",
				"Pierce",
				"Buchanan",
				"Lincoln",
				"Johnson",
				"Grant",
				"Hayes",
				"Garfield",
				"Arthur",
				"Cleveland",
				"Harrison",
				"Cleveland",
				"McKinley",
				"Roosevelt",
				"Taft",
				"Wilson",
				"Harding",
				"Coolidge",
				"Hoover",
				"Roosevelt",
				"Truman",
				"Eisenhower",
				"Kennedy",
				"Johnson",
				"Nixon",
				"Ford",
				"Carter",
				"Reagan",
				"111111101101000101001101110010001",
				"111111101101000101001101110010001"};

		Scanner in = new Scanner(System.in);
		int x;
		x = in.nextInt();

		System.out.println(table[x]);

	}

}


    import  java.util.Scanner;
    import java.util.Vector;

    public class A_VanyaAndCubes {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int cubes = input.nextInt();
            /*
            int [] arr = new int [cubes+1];

            arr[0] = 1 ;

            for(int i = 1 ; i < cubes ; i++)
                for(int j = 1 ; j <= i+1 ;j++)
                    arr[i] += j;

            int total = 0,count = 0 ;
            while (cubes > total)
                total += arr[count++];
            System.out.print(total == count? count :count-1);
            */
            int x = 0 ;
            int total = 0 ;
            int count = 0 ;
            for(int i = 1 ; i <= cubes ; i++){
                x += i;
                total += x;
                if(total > cubes)
                    break;
                count++;
            }
            System.out.print(count);
        }
    }


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

public class Main {

	static int mod = (int) 1e9 + 7;
	static int MAX = (int) 1e7;

	public static void main(String[] args) {
		FasterScanner s = new FasterScanner();
		PrintWriter out = new PrintWriter(System.out);
		int t = 1;
		loop: while (t-- > 0) {
			int n = s.nextInt();
			int[] a = s.nextIntArray(n);
			int[] c = new int[1001];
			for(int i=0;i<n;i++){
				c[a[i]]++;
			}
			for(int i=0;i<1001-2;i++){
				if(c[i]>0 && c[i+1]>0 && c[i+2]>0){
					out.println("YES");
					continue loop;
				}
			}
			out.println("NO");
		}
		out.close();
	}

	static void dfs1(List<Integer>[] graph, boolean[] used, List<Integer> order, int u) {
		used[u] = true;
		for (int v : graph[u])
			if (!used[v])
				dfs1(graph, used, order, v);
		order.add(u);
	}

	static void dfs2(List<Integer>[] reverseGraph, int[] comp, int u, int color) {
		comp[u] = color;
		for (int v : reverseGraph[u])
			if (comp[v] == -1)
				dfs2(reverseGraph, comp, v, color);
	}

	public static boolean[] solve2Sat(List<Integer>[] graph) {
		int n = graph.length;
		boolean[] used = new boolean[n];
		List<Integer> order = new ArrayList<>();
		for (int i = 0; i < n; ++i)
			if (!used[i])
				dfs1(graph, used, order, i);

		List<Integer>[] reverseGraph = new List[n];
		for (int i = 0; i < n; i++)
			reverseGraph[i] = new ArrayList<>();
		for (int i = 0; i < n; i++)
			for (int j : graph[i])
				reverseGraph[j].add(i);

		int[] comp = new int[n];
		Arrays.fill(comp, -1);
		for (int i = 0, color = 0; i < n; ++i) {
			int u = order.get(n - i - 1);
			if (comp[u] == -1)
				dfs2(reverseGraph, comp, u, color++);
		}

		for (int i = 0; i < n; ++i)
			if (comp[i] == comp[i ^ 1])
				return null;

		boolean[] res = new boolean[n / 2];
		for (int i = 0; i < n; i += 2)
			res[i / 2] = comp[i] > comp[i ^ 1];
		return res;
	}

	public static int[] createSets(int size) {
		int[] p = new int[size];
		for (int i = 0; i < size; i++)
			p[i] = i;
		return p;
	}

	public static int root(int[] p, int x) {
		return x == p[x] ? x : (p[x] = root(p, p[x]));
	}

	public static void unite(int[] p, int a, int b) {
		a = root(p, a);
		b = root(p, b);
		if (a != b)
			p[a] = b;
	}

	public static boolean nextPermutation(int[] p) {
		for (int a = p.length - 2; a >= 0; --a)
			if (p[a] < p[a + 1])
				for (int b = p.length - 1;; --b)
					if (p[b] > p[a]) {
						int t = p[a];
						p[a] = p[b];
						p[b] = t;
						for (++a, b = p.length - 1; a < b; ++a, --b) {
							t = p[a];
							p[a] = p[b];
							p[b] = t;
						}
						return true;
					}
		return false;
	}

	public static boolean isPrime(long n) {
		if (n <= 1)
			return false;
		for (long i = 2; i * i <= n; i++)
			if (n % i == 0)
				return false;
		return true;
	}

	static class SegTree {
		int[] val;
		int n;

		SegTree(int n) {
			val = new int[2 * n];
			for (int i = 0; i < n; i++)
				val[n + i] = i + 1;
			for (int i = n - 1; i >= 0; i--)
				val[i] = Math.max(val[2 * i], val[2 * i + 1]);
			this.n = n;
		}

		void upd(int p) {
			p += n;
			val[p] = -1;
			for (; (p >>= 1) != 0;) {
				val[p] = Math.max(val[2 * p], val[2 * p + 1]);
			}
		}

		int query(int l, int r) { // sum on interval [l, r)
			// System.out.println(l+" "+r);
			int res = -1;
			for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
				if ((l & 1) != 0)
					res = Math.max(res, val[l++]);
				if ((r & 1) != 0)
					res = Math.max(res, val[--r]);
			}
			return res;
		}
	}

	static boolean check(int i, int j, int n) {
		if (i >= 0 && j >= 0 && i < n && j < n)
			return true;
		return false;
	}

	public static int[] generateDivisorTable(int n) {
		int[] divisor = new int[n + 1];
		for (int i = 1; i <= n; i++)
			divisor[i] = i;
		for (int i = 2; i * i <= n; i++)
			if (divisor[i] == i)
				for (int j = i * i; j <= n; j += i)
					divisor[j] = i;
		return divisor;
	}

	public static int[][] matrixMul(int[][] a, int[][] b) {
		int n = a.length;
		int m = a[0].length;
		int k = b[0].length;
		int[][] res = new int[n][k];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < k; j++) {
				for (int p = 0; p < m; p++) {
					res[i][j] = (res[i][j] + (int) ((a[i][p] * 1l * b[p][j]) % mod)) % mod;
				}
			}
		}
		return res;
	}

	public static int[][] matrixPow(int[][] a, int p) {
		if (p == 0) {
			return matrixUnit(a.length);
		} else if (p % 2 == 0) {
			return matrixPow(matrixMul(a, a), p / 2);
		} else {
			return matrixMul(a, matrixPow(a, p - 1));
		}
	}

	public static int[][] matrixUnit(int n) {
		int[][] res = new int[n][n];
		for (int i = 0; i < n; ++i) {
			res[i][i] = 1;
		}
		return res;
	}

	static int[] modInv(int n, int mod) {
		int[] ans = new int[n];
		for (int i = 1; i < n; i++)
			ans[i] = (int) pow(i, mod - 2, mod);
		return ans;
	}

	static int[] primes;

	public static long ncr(int n, int r) {
		if (n < r)
			return 0;
		if (n == r)
			return 1;
		if (r == 0)
			return 1;
		if (r < 0)
			return 0;
		long ans = 1;
		for (int i = 0; i < primes.length; i++) {
			long power = count(n, primes[i]) - count(n - r, primes[i]) - count(r, primes[i]);
			ans *= pow(primes[i], power, mod);
			ans %= mod;
		}
		return ans;
	}

	public static long pow(long x, long n, int mod) {
		long res = 1;
		for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) {
			if ((n & 1) != 0) {
				res = (int) (res * p % mod);
			}
		}
		return res;
	}

	public static long count(int n, int p) {
		long count = 0;
		long z = p;
		while (z <= n) {
			count += n / z;
			z *= p;
		}
		return count;

	}

	public static int[] generatePrimes(int n) {
		boolean[] prime = new boolean[n + 1];
		Arrays.fill(prime, 2, n + 1, true);
		for (int i = 2; i * i <= n; i++)
			if (prime[i])
				for (int j = i * i; j <= n; j += i)
					prime[j] = false;
		int[] primes = new int[n + 1];
		int cnt = 0;
		for (int i = 0; i < prime.length; i++)
			if (prime[i])
				primes[cnt++] = i;

		return Arrays.copyOf(primes, cnt);
	}

	static class Pairxy implements Comparable<Pairxy> {
		int x;
		int y;

		Pairxy(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public int compareTo(Pairxy o) {
			return Integer.compare(this.x, o.x);
		}

	}

	static class Pair implements Comparable<Pair> {
		long val;
		int ind;

		Pair(long v, int i) {
			val = v;
			ind = i;
		}

		public int compareTo(Pair o) {
			if (this.val != o.val)
				return Long.compare(this.val, o.val);
			return Integer.compare(this.ind, o.ind);
		}
	}

	public static long C(int n, int r) {
		long ans = 1;
		int x = 1;
		while (x <= r) {
			ans *= n;
			ans /= x;
			x++;
			n--;
		}
		return ans;
	}

	static class DisjointSetsRank {

		int[] p;
		int[] rank;

		public DisjointSetsRank(int size) {
			p = new int[size];
			for (int i = 0; i < size; i++)
				p[i] = i;
			rank = new int[size];
		}

		public int root(int x) {
			return x == p[x] ? x : (p[x] = root(p[x]));
		}

		public int sameSet(int x, int y) {
			if (root(x) == root(y))
				return 1;
			return 0;

		}

		public void unite(int a, int b) {
			a = root(a);
			b = root(b);
			if (a == b)
				return;

			if (rank[a] < rank[b]) {
				p[a] = b;
			} else {
				p[b] = a;
				if (rank[a] == rank[b])
					++rank[a];
			}
		}

	}

	static int gcd(long m, long n) {
		long x;
		long y;
		while (m % n != 0) {
			x = n;
			y = m % n;
			m = x;
			n = y;
		}
		return (int) n;
	}

	static class FasterScanner {
		private byte[] buf = new byte[1024];
		private int curChar;
		private int snumChars;

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

		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public String nextString() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long 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 int nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				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 int[] nextIntArray(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n) {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLong();
			}
			return arr;
		}

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

		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}
	}
}


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]);
		if(ans>=MOD)
			ans-=MOD;
		return memo2[idx][remCnt1][remCnt2][remSum] = ans;
	}

	private static int dp3(int cnt0, int cnt1, int cnt2, int last) {
		if (cnt0 < 0 || cnt1 < 0 || cnt2 < 0)
			return 0;
		if (cnt0 + cnt1 + cnt2 == 0)
			return 1;
		if (memo3[last][cnt0][cnt1][cnt2] != -1)
			return memo3[last][cnt0][cnt1][cnt2];
		long ans = 0;
		if (last != 0)
			ans += dp3(cnt0 - 1, cnt1, cnt2, 0);
		if (last != 1)
			ans += dp3(cnt0, cnt1 - 1, cnt2, 1);
		if (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();
		}

	}

}


// https://codeforces.com/problemset/problem/1113/A
import java.util.*;import java.io.*;import java.math.*;
public class Main
{

	public static void process()throws IOException
    {
    	int n=ni(),v=ni();
    	int cost=0;

    	if(v>=n-1){
    		pn(n-1);
    		return;
    	}

    	cost+=v;
    	for(int i=2;i<=n-v;i++)
    		cost+=i;
    	pn(cost);
    }

   	static AnotherReader sc;

    public static void main(String[]args)throws IOException
    {
    	boolean oj = System.getProperty("ONLINE_JUDGE") != null;
    	if(oj)
        	sc=new AnotherReader();
        else
        	sc=new AnotherReader(100);
        int t=1;
        while(t-->0)
            process();
        System.out.flush();
        System.out.close();
    }

    static void pn(Object o){System.out.println(o);}
    static void p(Object o){System.out.print(o);}
    static void pni(Object o){System.out.println(o);System.out.flush();}
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static double nd()throws IOException{return sc.nextDouble();}
    static String nln()throws IOException{return sc.nextLine();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
    static boolean multipleTC=false;

/////////////////////////////////////////////////////////////////////////////////////////////////////////

    static class AnotherReader{BufferedReader br; StringTokenizer st;
    AnotherReader()throws FileNotFoundException{
    br=new BufferedReader(new InputStreamReader(System.in));}
    AnotherReader(int a)throws FileNotFoundException{
    br = new BufferedReader(new FileReader("input.txt"));}
    String next()throws IOException{
    while (st == null || !st.hasMoreElements()) {try{
    st = new StringTokenizer(br.readLine());}
    catch (IOException  e){ e.printStackTrace(); }}
    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()); }
    String nextLine() throws IOException{ String str = ""; try{
    str = br.readLine();} catch (IOException e){
    e.printStackTrace();} return str;}}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


/*
ID: davidzh8
PROG: subset
LANG: JAVA
 */

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

public class villagemin {

    static long startTime = System.nanoTime();
    static ArrayList<Integer>[] adj;
    static int[] pos;
    public static void solve() throws Exception {
        boolean debug = (true);
        FastScanner sc = new FastScanner("villagemin.in", debug);
        FastWriter pw = new FastWriter("villagemin.out", debug);
        int N= sc.ni();
        adj= new ArrayList[N];
        pos= new int[N];
        for (int i = 0; i < N; i++) {
            pos[i]=i;
        }
        for(int i=0; i<N; i++){
            adj[i]= new ArrayList<>();
        }
        for (int i = 0; i < N-1; i++) {
            int a= sc.ni()-1;
            int b= sc.ni()-1;
            adj[a].add(b);
            adj[b].add(a);
        }
        dfs(0,-1);
        System.out.println(res);
        for (int i = 0; i < N; i++) {
            System.out.print(pos[i]+1+" ");
        }

        long endTime = System.nanoTime();
        //System.out.println("Execution Time: " + (endTime - startTime) / 1e9 + " s");
        pw.close();
    }
    static int res=0;
    static void dfs(int cur, int par){
        for(int next: adj[cur] ){
            if(next==par) continue;
            dfs(next, cur);
            if(pos[next]==next) {
                res += 2;
                int temp = pos[cur];
                pos[cur] = pos[next];
                pos[next] = temp;
            }
        }
        if(cur==0 && pos[cur]==0){
            int next= adj[0].get(0);
            res += 2;
            int temp = pos[cur];
            pos[cur] = pos[next];
            pos[next] = temp;
        }

    }

    static class Pair implements Comparable<Pair> {
        int a;
        int b;

        public Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int compareTo(Pair obj) {
            if (obj.a == a) {
                return b - obj.b;
            } else return a - obj.a;
        }
    }

    static class djset {
        int N;
        int[] parent;

        // Creates a disjoint set of size n (0, 1, ..., n-1)
        public djset(int n) {
            parent = new int[n];
            N = n;
            for (int i = 0; i < n; i++)
                parent[i] = i;
        }

        public int find(int v) {

            // I am the club president!!! (root of the tree)
            if (parent[v] == v) return v;

            // Find my parent's root.
            int res = find(parent[v]);

            // Attach me directly to the root of my tree.
            parent[v] = res;
            return res;
        }

        public boolean union(int v1, int v2) {

            // Find respective roots.
            int rootv1 = find(v1);
            int rootv2 = find(v2);

            // No union done, v1, v2 already together.
            if (rootv1 == rootv2) return false;

            // Attach tree of v2 to tree of v1.
            parent[rootv2] = rootv1;
            return true;
        }

    }

    static class FastScanner {
        InputStream dis;
        byte[] buffer = new byte[1 << 17];
        int pointer = 0;

        public FastScanner(String fileName, boolean debug) throws Exception {
            if (debug) dis = System.in;
            else dis = new FileInputStream(fileName);
        }

        int ni() throws Exception {
            int ret = 0;

            byte b;
            do {
                b = nextByte();
            } while (b <= ' ');
            boolean negative = false;
            if (b == '-') {
                negative = true;
                b = nextByte();
            }
            while (b >= '0' && b <= '9') {
                ret = 10 * ret + b - '0';
                b = nextByte();
            }

            return (negative) ? -ret : ret;
        }

        long nl() throws Exception {
            long ret = 0;

            byte b;
            do {
                b = nextByte();
            } while (b <= ' ');
            boolean negative = false;
            if (b == '-') {
                negative = true;
                b = nextByte();
            }
            while (b >= '0' && b <= '9') {
                ret = 10 * ret + b - '0';
                b = nextByte();
            }

            return (negative) ? -ret : ret;
        }

        byte nextByte() throws Exception {
            if (pointer == buffer.length) {
                dis.read(buffer, 0, buffer.length);
                pointer = 0;
            }
            return buffer[pointer++];
        }

        String next() throws Exception {
            StringBuffer ret = new StringBuffer();

            byte b;
            do {
                b = nextByte();
            } while (b <= ' ');
            while (b > ' ') {
                ret.appendCodePoint(b);
                b = nextByte();
            }

            return ret.toString();
        }
    }

    static class FastWriter {
        public PrintWriter pw;

        public FastWriter(String name, boolean debug) throws IOException {
            if (debug) {
                pw = new PrintWriter(System.out);
            } else {
                pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(name))));
            }
        }

        public void println(Object text) {
            pw.println(text);
        }

        public void print(Object text) {
            pw.print(text);
        }

        public void close() {
            pw.close();
        }

        public void flush() {
            pw.flush();
        }
    }

    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, () -> {
            try {
                solve();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "", 1 << 28);
        thread.start();
        thread.join();
    }

}


import java.util.Scanner;

public class KolyaAndTanya {
    public static void main(String[] args) {
        Scanner inputScanner = new Scanner(System.in);
        int n = inputScanner.nextInt();
        int bound = 1000000007;
        System.out.println((getBoundedPower(3, 3 * n, bound) - getBoundedPower(7, n, bound) + bound) % bound);
    }

    public static long getBoundedPower(int base, int power) {
        long res = 1;
        for (int i = 0; i < power; i++) {
            res = (res * base) % 1000000007;
        }
        return res ;
    }

    public static long getBoundedPower(int base, int power, int bound) {
        long ans = 1;
        long currentBase = base % bound;
        while (power > 0) {
            if (power % 2 == 1)
                ans = (ans * currentBase) % bound;
            currentBase = (currentBase * currentBase) % bound;
            power = power / 2;
        }
        return ans;
    }

}


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

public class Solution{
	InputStream is;
	static PrintWriter out;
	String INPUT = "";
	static long mod = (long)1e9+7L;

	public void solve(){
		long n = nl(), m = nl(), k = nl();
		long l = 1L, r = m*n;
		while(l < r){
			long mid = l+(r-l)/2L;
			if(f(n, m, mid, k)){
				r = mid;
			}
			else{
				l = mid+1L;
			}
		}

		out.println(l);
	}

	boolean f(long n, long m, long key, long k){
		long  res2 = 0;
		for(long i = 1 ; i <= n; i++){
			res2 = res2 + Math.min(m, key/i);
		}
		if(res2 >= k){
			return true;
		}
		else{
			return false;
		}
	}

	void run(){
		is = new DataInputStream(System.in);
		out = new PrintWriter(System.out);
		int t=1;while(t-->0)solve();
		out.flush();
	}
	public static void main(String[] args)throws Exception{new Solution().run();}
	long mod(long v, long m){if(v<0){long q=(Math.abs(v)+m-1L)/m;v=v+q*m;}return v%m;}
    long mod(long v){if(v<0){long q=(Math.abs(v)+mod-1L)/mod;v=v+q*mod;}return v%mod;}
	//Fast I/O code is copied from uwi code.
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	private int readByte(){
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	private String ns(){
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	private char[] ns(int n){
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	private char[][] nm(int n, int m){
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	private int[] na(int n){
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	private int ni(){
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	private long nl(){
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	static  int i(long x){return (int)Math.round(x);}
	static class Pair implements Comparable<Pair>{
		long fs,sc;
		Pair(long a,long b){
			fs=a;sc=b;
		}
		public int compareTo(Pair p){
			if(this.fs>p.fs)return 1;
			else if(this.fs<p.fs)return -1;
			else{
				return i(this.sc-p.sc);
			}
			//return i(this.sc-p.sc);
		}
		public String toString(){
			return "("+fs+","+sc+")";
		}
	}

}


import java.util.Arrays;
import java.util.Scanner;

public class a {

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt() * 2;
        int[] f = new int[10];
        for (int i = 0; i < 4; i++)
        {
            String line = in.next();
            for (int j = 0; j < 4; j++)
            {
                if (line.charAt(j) == '.')
                    continue;
                f[line.charAt(j) - '0']++;
            }
        }

        Arrays.sort(f);
        System.out.println(f[9] > n ? "NO" : "YES");

    }

}


import java.util.Scanner;

public class A {
	public static void main(String args[]){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();String s= "";
		int t = in.nextInt();
		if(t==10){
			for(int i = 0 ; i < n-1 ; i++)
				s+="1";
			s+="0";
		}else{
			for(int i = 0 ; i < n ; i++)
				s+=t;
		}
		if(s.equals("0"))
			s = "-1";
		System.out.println(s);
	}
}


import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;
import java.math.BigInteger;
import java.io.InputStream;

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

class TaskF {
	int[] first;
	int[] second;
	int[] cost;

	public void solve(int testNumber, InputReader in, OutputWriter out) {
		int count = in.readInt();
		int edgeCount = in.readInt();
		int index = in.readInt() - 1;
		first = new int[edgeCount];
		second = new int[edgeCount];
		cost = new int[edgeCount];
		IOUtils.readIntArrays(in, first, second, cost);
		NavigableSet<State> set = new TreeSet<State>();
		set.add(new State(0, 0, 0, 0, 0));
		for (int i = 0; i < index; i++) {
			State current = set.pollFirst();
			for (int j = 0; j < edgeCount; j++) {
				State next = current.add(j);
				if (next != null)
					set.add(next);
			}
		}
		out.printLine(set.first().cost);
	}

	class State implements Comparable<State> {
		final int menMask;
		final int womenMask;
		final long firstEdgeMask;
		final long secondEdgeMask;
		final int cost;

		State(int menMask, int womenMask, long firstEdgeMask, long secondEdgeMask, int cost) {
			this.menMask = menMask;
			this.womenMask = womenMask;
			this.firstEdgeMask = firstEdgeMask;
			this.secondEdgeMask = secondEdgeMask;
			this.cost = cost;
		}

		public int compareTo(State o) {
			if (cost != o.cost)
				return cost - o.cost;
			int value = IntegerUtils.longCompare(firstEdgeMask, o.firstEdgeMask);
			if (value != 0)
				return value;
			return IntegerUtils.longCompare(secondEdgeMask, o.secondEdgeMask);
		}

		public State add(int edge) {
			if ((menMask >> first[edge] & 1) != 0 || (womenMask >> second[edge] & 1) != 0)
				return null;
			return new State(menMask + (1 << first[edge]), womenMask + (1 << second[edge]),
				firstEdgeMask + (edge < 64 ? 1L << edge : 0), secondEdgeMask + (edge >= 64 ? 1L << (edge - 64) : 0),
				cost + TaskF.this.cost[edge]);
		}
	}
}

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() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			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();
		}
		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 OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(outputStream);
	}

	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}

	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}

	public void close() {
		writer.close();
	}

	}

class IOUtils {

	public static void readIntArrays(InputReader in, int[]... arrays) {
		for (int i = 0; i < arrays[0].length; i++) {
			for (int j = 0; j < arrays.length; j++)
				arrays[j][i] = in.readInt();
		}
	}

	}

class IntegerUtils {

	public static int longCompare(long a, long b) {
		if (a < b)
			return -1;
		if (a > b)
			return 1;
		return 0;
	}

	}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
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;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskC {
		char[][] ans = new char[2][13];

		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			String s = in.next();
			int p1 = -1;
			int p2 = -1;
			for (int i = 0; i < s.length(); i++) {
				for (int j = i + 1; j < s.length(); j++) {
					if (s.charAt(i) == s.charAt(j)) {
						p1 = i;
						p2 = j;
					}
				}
			}
			int len = p2 - p1 - 1;
			if (len == 0) {
				out.println("Impossible");
				return;
			}

			Arrays.fill(ans[0], '.');
			Arrays.fill(ans[1], '.');
			if (len == 1) {
				goLeft(0, 1, s.substring(p1, p2));
			} else {
				goLeft(0, len / 2, s.substring(p1, p2));
			}
			String u = rev(s.substring(0, p1));
			String v = s.substring(p2 + 1);
			if (len / 2 + 1 >= ans[0].length && !u.isEmpty()) {
				String t = u;
				u = v;
				v = t;
			}
			if (len == 1) {
				goRight(0, 2, u);
				goRight(1, 0, v);
			} else {
				goRight(0, len / 2 + 1, u);
				goRight(1, (len + 1) / 2, v);
			}
			out.println(new String(ans[0]));
			out.println(new String(ans[1]));
		}

		private String rev(String s) {
			return new StringBuilder(s).reverse().toString();
		}

		private void goLeft(int r, int c, String s) {
			if (s.isEmpty()) {
				return;
			}
			if (ans[r][c] != '.') {
				throw new AssertionError();
			}
			ans[r][c] = s.charAt(0);
			if (c > 0) {
				goLeft(r, c - 1, s.substring(1));
			} else {
				goRight(1 - r, c, s.substring(1));
			}
		}

		private void goRight(int r, int c, String s) {
			if (s.isEmpty()) {
				return;
			}
			if (ans[r][c] != '.') {
				throw new AssertionError();
			}
			ans[r][c] = s.charAt(0);
			if (c + 1 < ans[r].length) {
				goRight(r, c + 1, s.substring(1));
			} else {
				goLeft(1 - r, c, s.substring(1));
			}
		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					String rl = in.readLine();
					if (rl == null) {
						return null;
					}
					st = new StringTokenizer(rl);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

	}
}


import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        int x1=sc.nextInt(),y1=sc.nextInt();
        int x2=sc.nextInt(),y2=sc.nextInt();
        int r1=0,r2=0;
        r1+=Math.abs(((x1+y1)/(2*a))-((x2+y2)/(2*a)));
        if(((x1+y1)*1.0)/(x2+y2)<0) {
            r1++;
        }
        r2+=Math.abs(((x1-y1)/(2*b))-((x2-y2)/(2*b)));
        if(((x1-y1)*1.0)/(x2-y2)<0){
            r2++;
        }
        System.out.println(Math.max(r1,r2));
    }
}


import java.util.Scanner;

public class BeautifulYear {

	public static void main(String [] args)
	{
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int m = sc.nextInt();

		int inter = n*m;
		int pas=0;

		while(inter>=0 && n>=0 && m>=0)
		{
			inter--;
			n--;
			m--;
			pas++;
		}
		if(pas%2 == 0) System.out.println("Akshat");
		else System.out.println("Malvika");

	}
}


import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class palindromeDance {
    static int n, a, b;
    static int[] input;
    static ArrayList<Integer> unbought;
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

        StringTokenizer st = new StringTokenizer(br.readLine());

         n = Integer.parseInt(st.nextToken());
         a = Integer.parseInt(st.nextToken());
         b = Integer.parseInt(st.nextToken());

        input = new int[n];

        st = new StringTokenizer(br.readLine());

        unbought = new ArrayList<Integer>();

        for(int i = 0; i < n; i++){
            input[i] = Integer.parseInt(st.nextToken());
            if(input[i] == 2){
                unbought.add(i);
            }
        }

 boolean alreadyComplete = true;

        for(int i = 0; i < n; i++){
            if(input[i] == 2){
                alreadyComplete = false;
            }
        }

         if(alreadyComplete && isPalindrome(input)){
            out.println(0);
            out.close();
        }
        else if(alreadyComplete && !isPalindrome(input)){
            out.println(-1);
            out.close();
        }
        else if(!alreadyComplete) {

            int n2 = Math.min(solve(0, a, input, 0), solve(0, b, input, 1));

            if (n2 == Integer.MAX_VALUE) {
                out.println(-1);
            } else {
                out.println(n2);
            }

            out.close();
        }

    }

    public static int solve(int pos, int cost, int[] list, int suit){

        list[unbought.get(pos)] = suit;

        if(pos == unbought.size()-1 && isPalindrome(list)){
            return cost;
        }
        //if is not palindrome
        else if(pos == unbought.size()-1){
            return Integer.MAX_VALUE;
        }

        return Math.min(solve(pos+1, cost+a, list, 0), solve(pos+1, cost+b, list, 1));
    }

    public static boolean isPalindrome(int[] list){
        int rightPointer = list.length-1;

        for(int i = 0; i < list.length/2; i++){
            if(list[i] != list[rightPointer]){
                return false;
            }
            rightPointer--;
        }

        return true;
    }

}


import java.util.Scanner;

public class Brain_Photo {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		sc.nextLine();

		boolean color = false;
		for(int i = 0;i<n;i++)
		{
			String arr[] = sc.nextLine().split(" ");
			for(int j = 0;j<m;j++)
			{
				char ch = arr[j].charAt(0);
				if(ch == 'C' || ch == 'M' || ch == 'Y')
					color = true;
			}
		}

		if(color)
			System.out.println("#Color");
		else
			System.out.println("#Black&White");

		sc.close();

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigInteger;
import java.util.Random;
import java.util.StringTokenizer;

public class D {
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();

        long a1 = sc.nextLong();
        long b1 = sc.nextLong();
        long a2 = sc.nextLong();
        long b2 = sc.nextLong();
        long left = sc.nextLong();
        long right = sc.nextLong();

        long lcm = lcm(a1, a2);
        left = Math.max(left, Math.max(b1, b2));

        // a1*x + b1 = a2*y + b2
        // a1*x - a2*y = b1 - b2
        long[] x_y_gcd = solveDiophantine(a1, -a2, b2 - b1);
        if (x_y_gcd == null) {
            System.out.println(0);
        } else {
            BigInteger a1Bi = BigInteger.valueOf(a1);
            BigInteger b1Bi = BigInteger.valueOf(b1);
            BigInteger leftBi = BigInteger.valueOf(left);
            BigInteger rightBi = BigInteger.valueOf(right);
            BigInteger lcmBi = BigInteger.valueOf(lcm);

            BigInteger firstEq = a1Bi.multiply(BigInteger.valueOf(x_y_gcd[0])).add(b1Bi);
            BigInteger diff = leftBi.subtract(firstEq).divide(lcmBi).subtract(BigInteger.ONE);
            firstEq = firstEq.add(diff.multiply(lcmBi));
            while (firstEq.compareTo(leftBi) < 0) { // Might only happen once or twice
                firstEq = firstEq.add(lcmBi);
            }
            if (rightBi.compareTo(firstEq) < 0) {
                System.out.println(0);
            } else {
                long count = Math.max(rightBi.subtract(firstEq).divide(lcmBi).longValue() + 1, 0);
                System.out.println(count);
            }
        }
    }

    /**
     * Finds a single solution {x, y} to the LDE a*x + b*y = c.
     * Outputs an array of the form {x, y, d}, where d = GCD(a,b).
     */
    private static long[] solveDiophantine(long a, long b, long c) {
        long[] e = extEuclid(a, b);
        long k = c / e[2];

        //c not divisible by the GCD(a,b) -> no solution
        if (c - k * e[2] != 0)
            return null;

        long[] output = {e[0] * k, e[1] * k, e[2]};
        return output;
    }

    /**
     * Extended Euclidean Algorithm finds {x, y, d}
     * where d=GCD(a,b) and x and y satisfy a*x + b*y = d
     * The output is an array of the form {x, y, d}.
     */
    private static long[] extEuclid(long a, long b) {
        long s0 = 1, s1 = 0, sTemp;
        long t0 = 0, t1 = 1, tTemp;
        long r0 = a, r1 = b, rTemp;
        long q;

        while (r1 != 0) {
            q = r0 / r1;

            rTemp = r1;
            r1 = r0 - q * r1;
            r0 = rTemp;

            sTemp = s1;
            s1 = s0 - q * s1;
            s0 = sTemp;

            tTemp = t1;
            t1 = t0 - q * t1;
            t0 = tTemp;
        }

        long[] output = {s0, t0, r0};
        return output;
    }

    static long lcm(long a, long b) {
        return a / gcd(a, b) * b;
    }

    static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }

        public FastScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

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

        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readIntArray(int n) {
            int[] a = new int[n];
            for (int idx = 0; idx < n; idx++) {
                a[idx] = nextInt();
            }
            return a;
        }

        long[] readLongArray(int n) {
            long[] a = new long[n];
            for (int idx = 0; idx < n; idx++) {
                a[idx] = nextLong();
            }
            return a;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;

public final class Main
{
    static class Cup
    {
        long c, w;

        Cup(long c, long w) {
            this.c = c;
            this.w = w;
        }

        static Comparator<Cup> cupComparator = new Comparator<Cup>() {
            @Override
            public int compare(Cup o1, Cup o2) {
                if (o1.c != o2.c)
                    return - Long.compare(o1.c, o2.c);
                return Long.compare(o1.w, o2.w);
            }
        };
    }

    static class FastReader
    {
        private BufferedReader br;
        private StringTokenizer st;

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

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

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

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    private static long ans = 0;

    public static void main(String[] args)
    {
        FastReader fastReader = new FastReader();
        int n = fastReader.nextInt();
        int m = fastReader.nextInt();
        long d = fastReader.nextInt();
        ArrayList<Cup> cupsP = new ArrayList<>();
        ArrayList<Cup> cupsI = new ArrayList<>();

        for(int i = 0; i < n; i ++)
        {
            long c = fastReader.nextInt();
            long w = fastReader.nextInt();
            cupsP.add(new Cup(c, w));
        }

        long sumCI = 0, sumWI = 0;
        for(int i = 0; i < m; i ++)
        {
            long c = fastReader.nextInt();
            long w = fastReader.nextInt();
            sumCI += c; sumWI += w;
            cupsI.add(new Cup(c, w));
        }

        Collections.sort(cupsP, Cup.cupComparator);
        Collections.sort(cupsI, Cup.cupComparator);

        int idI = m - 1;
        long sumCP = 0, sumWP = 0;
        for(int idP = 0; idP < n; idP ++)
        {
            sumCP += cupsP.get(idP).c;
            sumWP += cupsP.get(idP).w;
            while (sumWP + sumWI > d && idI >= 0)
            {
                sumCI -= cupsI.get(idI).c;
                sumWI -= cupsI.get(idI).w;
                idI --;
            }
            if (sumWP + sumWI <= d && idI >= 0 && ans < sumCI + sumCP) {
                ans = sumCI + sumCP;
            }
        }

        System.out.print(ans);
    }
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.StringTokenizer;

public class r58 {
	static StringTokenizer st;
	static BufferedReader br;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		Locale.setDefault(Locale.US);
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
				System.out)));

		int a1=nextInt(),a2=nextInt(),b1=nextInt(),b2=nextInt();
			if((a1>=b2&&a1-b2==1)||(a2>=b1&&a2-b1==1))
				pw.println("YES");
			else
				if((b1>=a2&&(b1+1)/2-1<=a2)||(b2>=a1&&(b2+1)/2-1<=a1))
					pw.print("YES");
			else
			pw.print("NO");
		pw.close();
	}

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

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

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

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


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

public class A346 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt();
		int a = scan.nextInt();
		int b = scan.nextInt();

		if (Math.abs(b) > n) {
			if (b > 0) {
				b = b%n;
			}
			else if (b < 0) {
				b = Math.abs(b)%n;
				b *= -1;
			}
		}
		if (b > 0) {
			int ans = (a+b)%n;
			if (ans == 0) {
				ans = n;
			}
			System.out.println(ans);
		} else if (b < 0) {
			int newB = (b+a);
			if (newB > 0) {
				System.out.println(newB);
			}
			else {
				int ans = n - Math.abs(newB);
				System.out.println(ans);
			}
		} else {
			System.out.println(a);
		}
	}
}


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

public class Emperor implements Runnable {
    static class Side implements Comparable<Side> {
        int dx;
        int dy;
        double alpha;
        Side next;

        public int compareTo(Side side) {
            return Double.compare(alpha, side.alpha);
        }

        public void getAlpha() {
            alpha = Math.atan2(dy, dx);
        }
    }

    static final int MAX = 500;
    static final int MAGIC = 7;
    List<Side> res;
    int tdx;
    int tdy;

    private void solve() throws IOException {
        int n = nextInt();
        Side[] first = new Side[MAX * MAX + 1];
        for (int dx = -MAX; dx <= MAX; ++dx)
            for (int dy = -MAX; dy <= MAX; ++dy) {
                int z = dx * dx + dy * dy;
                if (z <= MAX * MAX) {
                    Side s = new Side();
                    s.dx = dx;
                    s.dy = dy;
                    s.next = first[z];
                    first[z] = s;
                }
            }
        int total = 0;
        List<Side> firsts = new ArrayList<Side>();
        for (int len = 1;; ++len) {
            if (first[len] == null)
                continue;
            ++total;
            firsts.add(first[len]);
            if (total < n)
                continue;
            if (doit(firsts, n)) {
                System.err.println(n + " " + total);
                break;
            }
        }
        for (Side s : res)
            s.getAlpha();
        Collections.sort(res);
        int x = 0;
        int y = 0;
        writer.println("YES");
        for (Side s : res) {
            writer.println(x + " " + y);
            x += s.dx;
            y += s.dy;
        }
    }

    private boolean doit(List<Side> firsts, int n) {
        res = new ArrayList<Side>();
        tdx = 0;
        tdy = 0;
        return rec(firsts, n, firsts.size() - 1);
    }

    private boolean rec(List<Side> firsts, int toMake, int at) {
        if (toMake == 0) {
            return (tdx == 0 && tdy == 0);
        }
        if (at < 0) {
            throw new RuntimeException();
        }
        if (at >= MAGIC) {
            Side best = firsts.get(at);
            int bdist = (tdx + best.dx) * (tdx + best.dx) + (tdy + best.dy) * (tdy + best.dy);
            Side cur = best.next;
            while (cur != null) {
                int cdist = (tdx + cur.dx) * (tdx + cur.dx) + (tdy + cur.dy) * (tdy + cur.dy);
                if (cdist < bdist) {
                    bdist = cdist;
                    best = cur;
                }
                cur = cur.next;
            }
            res.add(best);
            tdx += best.dx;
            tdy += best.dy;
            if (rec(firsts, toMake - 1, at - 1))
                return true;
            res.remove(res.size() - 1);
            tdx -= best.dx;
            tdy -= best.dy;
        } else {
            Side best = firsts.get(at);
            while (best != null) {
                res.add(best);
                tdx += best.dx;
                tdy += best.dy;
                if (rec(firsts, toMake - 1, at - 1))
                    return true;
                res.remove(res.size() - 1);
                tdx -= best.dx;
                tdy -= best.dy;
                best = best.next;
            }
            if (at >= toMake)
                if (rec(firsts, toMake, at - 1))
                    return true;
        }
        return false;
    }

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

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter writer;

    public void run() {
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

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

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

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

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


import java.util.Scanner;
public class CarrotCakes {

    public static void main(String[] args) {
        Scanner input =new Scanner(System.in);
        int n   =  input.nextInt();
        int t   =  input.nextInt();
        int k   =  input.nextInt();
        int d   =  input.nextInt();

        int time1=0;
        int tot1 = 0;

        for(int i = 1;tot1!=n&&tot1<=n;i++){
            time1=time1+t;
            tot1 = tot1 + k;
        }

      //   System.out.println();
      //   System.out.println("total    "+tot1);
       //  System.out.println("time     "+time1);

        int time2=0;
        int tot2 = 0;
        int f = 0;
        int ww =0;
        int oo = 0;
        for(int i = 1;true;i++){
          if(tot2>=n){break;}
          ww++;
          f++;
//          time2++;
          if(ww==t){
              tot2 = tot2+ k;
              ww=0;
          }
          if(f>d ){
           oo++;
          }
          if(oo==t){
              tot2 = tot2+ k;
              oo=0;
          }
          time2++;
    }

     //   System.out.println();
      //  System.out.println("total     "+tot2);
       // System.out.println("time      "+time2);

         if(time2<time1){System.out.println("YES");}
         else{System.out.println("NO");}

    }
}


import org.omg.CORBA.ARG_OUT;

import java.lang.reflect.Array;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: piyushd
 * Date: 3/26/11
 * Time: 10:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class TaskC {

    int[] senior;

    long totalArrangements(int[] a) {
        int N = a.length;
        long[] dp = new long[1<<N];
        dp[0] = 1;
        for(int mask = 0; mask < (1<<N); mask++) {
            if(dp[mask] == 0) continue;

            int i = Integer.bitCount(mask);
            for(int j = 0; j < N; j++) {
                if(a[j] == -1 || a[j] == i) {
                    if((mask & (1<<j)) == 0) {
                        if((mask & senior[j]) == senior[j]) {
                           dp[mask | (1<<j)]  += dp[mask];
                        }
                    }
                }
            }
        }
        return dp[(1<<N) - 1];
    }

    void run(){
        int N = nextInt();
        long year = nextLong() - 2001;
        int M = nextInt();
        senior = new int[N];
        for(int i = 0; i < M; i++) {
            int a = nextInt() - 1;
            int b = nextInt() - 1;
            senior[b] |= (1<<a);
        }

        int[] ans = new int[N];
        Arrays.fill(ans, -1);

        for(int i = 0; i < N; i++) {
            while(true) {
                ans[i]++;
                if(ans[i] == N) {
                    System.out.println("The times have changed");
                    return;
                }

                long ways = totalArrangements(ans);
                if(year < ways) {
                    break;
                }
                else {
                    year -= ways;
                }
            }
        }

        for(int x : ans) System.out.print((x + 1) + " ");
        System.out.println();
    }

    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 TaskC().run();
    }
}


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

public class btest9
{
	public static void main(String[] args) throws Exception
	{
		InputReader in = new InputReader(System.in);
		OutputWriter out = new OutputWriter(System.out);
		int n=in.readInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)a[i]=in.readInt();
		int k=in.readInt();
		Arrays.sort(a);
		int lo,hi,mid;
		int fi=0;
		int ans=0;
		int max=-5;
		for(int i=0;i<n;i++)
		{
			lo=i+1;hi=n-1;fi=a[i]+k;
			while(lo<hi)
			{
				mid=lo+(hi-lo+1)/2;
				if(a[mid]>fi)hi=mid-1;
				else lo=mid;
			}
			if(lo<n&&a[lo]<=fi)ans=lo-i+1;
			else ans=1;
			if(ans>max)max=ans;
		}
		out.printLine(max);
		out.close();
	}

	private static class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;

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

		public int read()
		{
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = stream.read(buf);
				} catch (IOException e)
				{
					throw new InputMismatchException();
				}
				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();
			}
			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 long readLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long 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 String readString()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter
		{
			public boolean isSpaceChar(int ch);
		}
	}

	private static class OutputWriter
	{
		private final PrintWriter writer;

		public OutputWriter(OutputStream outputStream)
		{
			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
		}

		public OutputWriter(Writer writer)
		{
			this.writer = new PrintWriter(writer);
		}
 		public void print(Object... objects)
		{
			for (int i = 0; i < objects.length; i++)
			{
				if (i != 0)
				writer.print(' ');
				writer.print(objects[i]);
			}
		}

		public void printf(double n)
		{
			writer.printf("%.4f%n", n);
		}
		public void printLine(Object... objects)
		{
			print(objects);
			writer.println();
		}

		public void close()
		{
			writer.close();
		}

		public void flush()
		{
			writer.flush();
		}

	}
}


import java.util.*;
public class QAQ {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner as = new Scanner (System.in);
        Vector as1 = new Vector ();
        String qaq;
        int dd=0;
        qaq = as.next();

        String [] qaq1 = qaq.split("");
       // System.out.println(Arrays.toString(qaq1));
        for (int i =0;i<qaq1.length;i++){
        if(qaq1[i].equalsIgnoreCase("A") || qaq1[i].equalsIgnoreCase("Q") ){
        as1.add(qaq1[i]);

        }

        }
        //System.out.println(as1);
        for (int i =0; i<as1.size();i++){
            if(as1.get(i).equals("Q")){
                for (int z=i; z<as1.size();z++){

                if(as1.get(z).equals("A")){
                for(int c=z; c<as1.size();c++){
                if(as1.get(c).equals("Q")){
                dd++;
                }

                }

                }

                }

            }

        }
        System.out.println(dd);
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * Created by Sergey on 19.09.2014.
 */
public class Main {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        BigInteger a = new BigInteger(in.next());
        String s17 = "100000000000000000";
        BigInteger ost = BigInteger.valueOf(45).multiply(BigInteger.valueOf(18)).multiply(new BigInteger(s17));
        ost = ost.add(BigInteger.ONE);
        ost = ost.mod(a);

        BigInteger L = BigInteger.ONE;
        BigInteger R = new BigInteger(s17 + "0");

        BigInteger add = a.subtract(ost);
        add = add.mod(a);

        L = L.add(add);
        R = R.add(add);

        System.out.println(L + " " + R);
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

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


import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;

// Please name your class Main
public class Main {
    static FastScanner fs=new FastScanner();
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        public String next() {
            while (!st.hasMoreElements())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        int Int() {
            return Integer.parseInt(next());
        }

        long Long() {
            return Long.parseLong(next());
        }

        String Str(){
            return next();
        }
    }

    public static void main (String[] args) throws java.lang.Exception {
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        //reading /writing file
        //Scanner in=new Scanner(System.in);
        //Scanner in=new Scanner(new File("input.txt"));
        //PrintWriter pr=new PrintWriter("output.txt");

        int T=1;
        for(int t=0;t<T;t++){
            Solution sol1=new Solution(out,fs);
            sol1.solution(T,t);
        }
        out.flush();

    }

    public static int[] Arr(int n){
        int A[]=new int[n];
        for(int i=0;i<n;i++)A[i]=Int();
        return A;
    }
    public static int Int(){
        return fs.Int();
    }
    public static long Long(){
        return fs.Long();
    }
    public static String Str(){
        return fs.Str();
    }

}

class Solution {
    PrintWriter out;
    int INF = Integer.MAX_VALUE;
    int NINF = Integer.MIN_VALUE;
    int MOD = 998244353;
    int mod = 1000000007;
    Main.FastScanner fs;

    public Solution(PrintWriter out, Main.FastScanner fs) {
        this.out = out;
        this.fs = fs;

    }

    public void solution(int all, int testcase) {
        int n = fs.Int();
        mod = fs.Int();

        long dp[][] = new long[n][n * n + 5];
        dp[0][0] = 1;
        for(int i = 1; i < n; i++) {
            for(int j = 0; j < dp[0].length; j++) {
                for(int cnt = 0; cnt <= i; cnt++){
                    if(j - cnt >= 0) {
                        dp[i][j] += dp[i - 1][j - cnt];
                        dp[i][j] %= mod;
                        if(dp[i][j] == 0){
                            dp[i][j] += mod;
                        }
                    }
                }
            }
        }

        long sums[][] = new long[dp.length][dp[0].length];
        for(int i = 0; i < dp.length;i++){
            int sum = 0;
            for(int j = 0; j < dp[0].length; j++){
                sum += dp[i][j];
                sum %= mod;
                if(sum == 0) sum += mod;
                sums[i][j] = sum;
            }
        }

        //init
        long fact[] = new long[501 * 501];
        fact[0] = fact[1] = 1;
        for(int i = 2; i < fact.length;i++){
            fact[i] = fact[i - 1] * i;
            fact[i] %= mod;
        }
        long ncr[][] = new long[501][501];
        ncr[0][0]=1;
        for (int i=1;i<ncr.length;i++) {
            ncr[i][0]=1;
            for (int j=1;j<ncr[0].length;j++) {
                ncr[i][j]=(ncr[i-1][j] + ncr[i-1][j-1])%mod;
                if(ncr[i][j] == 0)ncr[i][j] += mod;
            }
        }

        long res = 0;
        for(int i = n - 2; i >= 0; i--) {
            int cnt = n - i;
            int pre = n - cnt;
            long com = (ncr[n][pre] * fact[pre]) % mod;
            if(com == 0) com += mod;
            long sum = 0;

            long last[] = dp[cnt - 2];
            long ssum = 0;
            long total =0;
            for(int j = 2; j < dp[0].length; j++) { // j == inversions
                total += sums[cnt - 2][j - 2] * (cnt - 1);
                total %= mod;
                if(total == 0) total += mod;

                sum += last[j] * total;
                sum %= mod;
                if(sum == 0) sum += mod;

                ssum += sums[cnt - 2][j - 2];
                if(j - cnt - 1 >= 0) {
                    ssum -= sums[cnt - 2][j - cnt - 1];
                }
                ssum += mod;
                ssum %= mod;
                if(ssum == 0) ssum += mod;

                total -= ssum;
                total += mod;
                total %= mod;
                if(total == 0)total += mod;
            }
            res += sum * com;
            res%=mod;
            if(res == 0) res += mod;
        }
        out.println(res % mod);
    }
}


import java.util.Scanner;
public class abdo  {
public static void main(String[] args){
Scanner abd = new Scanner(System.in);
        String a=abd.next();
        String s=abd.next();
        int q=a.length(),k=0,z=0;
        int w=s.length();
        char[]x=new char[q+w];
        int f=0;
        for(int i=0;i<q;i++){
            x[i]=a.charAt(i);
            if(x[i]=='|'){
               k=q-(i+1);      //right
               z=q-(k+1);      //left
               f++;
            }
            if(f>0)
                break;
        }
        for(int i=0;i<w;i++){
            x[i]=s.charAt(i);
        }
//        System.out.println(z+" "+k);
         if(k<z){

             if(w==z-k){
                 System.out.println(a+s);
                 return;
             }
            else if(w-(z-k)>0){
              if((w-(z-k))%2==0){
                 for(int i=0;i<(w-(z-k))/2;i++){
                  System.out.print(x[i]);
                 }

                 System.out.print(a);
                 for(int i=(w-(z-k))/2;i<w;i++){
                     System.out.print(x[i]);
                 }
             }
              else{
                  System.out.println("Impossible");
                   return;
              }
            }
             else{
                  System.out.println("Impossible");
                 return;
             }
         }
         else if(z<k){

             if(w==k-z){
                 System.out.println(s+a);
                 return;
             }
            else if((w-(k-z))>0){
              if((w-(k-z))%2==0){
                 for(int i=(w-(k-z))/2;i<w;i++){
                     System.out.print(x[i]);
                 }
                 System.out.print(a);
                 for(int i=0;i<(w-(k-z))/2;i++){
                  System.out.print(x[i]);
                 }
             }
              else{
                  System.out.println("Impossible");
                 return;
             }
            }
              else{
                  System.out.println("Impossible");
                 return;
             }

         }
         else{
             if(w%2==0){
                 for(int i=0;i<(w/2);i++){
                  System.out.print(x[i]);
                 }
                 System.out.print(a);
                 for(int i=(w/2);i<w;i++){
                  System.out.print(x[i]);
                 }
             }
             else{
                  System.out.println("Impossible");
                 return;
             }
         }
}}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedOutputStream;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;

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

    static class TaskE {
        TaskE.GCDResult gcdExt(final long a, final long b) {
            TaskE.GCDResult result;
            if (a == 0) {
                result = new TaskE.GCDResult();
                result.d = b;
                result.x = 0;
                result.y = 1;

                return result;
            }
            result = gcdExt(b % a, a);
            long t = result.x;
            result.x = result.y - (b / a) * t;
            result.y = t;

            return result;
        }

        public void solve(int testNumber, FastScanner in, FastWriter out) {
            long n = in.nextInt();
            long m = in.nextInt();
            long x0 = in.nextInt();
            long y0 = in.nextInt();
            int vx = in.nextInt();
            int vy = in.nextInt();

            long x;
            long y;
            if (vx == 0 || vy == 0) {

                if (x0 == 0) {
                    x = 0;
                    if (vx != 0) {
                        out.println(-1);
                        return;
                    }
                    if (vy > 0) {
                        y = m;
                    } else {
                        y = 0;
                    }
                } else if (x0 == n) {
                    x = n;
                    if (vx != 0) {
                        out.println(-1);
                        return;
                    }
                    if (vy > 0) {
                        y = m;
                    } else {
                        y = 0;
                    }
                } else if (y0 == 0) {
                    y = 0;
                    if (vy != 0) {
                        out.println(-1);
                        return;
                    }
                    if (vx > 0) {
                        x = n;
                    } else {
                        x = 0;
                    }
                } else if (y0 == m) {
                    y = m;
                    if (vy != 0) {
                        out.println(-1);
                        return;
                    }
                    if (vx > 0) {
                        x = n;
                    } else {
                        x = 0;
                    }
                } else {
                    out.println(-1);
                    return;
                }

                out.println(x + " " + y);
            } else {

                if (vx < 0) x0 = n - x0;
                if (vy < 0) y0 = m - y0;

                //vx = 1, vy = 1
                long c = x0 - y0;
                TaskE.GCDResult g = gcdExt(n, m);
                if (c % g.d == 0) {
                    c /= g.d;

                    x = g.x * c;
                    y = g.y * c;

                    long dx = m / g.d;
                    long dy = n / g.d;
                    if (x < 0) {
                        long d = (-x) / dx;
                        if (-x % dx > 0) d++;

                        x += d * dx;
                        y -= d * dy;
                    }

                    if (y > 0) {
                        long d = y / dy;
                        if (y % dy > 0) d++;

                        x += d * dx;
                        y -= d * dy;
                    }
                    if (x > 0 && y < 0) {
                        long d1 = x / dx;
                        long d2 = -y / dy;
                        long d = Math.min(d1, d2);
                        if (d > 0) {
                            x += d * dx;
                            y -= d * dy;
                        }
                    }

                    if (x == 0 && y == 0) {
                        x += dx;
                        y -= dy;
                    }

                    x = n * (x % 2);
                    y = m * (-y % 2);

                    if (vx < 0) x = n - x;
                    if (vy < 0) y = m - y;
                    out.println(x + " " + y);
                } else {
                    out.println(-1);
                }
            }
        }

        static class GCDResult {
            long x;
            long y;
            long d;

            public String toString() {
                return "GCDResult{" +
                        "x=" + x +
                        ", y=" + y +
                        ", d=" + d +
                        '}';
            }

        }

    }

    static class FastScanner {
        static final int BUFFER_SIZE = 1 << 18;
        final BufferedReader input;
        StringTokenizer buffer;

        public FastScanner(InputStream inputStream) {
            input = new BufferedReader(new InputStreamReader(inputStream), BUFFER_SIZE);
        }

        private String readToken() {
            try {
                if (buffer == null || !buffer.hasMoreTokens()) {
                    buffer = new StringTokenizer(input.readLine());
                }
                return buffer.nextToken();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

        public int nextInt() {
            return Integer.parseInt(readToken());
        }

    }

    static class FastWriter extends PrintWriter {
        static final int BUFFER_SIZE = 1 << 18;

        public FastWriter(OutputStream out) {
            super(new BufferedOutputStream(out, BUFFER_SIZE));
        }

        public FastWriter(Writer out) {
            super(out);
        }

        public void close() {
            super.close();
        }

    }
}


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

public class Getting_an_A {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        double n=Double.parseDouble(br.readLine());
        String str[]=br.readLine().split(" ");
        double a[]=new double[(int) n];
        double sum=0;
        for(int i=0;i<n;i++) {
            a[i] = Double.parseDouble(str[i]);
            sum+=a[i];
        }
        Arrays.sort(a);
        long count=0;
        if(sum/n>=4.5)
            System.out.println("0");
        else
        {
            long value=round((n*4.5)-sum);
            for(int i = 0; i<n; i++)
            {
                if(value<=0)
                    break;
                long diff= (long) (5-a[i]);
                if(diff>0)
                {
                    value-=diff;
                    count++;
                }

            }
            System.out.println(count);
        }
    }
    private static int  round(double d) {
        double dAbs = Math.abs(d);
        int i = (int) dAbs;
        double result = dAbs - (double) i;
        if (result < 0.5) {
            return d < 0 ? -i : i;
        } else {
            return d < 0 ? -(i + 1) : i + 1;
        }
    }
}


import java.io.*;

public class IlyaBank {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String line = in.readLine();
		int num = Integer.parseInt(line);
		if (Integer.parseInt(line) < 0) {
			if (line.charAt(line.length() - 1) >= line.charAt(line.length() - 2)) {
				num /= 10;
			} else {
				int add = Character.getNumericValue(line.charAt(line.length() - 1));
				num /= 100;
				num *= 10;
				num -= add;
			}
		}
		System.out.println(num);
	}
}