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

public class Postcard {

    static void print(char[] arr) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < arr.length; j++) {
            if (arr[j] != '?' && arr[j] != '*' && arr[j] != 'A') {
                sb.append(arr[j]);
            }
        }
        System.out.println(sb);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char arr[] = in.next().toCharArray();
        int num = in.nextInt();
        int cane = 0, snow = 0, size = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == '*') {
                snow++;
            } else if (arr[i] == '?') {
                cane++;
            } else {
                size++;
            }
        }
        if(size<num&&snow==0){
            System.out.println("Impossible");
        }else if (size == num) {
            print(arr);
            //////////////////////////////////////////////////////////////
        } else if (num > size && snow > 0) {
            StringBuilder sb = new StringBuilder();
            int diff=num-size;
            for (int j = 0; j < arr.length; j++) {
                if(arr[j]=='*'){
                    for (int i = j-1; num>size; ) {
                        sb.append(arr[i]);
                        size++;
                    }
                }
                if (arr[j] != '?' && arr[j] != '*' && arr[j] != 'A') {
                    sb.append(arr[j]);
                }
            }
            System.out.println(sb);
            ///////////////////////////////////////////////////////////////////
        }else if (size - (cane + snow) <= num) {
            int i = 0;
            while (size > num && i < arr.length) {
                if (arr[i] == '?' || arr[i] == '*') {
                    arr[i - 1] = 'A';
                    size--;
                }
                i++;
            }//while

            print(arr);
            ////////////////////////////////////////////////////////////////
        } else {
            System.out.println("Impossible");
        }

    }//psvm
}//class


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

/**
 * Created by vignesh on 1/27/2017.
 */
public class hackerRank {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String[] pokemons= {"vaporeon", "jolteon", "flareon", "umbreon", "leafeon", "glaceon", "sylveon", "espeon"};
        int n = input.nextInt();
        String s = new String();
        s = input.next();
        int alpha  = 0, count = 0;

        for (int i = 0; i < s.length(); i++) {

            if(s.charAt(i) != '.')

                alpha++;

        }

        if(s.length() == 8)

            System.out.println("vaporeon");

        else if(s.length() == 6)

            System.out.println("espeon");

        else{

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

                count = 0;

                for(int j = 0; j < 7; j++){

                    if(s.charAt(j) != '.'){

                        if(s.charAt(j) == pokemons[i].charAt(j))

                            count++;

                    }

                    if(count  == alpha) {

                        System.out.println(pokemons[i]);
                        break;
                    }

                }

                if(count == alpha)

                    break;

            }

        }

    }
}


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

public class Training {

    public static void main(String[] args) {
        FastReader in = new FastReader();

        int n = in.nextInt();

        StringBuilder sb = new StringBuilder();

        int prv = 0;
        for (int i = n; i >= 1; i--) {
            if ((n % i == 0) && prv % i == 0) {
                sb.append(i == 1 ? i : i + " ");
                prv = i;
            }
        }

        System.out.println(sb);
    }
}

class FastReader {

    BufferedReader br;
    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;
    }
}


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

public class E {

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

    void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();

        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();

        long ans = 0;

        int maxP = m / n;
        for (int p = 1; p <= maxP; p++) {
            long minLastSum = (long)p * (p + 1) / 2 * n;
            if (a[n - 1] < minLastSum)
                continue;
            long maxLastSum = (long)m * p - (long)n * p * (p - 1) / 2;
            long[] money = new long[n];
            money[n - 1] = Math.min(maxLastSum, a[n - 1]);
            for (int i = n - 2; i >= 0; i--)
                money[i] = Math.min(a[i], money[i + 1] - p);

            long firstMax = (long)(m + 1) * p - (long)n * p * (p + 1) / 2;
            long diff = firstMax - money[0];
            if (diff < 0)
                throw new AssertionError();
            long firstPay = diff <= m - p * n ? (m - p * n + 1 - diff) : 1;
            long free = (m - firstPay + 1) - n * p;

            long cur = money[0];
            long last = money[0];
            for (int j = 1; j < n; j++) {
                long now = last + p;
                long shift = Math.min(free, money[j] - now);
                free -= shift;
                last = now + shift;
                cur += last;
            }

            ans = Math.max(ans, cur);
        }

        out.println(ans);
    }

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

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

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

    String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            eof = true;
            return null;
        }
    }

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

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

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


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

public	 class CODEFORCES
	{
		private InputStream is;
		private PrintWriter out;
		int time = 0, DP[], start[], end[], dist[], black[], MOD = (int) (1e9 + 7),
				arr[], weight[][], x[], y[], parent[];
		int MAX = 100000, N, K;
		long red[];
		ArrayList<Integer>[] amp;
		ArrayList<Pair>[][] pmp;
		boolean b[], boo[][];
		Pair prr[];
		HashMap<Pair, Integer> hm = new HashMap();
		long Dp[][][][] = new long[110][110][12][12];

		void soln()
		{
			is = System.in;
			out = new PrintWriter(System.out);
			long s = System.currentTimeMillis();
			solve();
			// out.close();
			out.flush();
			// tr(System.currentTimeMillis() - s + "ms");
		}

		public static void main(String[] args) throws Exception
		{
			new Thread(null, new Runnable()
			{
				public void run()
				{
					try
					{
						// new CODEFORCES().soln();
					} catch (Exception e)
					{
						System.out.println(e);
					}
				}
			}, "1", 1 << 26).start();
			new CODEFORCES().soln();
		}

		int ans = 0, cost = 0, D[][];
		char ch[], ch1[];
		int hash = 29;
		TreeSet<Cell> ts;
		int prime[] = new int[100000];
		int dp[] = new int[50000];
		TreeSet<String> tset = new TreeSet<>();
		void solve()
		{
			long n = nl();
			long low = 1, high = n, ans = high;
			while(low<=high) {
				long mid = (low+high)/2;
				long temp = n;
				long cnt = 0;
				while(temp>0) {
					if(temp<=mid) {
						cnt += temp;
						temp = 0;
					}
					else {
						temp -= mid;
						cnt += mid;
					}
					temp -= temp/10;
				}
				//out.println(mid+" "+cnt);
				if(cnt>=((n+1)/2)) {
					high = mid-1;
					ans = mid;
				}
				else {
					low = mid+1;
				}
			}
			out.println(ans);
		}
		void printArray(long[] arr) {
			for(long i : arr) out.print(i +" ");
			out.println();
		}
		void bfs(int x) {
			Queue<Integer> q = new LinkedList<>();
			q.add(x);
			arr[x] = 0;
			while(!q.isEmpty()) {
				int y = q.poll();
				for(int i : amp[y]) {
					arr[i] = min(arr[i],arr[y]+1);
					if(!b[i]) {
						q.add(i);
						b[i] = true;
					}
				}
			}
		}
		long calc(char ch[]) {
			long val = 1, sum = 0;
			for(char c : ch) {
				if(c == 'S') sum += val;
				else val*=2;
			}
			return sum;
		}
		int dfs(int x) {
			b[x] = true;
			//start[x] = time++;
			int ans = 1;
			for(int i : amp[x]) {
				if(!b[i]) {
					ans += dfs(i);
				}
			}
			//end[x] = time;
			if(x!= 0 && ans%2==0 && (N-ans)%2==0) cost++;

			return ans;
		}
		int calc(int x) {
			String s = Integer.toString(x);
			int ans = 0;
			for(char ch : s.toCharArray()) ans += (ch-'0');
			return ans;
		}
		boolean isCons(char ch){
			if(ch!='a' && ch!='e' && ch!='i' && ch!='o' && ch!='u' && ch!=' ') return true;
			return false;
		}
		void seive(int x){
			b = new boolean[x+1];
			for(int i = 2;i*i<=x;i++){
				if(!b[i]){
					for(int j = 2*i;j<=x;j+=i) b[j] = true;
				}
			}
		}
		long[][] matMul(long a[][] , long b[][], int n)
		{
			long res[][] = new long[n][n];
			for(int i = 0;i<n;i++){
				for(int j = 0;j<n;j++){
					long sum = 0;
					for(int k = 0;k<n;k++){
						sum += (a[i][k]*1l*b[k][j]);
						sum %= MOD;
					}
					res[i][j] = sum;
				}
			}
			return res;
		}
		int min(int a, int b)
		{
			return (a<b)?a:b;
		}
		private class Cell implements Comparable<Cell>
		{
			int u, v, s;
			public Cell(int u, int v, int s)
			{
				this.u = u;
				this.v = v;
				this.s = s;
			}
			public int hashCode()
			{
				return Objects.hash();
			}
			public int compareTo(Cell other)
			{
				return (Long.compare(s, other.s) != 0 ? (Long.compare(s, other.s)):(Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u)))
						&((Long.compare(s, other.s) != 0 ? (Long.compare(s, other.s)):(Long.compare(u, other.v)!=0?Long.compare(u, other.v):Long.compare(v, other.u))));
			}
			public String toString()
			{
				return this.u + " " + this.v;
			}
		}

		class Pair implements Comparable<Pair>
		{
			int u, v, z, i;

			Pair(int u, int v)
			{
				this.u = u;
				this.v = v;
			}

			Pair()
			{

			}

			Pair(int u, int v, int z, int i)
			{
				this.u = u;
				this.v = v;
				this.z = z;
				this.i = i;
			}

			public int hashCode()
			{
				return Objects.hash();
			}

			public boolean equals(Object o)
			{
				Pair other = (Pair) o;
				return (((u == other.v && v == other.u)));
			}

			public int compareTo(Pair other)
			{
				//return (((u == other.v && v == other.u)))?0:1;
				// return Integer.compare(val, other.val);
				return Long.compare(u, other.u);// != 0 ? (Long.compare(u, other.u)): (Long.compare(v, other.v));
						//&(Long.compare(u, other.v) != 0 ? (Long.compare(u, other.v)): (Long.compare(v, other.u))));
			}

			public String toString()
			{
				return this.u + " " + this.v;
			}
		}

		int max(int a, int b)
		{
			if (a > b)
				return a;
			return b;
		}

		void buildGraph(int n)
		{
			for (int i = 0; i < n; i++)
			{
				int x1 = ni() - 1, y1 = ni() - 1;
				amp[x1].add(y1);
				amp[y1].add(x1);
			}
		}
		long modInverse(long a, long mOD2)
		{
			return power(a, mOD2 - 2, mOD2);
		}

		long power(long x, long y, long m)
		{
			if (y == 0)
				return 1;
			long p = power(x, y / 2, m) % m;
			p = (p * p) % m;

			return (y % 2 == 0) ? p : (x * p) % m;
		}

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

		static class ST1
		{
			int arr[], st[], size;

			ST1(int a[])
			{
				arr = a.clone();
				size = 10 * arr.length;
				st = new int[size];
				build(0, arr.length - 1, 1);
			}

			void build(int ss, int se, int si)
			{
				if (ss == se)
				{
					st[si] = arr[ss];
					return;
				}
				int mid = (ss + se) / 2;
				int val = 2 * si;
				build(ss, mid, val);
				build(mid + 1, se, val + 1);
				st[si] = Math.min(st[val], st[val + 1]);
			}

			int get(int ss, int se, int l, int r, int si)
			{
				if (l > se || r < ss || l > r)
					return Integer.MAX_VALUE;
				if (l <= ss && r >= se)
					return st[si];
				int mid = (ss + se) / 2;
				int val = 2 * si;
				return Math.min(get(ss, mid, l, r, val),
						get(mid + 1, se, l, r, val + 1));
			}
		}

		static class ST
		{
			int arr[], lazy[], n;

			ST(int a)
			{
				n = a;
				arr = new int[10 * n];
				lazy = new int[10 * n];
			}

			void up(int l, int r, int val)
			{
				update(0, n - 1, 0, l, r, val);
			}

			void update(int l, int r, int c, int x, int y, int val)
			{
				if (lazy[c] != 0) {
					lazy[2 * c + 1] += lazy[c];
					lazy[2 * c + 2] += lazy[c];
					if (l == r)
						arr[c] += lazy[c];
					lazy[c] = 0;
				}
				if (l > r || x > y || l > y || x > r)
					return;
				if (x <= l && y >= r)

				{
					lazy[c] += val;
					return;
				}
				int mid = l + r >> 1;
				update(l, mid, 2 * c + 1, x, y, val);
				update(mid + 1, r, 2 * c + 2, x, y, val);
				arr[c] = Math.max(arr[2 * c + 1], arr[2 * c + 2]);
			}

			int an(int ind)
			{
				return ans(0, n - 1, 0, ind);
			}

			int ans(int l, int r, int c, int ind)
			{
				if (lazy[c] != 0)
				{
					lazy[2 * c + 1] += lazy[c];
					lazy[2 * c + 2] += lazy[c];
					if (l == r)
						arr[c] += lazy[c];
					lazy[c] = 0;
				}
				if (l == r)
					return arr[c];
				int mid = l + r >> 1;
				if (mid >= ind)
					return ans(l, mid, 2 * c + 1, ind);
				return ans(mid + 1, r, 2 * c + 2, ind);
			}
		}

		public static int[] shuffle(int[] a, Random gen)
		{
			for (int i = 0, n = a.length; i < n; i++)
			{
				int ind = gen.nextInt(n - i) + i;
				int d = a[i];
				a[i] = a[ind];
				a[ind] = d;
			}
			return a;
		}

		long power(long x, long y, int mod)
		{
			long ans = 1;
			while (y > 0)
			{
				if (y % 2 == 0)
				{
					x = (x * x) % mod;
					y /= 2;
				} else
				{
					ans = (x * ans) % mod;
					y--;
				}
			}
			return ans;
		}

		// To Get Input
		// Some Buffer Methods
		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();
			}
		}

		private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

		private void tr(Object... o)
		{
			if (!oj)
				System.out.println(Arrays.deepToString(o));
		}
	}


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

 //@Manan Parmar

public class Solution2 implements Runnable {

    public void run() {

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

        int t=sc.nextInt();
        while(t--!=0)
        {
            int n=sc.nextInt();
            if(n%3==0||n%7==0)
                n=0;
            else
            {
            while(true)
            {
                if(n%3==0||n%7==0)
                {
                    n=0;
                    break;
                }
                else if(n>=7)
                    n=n-7;
                else if(n>=3)
                    n=n-3;
                else
                    break;
            }
            }
            //out.println(n);
            if(n==0)
                out.println("YES");
            else
                out.println("NO");
        }
        out.close();
    }

//========================================================================

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        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 String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        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 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 double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            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);
        }
    }

    public static void main(String args[]) throws Exception {
        new Thread(null, new Solution2(),"Main",1<<27).start();
    }
}


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

public class Main {

    void run(){
        boolean oj = System.getProperty("ONLINE_JUDGE")!=null;
//        boolean oj = false;
        if( oj ){
            br  = new BufferedReader( new InputStreamReader (System.in ) );
            out = new PrintWriter   ( new OutputStreamWriter(System.out) );
        }
        else{
            try{
                br  = new BufferedReader(new FileReader("abc.in"));
                out = new PrintWriter(   new FileWriter("abc.out") );
            } catch (IOException e) {
                MLE();
            }
        }
        long tBeg = System.currentTimeMillis();
        solve();
//        deb();
//        out.println( "TIME: " + (System.currentTimeMillis()-tBeg)/1e3 );
        out.flush();
    }

    BufferedReader br;
    StringTokenizer in;
    PrintWriter out;

    String next() {
        while( in==null || !in.hasMoreTokens() )
            try {
                in = new StringTokenizer( br.readLine() );
            } catch (IOException e) {
                MLE();
            }
        return in.nextToken();
    }

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

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

    public static void main(String[] args) throws IOException {
        new Thread( null, new Runnable() {
            @Override
            public void run() {
                new Main().run();
            }
        }, "myThread", 1<<30 ).run();
    }

    void MLE(){
//        for(;;);
        int[][] adj = new int[1024*1024][];
        for( int i = 0; i < adj.length; ++i )
            adj[i] = new int[1024*1024];
    }

//    final int cntBits = 29;
//
//    HashMap<Integer,Integer> mem = new HashMap<Integer, Integer>();
//    int g( int msk ){
//        if( mem.containsKey(msk) )
//            return mem.get(msk);
//
//        if( msk == 0 ){
//            mem.put(msk,0);
//            return 0;
//        }
//
//        boolean[] u = new boolean[200];
//        for( int i = 1; i <= cntBits; ++i ){
//            if(!( (msk&(1<<i)) != 0 ))continue;
//            int toMsk = msk;
//            for( int j = i; j <= cntBits; j+=i ){
//                toMsk &= ~(1<<j);
//            }
//            u[g(toMsk)] = true;
//        }
//        for( int ansG = 0; ; ++ansG )
//            if( !u[ansG] ){
//                mem.put(msk,ansG);
//                return ansG;
//            }
//    }

    final int[] g = new int[]{0, 1, 2, 1, 4, 3, 2, 1, 5, 6, 2, 1, 8, 7, 5, 9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12};

    void solve(){
//        for( int c = 1; c <= cntBits; ++c ){
//            int msk = 0;
//            for( int j = 1; j <= c; ++j )
//                msk |= (1<<j);
//            out.print( ", " + g(msk) );
//        }
//        out.println();

        int n = nextInt();
        int s = g[1];
        int other = n-1;

        BitSet bs = new BitSet();
        int i;
        for( i = 2; i*i <= n; ++i ){
            if( bs.get(i) ) continue;

            int a = 0;
            for( long j = i; j <= n;  j *= i, ++a, --other ){
                if( j < 1e5 ) bs.set((int)j,true);
            }
            s ^= g[a];
        }
        if( other%2==1 ) s ^= 1;
        out.println( s != 0? "Vasya" : "Petya" );
    }

}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package Learning;

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

public class Try
{

    static class InputReader {

		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;

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

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

		public int ni() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nl() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public int[] nia(int n) {
			int a[] = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = ni();
			}
			return a;
		}

		public String rs() {
			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 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 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;
		}

	}

    static long mod=1000000007;
    static BigInteger bigInteger = new BigInteger("1000000007");
    static int n = (int)1e6;
    static boolean[] prime;
    static ArrayList<Integer> as;
     static ArrayList<Integer> []as1;
    static void sieve() {
			Arrays.fill(prime	, true);
			prime[0] = prime[1] = false;
			for(int i = 2 ; i * i <= n ; ++i) {
				if(prime[i]) {
					for(int k = i * i; k< n ; k+=i) {
						prime[k] = false;
					}
				}
			}

		}
		static PrintWriter w = new PrintWriter(System.out);
                static int p = 0;
 static char [][]sol;
    public static void main(String[] args)
    {

        InputReader sc = new InputReader(System.in);
        //PrintWriter w = new PrintWriter(System.out);

        prime  = new boolean[n + 1];
		sieve();
		prime[1] = false;

		/*
		as = new ArrayList<>();
		for(int i=2;i<=1000000;i++)
		{
		    if(prime[i])
		    as.add(i);
		}
		*/

       /*

	long a = sc.nl();

            BigInteger ans = new BigInteger("1");
            for (long i = 1; i < Math.sqrt(a); i++) {
                if (a % i == 0) {
                    if (a / i == i) {
                        ans = ans.multiply(BigInteger.valueOf(phi(i)));

                    } else {
                        ans = ans.multiply(BigInteger.valueOf(phi(i)));
                        ans = ans.multiply(BigInteger.valueOf(phi(a / i)));

                    }

                }
            }
            w.println(ans.mod(bigInteger));
            */

         // MergeSort ob = new MergeSort();
       // ob.sort(arr, 0, arr.length-1);

    // Student []st = new Student[x];
   // st[i] = new Student(i,d[i]);
     //Arrays.sort(st,(p,q)->p.diff-q.diff);
//BigDecimal x = new BigDecimal(b[i]).multiply(new BigDecimal("-1")).divide(new BigDecimal(a[i]),100,RoundingMode.HALF_UP);

int x = sc.ni();
int k = sc.ni();
if(k==1)
{
    w.println(3*x);
}
else
{
    int p = Math.min(k-1, x-k);
    w.println(p + (3*x));
}

   w.close();

    }

    static int []a;
    static long a1,b;
    public static int searchlow(int x) {
		int lo=0, hi=a.length-1;
		int res=Integer.MIN_VALUE;
		while(lo<=hi){
	        int mid = (lo+hi)/2;
	        if(a[mid]==x){
	            res = mid;
	            hi = mid - 1;
	        }
	        else if(a[mid]>x){
	            hi = mid - 1;
	        }
	        else{
	            lo = mid + 1;
	        }
	    }
	    return res==Integer.MIN_VALUE?lo:res;
	}
	public static int searchhigh(int x) {
		int lo=0, hi=a.length-1;
		int res=Integer.MIN_VALUE;
		while(lo<=hi){
	        int mid = (lo+hi)/2;
	        if(a[mid]==x){
	            res = mid;
	            lo=mid+1;
	        }
	        else if(a[mid]>x){
	            hi = mid - 1;
	        }
	        else{
	            lo = mid + 1;
	        }
	    }
	    return res==Integer.MIN_VALUE?hi:res;
	}
	public static long ct(int l, int r) {
		int lo=searchlow(l), hi=searchhigh(r);
		return hi-lo+1;
	}
    static long log2(long value) {
    return Long.SIZE-Long.numberOfLeadingZeros(value);
}

    static class Student
    {
        int id;
        //int x;
       String s;
        int y;

        Student(int id,String s,int y)
        {
            this.id = id;
            //this.x = x;
            this.s = s;
            this.y = y;

        }

    }

     public static long modMultiply(long one, long two) {

		return (one % mod * two % mod) % mod;
	}
    static long fx(int m)
    {
        long re = 0;
        for(int i=1;i<=m;i++)
        {
            re += (long) (i / gcd(i,m));
        }
        return re;
    }
     static int gcd(int a, int b)
    {
	 if (a == 0)
	  return b;

	return gcd(b%a, a);
    }

     static long phi(long nx)
    {
        // Initialize result as n
        double result = nx;

        // Consider all prime factors of n and for
        // every prime factor p, multiply result
        // with (1 - 1/p)
        for (int p = 0; as.get(p) * as.get(p) <= nx; ++p)
        {
            // Check if p is a prime factor.
            if (nx % as.get(p) == 0)
            {
                // If yes, then update n and result
                while (nx % as.get(p) == 0)
                    nx /= as.get(p);
                result *= (1.0 - (1.0 / (double) as.get(p)));
            }
        }

        // If n has a prime factor greater than sqrt(n)
        // (There can be at-most one such prime factor)
        if (nx > 1)
            result *= (1.0 - (1.0 / (double) nx));

        return (long)result;

        //return(phi((long)result,k-1));

    }

    public static void primeFactors(int n,int x)
    {
        as = new ArrayList<>();

        // Print the number of 2s that divide n
        while (n%2==0)
        {

            as.add(2);
            //System.out.print(2 + " ");
            n /= 2;

        }

        // n must be odd at this point.  So we can
        // skip one element (Note i = i +2)
        for (int i = 3; i <= Math.sqrt(n); i+= 2)
        {
            // While i divides n, print i and divide n
            while (n%i == 0)
            {

               // System.out.print(i + " ");

                as.add(i);
                n /= i;

            }

        }

        // This condition is to handle the case whien
        // n is a prime number greater than 2
        if (n >= 2)
        {

            as.add(n);
        }

    }
    static int digitsum(int x)

    {

        int sum = 0;
        while(x > 0)
        {
            int temp = x % 10;
            sum += temp;
            x /= 10;
        }
        return sum;

    }
    static int countDivisors(int n)
    {
        int cnt = 0;
        for (int i = 1; i*i <=n; i++)
        {
             if (n % i == 0 && i<=1000000)
             {
                // If divisors are equal,
                // count only one
                if (n / i == i)
                   cnt++;

                else // Otherwise count both
                    cnt = cnt + 2;
             }
         }
        return cnt;
    }

    static boolean isprime(long n)
    {

    if(n == 2)
        return true;
    if(n == 3)
        return true;
    if(n % 2 == 0)
        return false;
    if(n % 3 == 0)
        return false;

    long i = 5;
   long w = 2;

    while(i * i <= n)
    {
        if(n % i == 0)
            return false;

        i += w;
        w = 6 - w;
    }

    return true;
    }

    static boolean binarysearch(int []arr,int p,int n)
    {
        //ArrayList<Integer> as = new ArrayList<>();
        //as.addAll(0,at);
        //Collections.sort(as);
        boolean re = false;
        int st = 0;
        int end = n-1;

        while(st <= end)
        {
            int mid = (st+end)/2;

            if(p > arr[mid])
            {
                st = mid+1;

            }
            else if(p < arr[mid])
            {
                end = mid-1;

            }
            else if(p == arr[mid])
            {

            re = true;
            break;
            }

        }
         return re;

    }

    /* Java program for Merge Sort */
static class MergeSort
{
    // Merges two subarrays of arr[].
    // First subarray is arr[l..m]
    // Second subarray is arr[m+1..r]
    void merge(int arr[], int l, int m, int r)
    {
        // Find sizes of two subarrays to be merged
        int n1 = m - l + 1;
        int n2 = r - m;

        /* Create temp arrays */
        int L[] = new int [n1];
        int R[] = new int [n2];

        /*Copy data to temp arrays*/
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];

        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays
        int i = 0, j = 0;

        // Initial index of merged subarry array
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        /* Copy remaining elements of L[] if any */
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }

        /* Copy remaining elements of R[] if any */
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    // Main function that sorts arr[l..r] using
    // merge()
    void sort(int arr[], int l, int r)
    {
        if (l < r)
        {
            // Find the middle point
            int m = (l+r)/2;

            // Sort first and second halves
            sort(arr, l, m);
            sort(arr , m+1, r);

            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }

    /* A utility function to print array of size n */

}

    public static int ip(String s){
		return Integer.parseInt(s);
	}
	 public static String ips(int s){
		return Integer.toString(s);
	}

         // Java program to print all permutations using
// Heap's algorithm
static class HeapAlgo
{
   //static ArrayList<Integer> []ad;
	//Prints the array
	void printArr(int a[], int n)
	{

		for (int i=0; i<n; i++)
		{
			System.out.print(a[i] + " ");
		as1[p].add(a[i]);
		}
                p++;

		//System.out.println();
	}

	//Generating permutation using Heap Algorithm
	void heapPermutation(int a[], int size, int n)
	{
		// if size becomes 1 then prints the obtained
		// permutation

		if (size == 1)
		{
			printArr(a,n);

		}

		for (int i=0; i<size; i++)
		{
			heapPermutation(a, size-1, n);
                        p++;

			// if size is odd, swap first and last
			// element
			if (size % 2 == 1)
			{
				int temp = a[0];
				a[0] = a[size-1];
				a[size-1] = temp;
			}

			// If size is even, swap ith and last
			// element
			else
			{
				int temp = a[i];
				a[i] = a[size-1];
				a[size-1] = temp;
			}
		}
	}
        public static int lowerBound(int[] array, int length, int value) {
        int low = 0;
        int high = length-1;
        while (low < high) {
            final int mid = (low + high) / 2;
            if (value <= array[mid]) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        return low;
  }
  public static int upperBound(int[] array, int length, int value) {
        int low = 0;
        int high = length-1;
        while (low < high) {
            final int mid = (low + high) / 2;
            if (value >= array[mid]) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }

	// Driver code

}

// This code has been contributed by Amit Khandelwal.
// JAVA program for implementation of KMP pattern
// searching algorithm

static class KMP_String_Matching {
	boolean KMPSearch(String pat, String txt)
	{
            int f = 0;
		int M = pat.length();
		int N = txt.length();

		// create lps[] that will hold the longest
		// prefix suffix values for pattern
		int lps[] = new int[M];
		int j = 0; // index for pat[]

		// Preprocess the pattern (calculate lps[]
		// array)
		computeLPSArray(pat, M, lps);

		int i = 0; // index for txt[]
		while (i < N) {
			if (pat.charAt(j) == txt.charAt(i)) {
				j++;
				i++;
			}
			if (j == M) {
                            /*
				System.out.println("Found pattern "
								+ "at index " + (i - j));
                            */
                            f = 1;
                           // return true;
				j = lps[j - 1];
			}

			// mismatch after j matches
			else if (i < N && pat.charAt(j) != txt.charAt(i)) {
				// Do not match lps[0..lps[j-1]] characters,
				// they will match anyway
				if (j != 0)
					j = lps[j - 1];
				else
					i = i + 1;
			}
		}
                if(f==0)
                    return false;
                else
                    return true;
	}

	void computeLPSArray(String pat, int M, int lps[])
	{
		// length of the previous longest prefix suffix
		int len = 0;
		int i = 1;
		lps[0] = 0; // lps[0] is always 0

		// the loop calculates lps[i] for i = 1 to M-1
		while (i < M) {
			if (pat.charAt(i) == pat.charAt(len)) {
				len++;
				lps[i] = len;
				i++;
			}
			else // (pat[i] != pat[len])
			{
				// This is tricky. Consider the example.
				// AAACAAAA and i = 7. The idea is similar
				// to search step.
				if (len != 0) {
					len = lps[len - 1];

					// Also, note that we do not increment
					// i here
				}
				else // if (len == 0)
				{
					lps[i] = len;
					i++;
				}
			}
		}
	}

	// Driver program to test above function
        /*
	public static void main(String args[])
	{
		String txt = "ABABDABACDABABCABAB";
		String pat = "ABABCABAB";
		new KMP_String_Matching().KMPSearch(pat, txt);
	}
        */
}
// This code has been contributed by Amit Khandelwal.

}


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

public class Main {
    public static MyScanner sc = new MyScanner(System.in);
    public static PrintWriter out = new PrintWriter(System.out);
    public static void main(String[] args) {
        int n = sc.nextint();
        int mod = sc.nextint();
        int[] dp1 = new int[n + 9];
        int[] dp2 = new int[n + 9];
        dp2[1] = 1;
        for(int i = 1; i <= n; i++){
            dp1[i] += dp1[i - 1];
            dp1[i] %= mod;
            dp2[i] = dp1[i];
            if(i == 1){
                dp2[i] = 1;
            }
            dp1[i + 1] += dp2[i];
            dp1[i + 1] %= mod;
            for(int j = 2; i * j <= n; j++){
                dp1[i * j] += dp2[i];
                dp1[i * j] %= mod;
                if((i + 1) * j <= n) {
                    dp1[(i + 1) * j] += mod - dp2[i];
                    dp1[(i + 1) * j] %= mod;
                }
            }
        }
        out.print(dp2[n]);
        out.close();
    }
}
class MyScanner {
    private BufferedReader br;
    private StringTokenizer tokenizer;

    public MyScanner(InputStream is) {
        br = new BufferedReader(new InputStreamReader(is));
    }

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

    public int nextint() {
        return Integer.parseInt(next());
    }
    public long nextlong() {
        return Long.parseLong(next());
    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;

public class Main {

    long binpow(int a, int n) {
        long r = 1;
        while (n > 0) {
            if ((n & 1) > 0) {
                r *= a;
            }
            a *= a;
            n /= 2;
        }
        return r;
    }

    long gcd(long x, long y) {
        return y == 0 ? x : gcd(y, x % y);
    }

    void solve() throws IOException {
        BigInteger n = BigInteger.valueOf(readLong()), k = BigInteger.valueOf(readLong()), m = BigInteger.valueOf(readLong()), d = BigInteger.valueOf(readLong());
        long maxRes = -1;

        // todo check M

        BigInteger resForM = checkM(n.longValue(), k.longValue(), m.longValue(), d.longValue());
        System.err.println("resForM = " + resForM);
        maxRes = Math.max(maxRes, resForM.longValue());

        for (long rounds = 1; rounds <= d.longValue(); rounds++) {
            System.err.println("rounds = " + rounds);
            BigInteger x = n.divide(k.multiply(BigInteger.valueOf(rounds - 1)).add(BigInteger.ONE));
            System.err.println("x = " + x);
            if (x.compareTo(m) > 0) {
                continue;
            }
            BigInteger res = x.multiply(BigInteger.valueOf(rounds));
            System.err.println("res = " + res);
            if (res.longValue() > maxRes) {
                maxRes = res.longValue();
            }
        }
        System.out.println(maxRes);
    }

    private BigInteger checkM(long n, long k, long m, long d) {
        long s = n / m;
        long rounds = (s + k - 1) / k;
        if (rounds > d) {
            return BigInteger.valueOf(-1);
        }
        return BigInteger.valueOf(rounds).multiply(BigInteger.valueOf(m));
    }

    //-------------------------------------------------

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;

    public void run() {
        try {
            long startTime = System.currentTimeMillis();
            Locale.setDefault(Locale.US);
            if (ONLINE_JUDGE) {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                in = new BufferedReader(new FileReader("input.txt"));
                out = new PrintWriter("output.txt");
            }
            tok = new StringTokenizer("");
            solve();
            in.close();
            out.close();
            long endTime = System.currentTimeMillis();
            long totalMemory = Runtime.getRuntime().totalMemory();
            long freeMemory = Runtime.getRuntime().freeMemory();
            System.err.println("Time = " + (endTime - startTime) + " ms");
            System.err.println("Memory = " + ((totalMemory - freeMemory) / 1024) + " KB");
        } catch (Throwable e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() {
        while (!tok.hasMoreTokens()) {
            String line = null;
            try {
                line = in.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (line == null) return null;
            tok = new StringTokenizer(line);
        }
        return tok.nextToken();
    }

    int readInt() {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

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

    void debug(Object... o) {
        if (!ONLINE_JUDGE) {
            System.err.println(Arrays.deepToString(o));
        }
    }

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


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

public class Prob242A {
	public static void main(String[] Args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int x = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		BigInteger temp = BigInteger.ONE;
		BigInteger mod = BigInteger.valueOf(1000000009);
		BigInteger two = temp.add(temp);
		for (int i = 0; i < m; i++)
			temp = temp.multiply(two).mod(mod);
		temp = temp.subtract(BigInteger.ONE);

		BigInteger bi = BigInteger.ONE;
		int temp2 = temp.intValue();
		for (int i = temp2, j = 0; j < x; i--, j++) {
			bi = bi.multiply(BigInteger.valueOf(i)).mod(mod);
			if (bi.intValue() == 0)
				break;
		}

		System.out.println(bi);

	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String s = in.next();
		int n  = s.length();
		char[] a = new char[n];
		for(int i = 0; i < s.length(); ++i) {
			a[i] = s.charAt(i);
		}

		int y=0,g=0,r=0,b=0;
		int[] YBRG = {-1,-1,-1,-1};
		char[] cYBRG = {'Y','B','R','G'};
		for(int i = 0;  i < s.length(); i++) {
			if(a[i] == 'Y' && YBRG[0] == -1) YBRG[0] = i%4;
			if(a[i] == 'B' && YBRG[1] == -1) YBRG[1] = i%4;
			if(a[i] == 'R' && YBRG[2] == -1) YBRG[2] = i%4;
			if(a[i] == 'G' && YBRG[3] == -1) YBRG[3] = i%4;
		}
		for(int z = 0; z < 4; ++z) {
			for(int i = 0; i < 3; ++i) {
				if(YBRG[i] > YBRG[i+1]) {
					int t = YBRG[i];
					char c = cYBRG[i];
					YBRG[i] = YBRG[i+1];
					cYBRG[i] = cYBRG[i+1];
					YBRG[i+1] = t;
					cYBRG[i+1] = c;
				}
			}
		}

		for(int i = 0; i < s.length(); ++i) {
			if(s.charAt(i) == '!') {
				if(cYBRG[i%4] == 'Y') y++;
				if(cYBRG[i%4] == 'B') b++;
				if(cYBRG[i%4] == 'R') r++;
				if(cYBRG[i%4] == 'G') g++;
			}
		}

		System.out.println(r + " " + b + " " + y + " " + g);
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * @author khokharnikunj8
 */

public class Main {
    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Main().solve();
            }
        }, "1", 1 << 26).start();
    }

    void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        BNumbers solver = new BNumbers();
        solver.solve(1, in, out);
        out.close();
    }

    static class BNumbers {
        int mod = 1000000007;

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            int[] ar = new int[10];
            for (int i = 0; i < 10; i++) {
                ar[i] = in.scanInt();
            }
            int[][] nCr = new int[101][];
            for (int i = 0; i <= 100; i++) {
                nCr[i] = new int[i + 2];
                for (int j = 0; j <= i; j++) {
                    if (i == 0 || j == 0) nCr[i][j] = 1;
                    else nCr[i][j] = (nCr[i - 1][j - 1] + nCr[i - 1][j]) % mod;
                }
            }
            long ans = 0;
            for (int i = 1; i <= 9; i++) {
                ar[i]--;
                long[][] dp = new long[2][n];
                dp[1][0] = 1;
                for (int j = 0; j <= 9; j++) {
                    Arrays.fill(dp[j & 1], 0);
                    for (int k = 0; k < n; k++) {
                        for (int l = Math.max(0, ar[j]); k + l < n; l++) {
                            dp[j & 1][k + l] = (dp[j & 1][k + l] + dp[(j - 1) & 1][k] * nCr[k + l][l]) % mod;
                        }
                    }
                }
                for (int j = 0; j < n; j++) ans = (ans + dp[1][j]) % mod;
                ar[i]++;
            }
            out.println(ans);

        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int index;
        private BufferedInputStream in;
        private int total;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (index >= total) {
                index = 0;
                try {
                    total = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (total <= 0) return -1;
            }
            return buf[index++];
        }

        public int scanInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

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

	static class F {
		private Long[][] lastX = new Long[2010][2010];
		private long mod = 998244353;
		private long p = 0;
		private long[] pPow = new long[2010];
		private long[] qPow = new long[2010];
		private long[] strong = new long[2010];
		private Long[] dp = new Long[2010];

		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int n = in.nextInt();
			int a = in.nextInt(), b = in.nextInt();
			p = MathUtils.powMod(b, mod - 2, mod) * a % mod;
			pPow[0] = 1;
			qPow[0] = 1;
			for (int i = 1; i < 2010; ++i) {
				pPow[i] = pPow[i - 1] * p % mod;
				qPow[i] = qPow[i - 1] * (1 - p + mod) % mod;
			}
			strong[1] = 1;
			for (int i = 2; i <= n; ++i) {
				long ans = 0;
				for (int j = 1; j < i; ++j) {
					ans += strong[j] * computeLastX(i, j) % mod;
					if (ans >= mod) ans -= mod;
				}
				strong[i] = 1 - ans;
				if (strong[i] < 0) strong[i] += mod;
			}
			long ans = dp(n);
			out.println(ans);
		}

		private long computeLastX(int n, int i) {
			if (lastX[n][i] != null) return lastX[n][i];
			if (n == i || i == 0) return lastX[n][i] = 1L;
			long ans = qPow[i] * computeLastX(n - 1, i) % mod + pPow[n - i] * computeLastX(n - 1, i - 1) % mod;
			return lastX[n][i] = ans % mod;
		}

		private long dp(int n) {
			if (dp[n] != null) return dp[n];
			if (n < 2) return dp[n] = 0L;
			if (n < 3) return dp[n] = 1L;
			long ans = strong[n] * (n * (n - 1) / 2) % mod;
			for (int x = 1; x < n; ++x) {
				long tmp = computeLastX(n, x) * strong[x] % mod;
				tmp *= (x * (x - 1) / 2 + x * (n - x) + dp(n - x) + dp(x)) % mod;
				ans += tmp % mod;
			}
			ans %= mod;
			ans *= MathUtils.powMod(1 + mod - strong[n], mod - 2, mod);
			return dp[n] = ans % mod;
		}

	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream inputStream) {
			reader = new BufferedReader(new InputStreamReader(inputStream));
			tokenizer = null;
		}

		public InputReader(String inputFile) throws FileNotFoundException {
			reader = new BufferedReader(new FileReader(inputFile));
			tokenizer = null;
		}

		public String nextLine() {
			String curr = "";
			try {
				curr = reader.readLine();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			return curr;
		}

		public String nextString() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(nextLine());
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

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

	}

	static class MathUtils {
		public static long powMod(long a, long b, long m) {
			long ans = 1;
			while (b > 0) {
				if (b % 2 == 1) ans = ans * a % m;
				b >>= 1;
				a = a * a % m;
			}
			return ans % m;
		}

	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * @author khokharnikunj8
 */

public class Main {
    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Main().solve();
            }
        }, "1", 1 << 26).start();
    }

    void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DFelicitysBigSecretRevealed solver = new DFelicitysBigSecretRevealed();
        solver.solve(1, in, out);
        out.close();
    }

    static class DFelicitysBigSecretRevealed {
        int mod = 1000000007;

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            char[] ar = in.scanChars(n);
            int[][] dp = new int[n + 1][1 << 20];
            long ans = 0;
            for (int i = 0; i < n; i++) {
                dp[i][0]++;
                for (int j = 0; j < (1 << 20); j++) {
                    if (dp[i][j] == 0) continue;
                    for (int k = i + 1, val = 0; k <= n && val <= 20; k++) {
                        val <<= 1;
                        val |= (ar[k - 1] - '0');
                        if (val > 20) break;
                        if ((j | (1 << (val - 1))) < (1 << 20)) {
                            if (val != 0)
                                dp[k][(j | (1 << (val - 1)))] = (dp[k][(j | (1 << (val - 1)))] + dp[i][j]) % mod;
                        }
                    }
                }
                for (int j = 1; j <= 20; j++) ans = (ans + dp[i + 1][(1 << j) - 1]) % mod;
            }
            out.println(ans);

        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int index;
        private BufferedInputStream in;
        private int total;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (index >= total) {
                index = 0;
                try {
                    total = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (total <= 0) return -1;
            }
            return buf[index++];
        }

        public int scanInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

        public char[] scanChars(int n) {
            char[] ar = new char[n];
            int c = scan();
            boolean flag = true;
            while (isWhiteSpace(c)) c = scan();
            ar[0] = (char) c;
            for (int i = 1; i < n; i++) ar[i] = (char) (c = scan());
            return ar;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}


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

public class E3 {

    void solve() throws IOException {
        char[] c = reader.readLine().toCharArray();
        int[] a = doit(c);
        if (a != null) {
            out.println(a.length);
            for (int j = 0; j < a.length; j++) {
                out.print(a[j] + " ");
            }
        }
    }

    int[] doit(char[] c) {
        int l = 0;
        int r = 200000;
        while (l < r) {
            index = 0;
            int n = (l + r) / 2;
            int[] a = new int[n];
            int[] b = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = b[i] = i + 1;
            }
            boolean res = simulateMerge(0, n, c, a, b);
            if (!res) {
                r = n;
            } else if (index < c.length) {
                l = n + 1;
            } else {
                for (int i = 0; i < n; i++) {
                    b[a[i] - 1] = i + 1;
                }
                return b;
            }
        }
        return null;
    }

    int index = 0;

    boolean simulateMerge(int l, int r, char[] c, int[] a, int[] b) {
        if (r - l <= 1) return true;
        int m = (l + r) >> 1;
        simulateMerge(l, m, c, a, b);
        simulateMerge(m, r, c, a, b);
        int i = l;
        int j = m;
        int k = l;
        while (i < m && j < r) {
            if (index == c.length) {
                return false;
            }
            if (c[index] == '0') {
                b[k] = a[i];
                i++;
            } else {
                b[k] = a[j];
                j++;
            }
            index++;
            k++;
        }

        while (i < m) {
            b[k] = a[i];
            i++;
            k++;
        }
        while (j < r) {
            b[k] = a[j];
            j++;
            k++;
        }
        for (int x = l; x < r; x++) {
            a[x] = b[x];
        }
        return true;
    }

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

    void run() throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
//		reader = new BufferedReader(new FileReader("input.txt"));
        tokenizer = null;
        out = new PrintWriter(new OutputStreamWriter(System.out));
//		out = new PrintWriter(new FileWriter("output.txt"));
        solve();
        reader.close();
        out.flush();

    }

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter out;

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

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

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

    int[] nextIntArray(int n) throws IOException {
        int[] result = new int[n];
        for (int i = 0; i < n; i++) {
            result[i] = nextInt();
        }
        return result;
    }

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

    double[] nextDoubleArray(int n) throws IOException {
        double[] result = new double[n];
        for (int i = 0; i < n; i++) {
            result[i] = nextDouble();
        }
        return result;
    }

    int[][] nextIntArray(int n, int m) throws IOException {
        int[][] result = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                result[i][j] = nextInt();
            }
        }
        return result;
    }

    long[][] nextLongArray(int n, int m) throws IOException {
        long[][] result = new long[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                result[i][j] = nextLong();
            }
        }
        return result;
    }

    double[][] nextDoubleArray(int n, int m) throws IOException {
        double[][] result = new double[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                result[i][j] = nextDouble();
            }
        }
        return result;
    }

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


import java.util.Scanner;
public class r262B {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scnr = new Scanner(System.in);

		int a = scnr.nextInt();
		int b = scnr.nextInt();
		int c = scnr.nextInt();
		int count = 0;
		long[] ans = new long[81];
		scnr.close();

		for (int i = 1; i <= 81; i++){
			long answer = 1;
			for (int j = 0; j < a; j++)
				answer = answer * i;
			answer = answer*b+c;
			if (answer > 0 && answer < 1000000000 && getSum((int)answer) == i){
				ans[count] = answer;
				count++;
			}
		}

		System.out.print(count);
		if (count!= 0){
			System.out.println();
			for (int i = 0; i < count-1; i++){
				System.out.print(ans[i] + " ");
			}
			System.out.print(ans[count-1]);
		}

	}

	private static int getSum(int a){
		int b = 0;
		while (a > 0){
			b = a%10 + b;
			a = a/10;
		}
		return b;
	}
}


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

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

File input,output;
Scanner in;
PrintWriter out;

public void run() throws IOException
{
in = new Scanner(System.in);
out = new PrintWriter(System.out);

int vx = in.nextInt();
int vy = in.nextInt();
int px = in.nextInt();
int py = in.nextInt();
int wx1 = in.nextInt();
int wy1 = in.nextInt();
int wx2 = in.nextInt();
int wy2 = in.nextInt();
int mx1 = in.nextInt();
int my1 = in.nextInt();
int mx2 = in.nextInt();
int my2 = in.nextInt();

Line2D vp = new Line2D.Double(vx,vy,px,py);
Line2D wall = new Line2D.Double(wx1,wy1,wx2,wy2);
Line2D mirror = new Line2D.Double(mx1,my1,mx2,my2);

if(!wall.intersectsLine(vp))
    {
//  out.println("Wall doesn't intersect vp");
    int vpx = px-vx;
    int vpy = py-vy;
    int mirrorX = mx2-mx1;
    int mirrorY = my2-my1;

//  out.println("Vasya-Petya vector: "+vpx+" "+vpy);
//  out.println("Mirror vector: "+mirrorX+" "+mirrorY);

    if(vpx*mirrorY-vpy*mirrorX==0)
        {
        out.println("YES");
        out.close();
        return;
        }

    if(!mirror.intersectsLine(vp))
        {
        out.println("YES");
        out.close();
        return;
        }
    }
//else out.println("Wall intersects vp");

int mirrorX = mx2-mx1;
int mirrorY = my2-my1;

int normalX = -mirrorY;
int normalY = mirrorX;

int ma = normalX;
int mb = normalY;
int mc = -ma*mx1-mb*my1;

int signv = ma*vx+mb*vy+mc;
int signp = ma*px+mb*py+mc;

signv = signv==0 ? 0 : signv/Math.abs(signv);
signp = signp==0 ? 0 : signp/Math.abs(signp);

if(signv*signp<=0)
    {
    out.println("NO");
    out.close();
    return;
    }

double pdist = mirror.ptLineDist(px,py);
double normalLength = Math.sqrt(normalX*normalX+normalY*normalY);
double normX = normalX*pdist/normalLength;
double normY = normalY*pdist/normalLength;

double p1X = px+2*normX;
double p2X = px-2*normX;
double p1Y = py+2*normY;
double p2Y = py-2*normY;

double p1dist = mirror.ptLineDist(p1X,p1Y);
double p2dist = mirror.ptLineDist(p2X,p2Y);
double qX,qY;
if(p1dist<p2dist)
    {
    qX = p1X;
    qY = p1Y;
    }
else
    {
    qX = p2X;
    qY = p2Y;
    }

Line2D imaginaryLine = new Line2D.Double(vx,vy,qX,qY);
if(!mirror.intersectsLine(imaginaryLine))
    {
    out.println("NO");
    out.close();
    return;
    }

double lx = vx;
double ly = vy;
double rx = qX;
double ry = qY;
double medx,medy;

while(Math.sqrt((lx-rx)*(lx-rx)+(ly-ry)*(ly-ry))>1e-4)
    {
    medx = (lx+rx)/2;
    medy = (ly+ry)/2;
    if(mirror.relativeCCW(medx,medy)==mirror.relativeCCW(vx,vy))
        {
        lx = medx;
        ly = medy;
        }
    else
        {
        rx = medx;
        ry = medy;
        }
    }

medx = (lx+rx)/2;
medy = (ly+ry)/2;

Line2D vmirror = new Line2D.Double(vx,vy,medx,medy);
Line2D pmirror = new Line2D.Double(px,py,medx,medy);
if(wall.intersectsLine(vmirror)||wall.intersectsLine(pmirror)) out.println("NO");
else out.println("YES");
out.close();

}

}


import java.util.Scanner;

public class cola {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();

        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }

        int index = -1;

        for (;;) {
            for (int j = 0; j < n; j++)

                if (arr[j] > 0) {
                    arr[j] -= m;
                    index = j;
                }

            int count = 0;

            for (int j = 0; j < n; j++)
                if (arr[j] > 0)
                    count++;

            if (count == 0)
                break;
        }

        System.out.println(index + 1);

    }
}


import java.io.IOException;
import java.util.Scanner;
public class magicnumber {
    public static void main(String[] args) throws IOException {
     Scanner cs= new Scanner(System.in);

     String s;

    s=cs.next();
       if(s.charAt(0)!='1')
       { System.out.println("NO");}
      else if(s.contains("444"))
      {  System.out.println("NO");}

      else if(s.contains("0")||s.contains("2") ||s.contains("3")||s.contains("5")||s.contains("6")||s.contains("7")|| s.contains("8")||s.contains("9"))
      {System.out.println("NO");
      }
      else
      {  System.out.println("YES");}
      }
     }


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.*;

public class B
{
	BufferedReader in;
	PrintStream out;
	StringTokenizer tok;
	public B() throws NumberFormatException, IOException
	{
		in = new BufferedReader(new InputStreamReader(System.in));
		//in = new BufferedReader(new FileReader("in.txt"));
		out = System.out;
		run();
	}
	double dist(int x1, int y1, int x2, int y2)
	{
		return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
	}
	boolean circInside(double d, int r1, int r2)
	{
		int min = Math.min(r2, r1);
		int max = Math.max(r1, r2);
		return (r1 < r2) && (d<max) && (min<= (max-d));
	}
	boolean circOut(double d, int r1, int r2)
	{
		return d>=r1 && d>=r2 && (d-r1)>=r2;
	}
	void run() throws NumberFormatException, IOException
	{
		int x1 = nextInt(),y1 = nextInt(),r1 = nextInt(),R1 = nextInt();
		int x2 = nextInt(),y2 = nextInt(),r2 = nextInt(),R2 = nextInt();
		int ans = 0;

		double d = dist(x1,y1,x2,y2);
		//r1
		if(circInside(d,r1,r2)||circOut(d,r1,R2) || circInside(d,R2,r1))
			ans++;
		//r2
		if(circInside(d,r2,r1) || circOut(d,r2,R1) ||circInside(d,R1,r2))
			ans++;
		//R1
		if(circInside(d,R1,R2)||circInside(d,R1,r2)||circOut(d,R1,R2) || circInside(d,R2,r1))
			ans++;
		//R2
		if(circInside(d,R2,R1) || circInside(d,R2,r1)||circOut(d,R1,R2) || circInside(d,R1,r2))
			ans++;
		out.println(ans++);

	}
	public static void main(String[] args) throws NumberFormatException, IOException
	{
		new B();
	}
	String nextToken() throws IOException
	{
		if(tok ==null || !tok.hasMoreTokens()) tok = new StringTokenizer(in.readLine());
		return tok.nextToken();
	}
	int nextInt() throws NumberFormatException, IOException
	{
		return Integer.parseInt(nextToken());
	}
	long nextLong() throws NumberFormatException, IOException
	{
		return Long.parseLong(nextToken());
	}
	double nextDouble() throws NumberFormatException, IOException
	{
		return Double.parseDouble(nextToken());
	}
}


import java.io.BufferedReader;
// import java.io.FileInputStream;
// import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;

public class Main {
    FastScanner in;
    PrintWriter out;

    class Pair {
        int s, d, c, id;

        Pair(int s, int d, int c, int id) {
            this.s = s;
            this.d = d;
            this.c = c;
            this.id = id;
        }
    }

    private void solve() throws IOException {
        int n = in.nextInt(), m = in.nextInt();
        PriorityQueue<Pair> q = new PriorityQueue<>(Comparator.comparingInt(o -> o.d));
        Pair[] p = new Pair[m];
        boolean[] block = new boolean[n + 1];
        for (int i = 0; i < m; i++) {
            p[i] = new Pair(in.nextInt(), in.nextInt(), in.nextInt(), i);
            block[p[i].d] = true;
        }
        shuffle(p);
        sort(p, Comparator.comparingInt(o -> o.s));
        int[] ans = new int[n];
        for (int i = 1, j = 0; i <= n; i++) {
            if (!q.isEmpty() && q.peek().d <= i && q.peek().c > 0) {
                out.print(-1);
                return;
            }
            while (j < m && p[j].s == i) {
                q.add(p[j++]);
            }

            if (!block[i]) {
                Pair c = q.peek() == null ? new Pair(0, 0, 0, -1) : q.remove();
                c.c--;
                if (c.c > 0)
                    q.add(c);
                ans[i - 1] = c.id;
            } else
                ans[i - 1] = m;
        }
        for (int i : ans)
            out.print(i + 1 + " ");
    }

    void shuffle(Pair[] a) {
        Pair b;
        Random r = new Random();
        for (int i = a.length - 1, j; i > 0; i--) {
            j = r.nextInt(i + 1);
            b = a[j];
            a[j] = a[i];
            a[i] = b;
        }
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

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

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

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

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

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

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

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

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

    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

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


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	static class Task {

		int NN = 200005;
		int MOD = 1000000007;
		int INF = 2000000000;
		long INFINITY = 2000000000000000000L;

		long cur;
		long sumLike=0, sumHate=0;
		int n, m;

		long [][][][] dp;

		long expo(long x, long y) {
			long ret = 1, sq =x;
			while(y>0) {
				if(y%2!=0) {
					ret = (ret * sq) % 998244353;
				}
				sq = (sq * sq) % 998244353;
				y >>= 1;
			}
			return ret;
		}

		long invMod(long p, long q) {
			return (p * expo(q, 998244353 - 2))%998244353;
		}

		long rec(int c1, int c2, int c, int flag) {
			int mul = flag==0?-1:1;
			if(dp[c1][c2][c][flag] != -1)
				return dp[c1][c2][c][flag];
			if(c==m) {
				return dp[c1][c2][c][flag] = cur + mul*c1;
			}
			long p = (cur + mul*c1);
			long q;
			if(mul == 1)
				q = (sumLike + c2 + sumHate - (c-c2));
			else
				q = (sumHate - c2 + sumLike + (c-c2));
			long ret = (rec(c1+1, c2+1, c+1, flag)*invMod(p, q))%998244353;
			if(mul == 1)
				p = (sumLike + c2) - (cur + c1);
			else
				p = (sumHate - c2) - (cur - c1);
			ret = (ret + rec(c1, c2+1, c+1, flag)*invMod(p, q))%998244353;
			if(mul == 1)
				p = (sumHate - (c-c2));
			else
				p = sumLike + (c-c2);
			ret = (ret + rec(c1, c2, c+1, flag)*invMod(p, q))%998244353;
			return dp[c1][c2][c][flag] = ret;
		}

		public void solve(InputReader in, PrintWriter out) {
			n = in.nextInt();
			m = in.nextInt();
			int [] a = new int[n];
			for(int i=0;i<n;++i)
				a[i] = in.nextInt();
			int [] w = new int[n];
			for(int i=0;i<n;++i) {
				w[i] = in.nextInt();
				if(a[i] == 0) {
					sumHate += w[i];
				} else {
					sumLike += w[i];
				}
			}
			dp = new long[m+1][m+1][m+1][2];
			for(int i=0;i<n;++i) {
				cur=w[i];
				for(int c1=0;c1<=m;++c1) {
					for(int c2=0;c2<=m;++c2) {
						for(int c=0;c<=m;++c) {
							dp[c1][c2][c][0] = dp[c1][c2][c][1] = -1;
						}
					}
				}
				out.println(rec(0, 0, 0, a[i]));
			}
		}

	}

	static void prepareIO(boolean isFileIO) {
		//long t1 = System.currentTimeMillis();
		Task solver = new Task();
		// Standard IO
		if(!isFileIO) {
			InputStream inputStream = System.in;
	        OutputStream outputStream = System.out;
	        InputReader in = new InputReader(inputStream);
	        PrintWriter out = new PrintWriter(outputStream);
	        solver.solve(in, out);
	        //out.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        out.close();
		}
        // File IO
		else {
			String IPfilePath = System.getProperty("user.home") + "/Downloads/ip.in";
	        String OPfilePath = System.getProperty("user.home") + "/Downloads/op.out";
	        InputReader fin = new InputReader(IPfilePath);
	        PrintWriter fout = null;
	        try {
				fout = new PrintWriter(new File(OPfilePath));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
	        solver.solve(fin, fout);
	        //fout.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        fout.close();
		}
	}

	public static void main(String[] args) {
        prepareIO(false);
	}

	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

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

        public InputReader(String filePath) {
        	File file = new File(filePath);
            try {
				reader = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            tokenizer = null;
        }

        public String nextLine() {
        	String str = "";
        	try {
				str = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	return str;
        }

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

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

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

    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.InputStreamReader;
import java.io.BufferedOutputStream;
import java.util.StringTokenizer;
import java.io.Closeable;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.Flushable;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	static class TaskAdapter implements Runnable {
		@Override
		public void run() {
			InputStream inputStream = System.in;
			OutputStream outputStream = System.out;
			Input in = new Input(inputStream);
			Output out = new Output(outputStream);
			BPerfectNumber solver = new BPerfectNumber();
			solver.solve(1, in, out);
			out.close();
		}
	}

	public static void main(String[] args) throws Exception {
		Thread thread = new Thread(null, new TaskAdapter(), "", 1<<29);
		thread.start();
		thread.join();
	}
	static class BPerfectNumber {
		public BPerfectNumber() {
		}

		public boolean perfect(int x) {
			String s = String.valueOf(x);
			int sum = 0;
			for(int i = 0; i<s.length(); i++) {
				sum += s.charAt(i)-'0';
			}
			return sum==10;
		}

		public void solve(int testNumber, Input in, Output pw) {
			int n = in.nextInt(), j = 0, i;
			for(i = 1; j<n; i++) {
				if(perfect(i)) {
					j++;
				}
			}
			pw.println(i-1);
		}

	}

	static class Output implements Closeable, Flushable {
		public StringBuilder sb;
		public OutputStream os;
		public int BUFFER_SIZE;
		public boolean autoFlush;
		public String LineSeparator;

		public Output(OutputStream os) {
			this(os, 1<<16);
		}

		public Output(OutputStream os, int bs) {
			BUFFER_SIZE = bs;
			sb = new StringBuilder(BUFFER_SIZE);
			this.os = new BufferedOutputStream(os, 1<<17);
			autoFlush = false;
			LineSeparator = System.lineSeparator();
		}

		public void println(int i) {
			println(String.valueOf(i));
		}

		public void println(String s) {
			sb.append(s);
			println();
			if(autoFlush||sb.length()>BUFFER_SIZE >> 1) {
				flush();
			}
		}

		public void println() {
			sb.append(LineSeparator);
		}

		public void flush() {
			try {
				os.write(sb.toString().getBytes());
			}catch(IOException e) {
				e.printStackTrace();
			}
			sb = new StringBuilder(BUFFER_SIZE);
		}

		public void close() {
			flush();
			try {
				os.close();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}

	}

	static class Input {
		BufferedReader br;
		StringTokenizer st;

		public Input(InputStream is) {
			this(is, 1<<20);
		}

		public Input(InputStream is, int bs) {
			br = new BufferedReader(new InputStreamReader(is), bs);
			st = null;
		}

		public boolean hasNext() {
			try {
				while(st==null||!st.hasMoreTokens()) {
					String s = br.readLine();
					if(s==null) {
						return false;
					}
					st = new StringTokenizer(s);
				}
				return true;
			}catch(Exception e) {
				return false;
			}
		}

		public String next() {
			if(!hasNext()) {
				throw new InputMismatchException();
			}
			return st.nextToken();
		}

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

	}
}


import java.util.Scanner;

public class CF476A {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int stairsToClimb = in.nextInt();
		int multiple = in.nextInt();
		in.nextLine();
		int maxPossibleSteps = stairsToClimb;
		int minPossibleSteps = (int) Math.ceil(stairsToClimb/2.0);
		//System.out.println(minPossibleSteps);
		int toPrint = -1;

		for(int i = minPossibleSteps; i <= maxPossibleSteps; i++){
			if(i % multiple == 0){
				toPrint = i;
				break;
			}
		}
		System.out.println(toPrint);
	}
}


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

public class Forces_579_A {
	public static void main(String args[]) throws NumberFormatException, IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pr = new PrintWriter(System.out);
		long x=Long.parseLong(br.readLine());
		int c=1;
		while(x!=1)
		{
			if(x%2==0) x=x/2;
			else
			{
				c++;
				x=x-1;
			}
		}

		pr.println(c);
		pr.close();
	}
}


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

public class D implements Runnable {

	private void Solution() throws IOException {
		long n = nextLong(), m = nextLong(), k = nextLong();
		long MOD = 1000000000 + 7, ans = 0;
		if (k > n) {
			ans = 1;
			for (int i = 0; i < n; i++) {
				ans *= m;
				ans %= MOD;
			}
		} else if (k == n) {
			ans = 1;
			for (int i = 0; i < (n + 1) / 2; i++) {
				ans *= m;
				ans %= MOD;
			}
		} else if (k == 1) {
			ans = 1;
			for (int i = 0; i < n; i++) {
				ans *= m;
				ans %= MOD;
			}
		} else if (k % 2 == 0) {
			ans = m;
		} else {
			ans = m * m;
		}
		print(ans);
	}

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

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tokenizer;

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			Solution();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	void print(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(o[i]);
		}
	}

	void println(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(o[i]);
		}
		out.println();
	}

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

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

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

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

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


import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.HashMap;
    public class templ {
    int binarySearch(int arr[], int l, int r, int x)
    {
        if (r >= l)
        {
            int mid = l + (r - l)/2;
            if (arr[mid] == x)
            return mid;
            if (arr[mid] > x)
            return binarySearch(arr, l, mid-1, x);
            return binarySearch(arr, mid+1, r, x);
        }
        return -1;
    }
    void merge1(int arr[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
    void sort1(int arr[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort1(arr, l, m);
            sort1(arr , m+1, r);
            merge1(arr, l, m, r);
        }
    }
    void merge3(int arr[],int arr1[],int arr2[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        int L2[]=new int[n1];
        int R2[]=new int[n2];
        //long L3[]=new long[n1];
        //long R3[]=new long[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
            L2[i]=arr2[l+i];
            //L3[i]=arr3[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
            R2[j]=arr2[m+1+j];
            //R3[j]=arr3[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                arr2[k]=L2[i];
                //arr3[k]=L3[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                arr2[k]=R2[j];
                //arr3[k]=R3[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            arr2[k]=L2[i];
            //arr3[k]=L3[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            arr2[k]=R2[j];
            //arr3[k]=R3[j];
            j++;
            k++;
        }
    }
    void sort3(int arr[],int arr1[],int arr2[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort3(arr,arr1,arr2, l, m);
            sort3(arr ,arr1,arr2, m+1, r);
            merge3(arr,arr1,arr2,l, m, r);
        }
    }
    void merge2(int arr[],int arr1[],int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            j++;
            k++;
        }
    }
    void sort2(int arr[],int arr1[],int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort2(arr,arr1, l, m);
            sort2(arr ,arr1, m+1, r);
            merge2(arr,arr1,l, m, r);
        }
    }
    public static void main(String[] args) throws FileNotFoundException {
     try
     {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        templ ob=new templ();
        long a=in.nextLong();
        long b=in.nextLong();
        int m[]=new int[20];
        int o=0;
        int n[]=new int[20];
        int oo=0;
        long c=a;
        while(c!=0)
        {
            int d=(int)(c%10);
            m[o]=d;
            o++;
            c=c/10;
        }
        ob.sort1(m,0,o-1);
        long d=b;
        while(d!=0)
        {
            int k=(int)(d%10);
            n[oo]=k;
            oo++;
            d=d/10;
        }
        for(int i=0;i<oo/2;i++)
        {
            int temp=n[i];
            n[i]=n[oo-1-i];
            n[oo-1-i]=temp;
        }
        if(oo>o)
        {
            for(int i=o-1;i>=0;i--)
                out.print(m[i]);
        }
        else
        {
        int dest[]=new int[20];
        int p=0;
        int vis[]=new int[o];
        for(int i=0;i<oo;i++)
        {
            int x=n[i];
            int prev=-1;
            int j1=-1;
            for(int j=0;j<o;j++)
            {
                int y=m[j];
                if(y>x)
                    break;
                else if(y<=x&&vis[j]==0)
                {
                    prev=y;
                    j1=j;
                }
            }
            if(prev!=-1)
            {
                dest[p]=prev;
                p++;
                vis[j1]=1;
                if(prev<x)
                {
                    for(int l=o-1;l>=0;l--)
                    {
                        if(vis[l]==0)
                        {
                            dest[p]=m[l];
                            p++;
                            vis[l]=1;
                        }
                    }
                    break;
                }
            }
            else
            {
                int max=-1;
                int jj=-1;
                for(int l=p-1;l>=0;l--)
                {
                    //for(int ll=0;ll<o;ll++)
                        //out.println(m[ll]+" "+vis[ll]);
                    for(int ll=0;ll<o;ll++)
                    {
                        if(vis[ll]==0&&m[ll]<dest[l]&&m[ll]>max)
                        {
                            max=m[ll];
                            jj=ll;
                        }
                    }
                    if(max!=-1)
                    {
                        vis[jj]=1;
                        int kk=dest[l];
                        dest[l]=max;
                        for(int ll=0;ll<o;ll++)
                        {
                            if(vis[ll]==1&&m[ll]==kk)
                            {
                                vis[ll]=0;
                                break;
                            }
                        }
                        break;
                    }
                    else
                    {
                        int kk=dest[l];
                        //out.println(kk);
                        p--;
                        for(int ll=0;ll<o;ll++)
                        {
                            if(vis[ll]==1&&m[ll]==kk)
                            {
                                vis[ll]=0;
                                break;
                            }
                        }
                    }
                }
                for(int l=o-1;l>=0;l--)
                {
                    if(vis[l]==0)
                    {
                        dest[p]=m[l];
                        p++;
                        vis[l]=1;
                    }
                }
                break;
            }
        }
        for(int i=0;i<p;i++)
            out.print(dest[i]);
        }
        out.close();
    }
        catch(Exception e){
        return;
    }
    }
    static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

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

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

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextInt();
            }
            return a;
        }

        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 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 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.util.*;
import java.util.LinkedList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.math.BigInteger;

public class tmp {
    public static void main(String [] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(in.readLine());
        StringBuilder sb = new StringBuilder();

        int n = Integer.parseInt(st.nextToken());
        int a = Integer.parseInt(st.nextToken());
        int [] arr = new int[n+1];
        st = new StringTokenizer(in.readLine());

        for(int i=1; i<=n; i++){
            arr[i] = Integer.parseInt(st.nextToken());
        }
        int out = 0;
        if(a <= n/2){
            for(int i=1; i<a; i++){
                if(arr[i] + arr[2*a-i] == 2)
                    out+=2;
            }
            out += arr[a];
            for(int i=2*a; i<=n; i++){
                out += arr[i];
            }
        }else{
            for(int i=n; i>a; i--){
                if(arr[i] + arr[2*a-i] == 2)
                    out+=2;
            }
            out += arr[a];
            for(int i=1; i<2*a-n; i++){
                out += arr[i];
            }
        }

        System.out.println(out);

    }

}

class P implements Comparable<P>{
    int val, idx;
    public P(int val, int idx){
        this.val = val;
        this.idx = idx;
    }

    public int compareTo(P other){
        if (this.val != other.val) return -this.val + other.val;
        return this.idx - other.idx;
    }
}