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

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

    static class TaskF {
        public void solve(int testNumber, MyScan in, PrintWriter out) {
            int n = in.nextInt();
            String pos = in.next();
            int[][] auto = toAuto(pos);

            long[][][] state = new long[2 * n + 1][2 * n + 1][pos.length() + 1];
            long[][] ansFull = new long[2 * n + 1][2 * n + 1];

            for (int s = 0; s <= n; s++) {
                long[][] starts = new long[2 * n + 1][2 * n + 1];
                starts[0][s] = 1;
                for (int len = 0; len < 2 * n; len++) {
                    for (int gand = 0; gand <= n; gand++) {
                        if (gand > 0) {
                            starts[len + 1][gand - 1] = (starts[len + 1][gand - 1] + starts[len][gand]) % Util._m;
                        }
                        starts[len + 1][gand + 1] = (starts[len + 1][gand + 1] + starts[len][gand]) % Util._m;
                    }
                }
                for (int r = 0; r <= 2 * n; r++) {
                    ansFull[r][s] = starts[r][0];
                }

            }

            state[0][0][0] = 1;
            long res = 0;
            for (int px = 0; px < 2 * n; px++) {
                for (int l = 0; l < 2 * n; l++) {
                    for (int s = 0; s < pos.length(); s++) {
                        int next0 = auto[s][0];
                        int next1 = auto[s][1];
                        // (
                        state[px + 1][l + 1][next0] = (state[px + 1][l + 1][next0] + state[px][l][s]) % Util._m;
                        // )
                        if (l > 0)
                            state[px + 1][l - 1][next1] = (state[px + 1][l - 1][next1] + state[px][l][s]) % Util._m;
                    }
                }
                for (int l = 0; l < 2 * n; l++) {
                    int l2 = 2 * n - px - 1;
                    int n2 = l;
                    if (state[px + 1][l][pos.length()] == 0 || n2 > l2) continue;
                    res += state[px + 1][l][pos.length()] * ansFull[l2][n2] % Util._m;
                }
            }
            out.println(res % Util._m);
        }

        private int[][] toAuto(String pos) {
            int[][] r = new int[pos.length()][2];

            String chars = "()";
            for (int t = 0; t < pos.length(); t++) {
                for (int i = 0; i < chars.length(); i++) {
                    char ch = chars.charAt(i);
                    if (pos.charAt(t) == ch) {
                        r[t][i] = t + 1;
                    } else {
                        String tar = pos.substring(0, t) + ch;
                        for (int l = t; l > 0; l--) {
                            if (tar.endsWith(pos.substring(0, l))) {
                                r[t][i] = l;
                                break;
                            }
                        }
                    }
                }
            }

            return r;
        }

    }

    static class MyScan {
        private final InputStream in;
        private byte[] inbuf = new byte[1024];
        public int lenbuf = 0;
        public int ptrbuf = 0;

        public MyScan(InputStream in) {
            this.in = in;
        }

        private int readByte() {
            if (lenbuf == -1) throw new InputMismatchException();
            if (ptrbuf >= lenbuf) {
                ptrbuf = 0;
                try {
                    lenbuf = in.read(inbuf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0) return -1;
            }
            return inbuf[ptrbuf++];
        }

        public boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }

        public int skip() {
            int b;
            while ((b = readByte()) != -1 && isSpaceChar(b)) ;
            return b;
        }

        public String next() {
            int b = skip();
            StringBuilder sb = new StringBuilder();
            while (!(isSpaceChar(b))) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public int nextInt() {
            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();
            }
        }

    }

    static class Util {
        public static final long M07 = 1000_000_007;
        public static final long _m = M07;

    }
}


import java.util.Scanner;

public class ShowerLine {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		coeffs = new int[5][5];
		for(int i = 0; i < 5; i++)
		{
			for (int j = 0; j < 5; j++)
			{
				coeffs[i][j] = scan.nextInt();
			}
		}
		scan.close();
		int[] studsInQueue = new int[5];
		int maxCoefficient = 0;
		int sumCoefficient = 0;
		for (int i = 0; i < 5; i++)
		{
			for (int j = 0; j < 5; j++)
			{
				if (i != j)
				{
					for (int k = 0; k < 5; k++)
					{
						if (k != i && k != j)
						{
							for (int l = 0; l < 5; l++)
							{
								if (l != i && l != j && l != k)
								{
									for (int m = 0; m < 5; m++)
									{
										if (m != i && m != j && m != k && m != l)
										{
											sumCoefficient = coeff(i, j, k, l, m);
											if (sumCoefficient > maxCoefficient)
											{
												maxCoefficient = sumCoefficient;
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		System.out.println(maxCoefficient);
	}

	public static int coeff(int i, int j, int k, int l, int m)
	{
		return 2*(coeffs[l][m] + coeffs[m][l] + coeffs[k][l] + coeffs[l][k])+
				coeffs[i][j] + coeffs[j][i] + coeffs[j][k] + coeffs[k][j];
	}

	private static int coeffs[][];

}


import java.util.*;

/**
 *
 * @author greggy
 */
public class PlayingWithPaper {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long a, b;
        a = in.nextLong();
        b = in.nextLong();

        long ans = 0;
        while (a > 0 && b > 0) {
            if (a > b) {
                ans += a / b;
                a = a % b;
            } else {
                ans += b / a;
                b %= a;
            }
        }
        System.out.println(ans);
    }
}


import java.util.HashSet;
import java.util.Scanner;

public class Antipalindrome {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String s=scan.nextLine();
		HashSet<Character> h= new HashSet<Character>();
		for(int i=0;i<s.length();i++) {
			h.add(s.charAt(i));
		}
		if(h.size()==1) {
			System.out.println("0");
			return;
		}
		StringBuilder s1= new StringBuilder(s);
		s1.reverse();
		if(s.contains(s1)) System.out.println(s.length()-1);
		else System.out.println(s.length());

	}

}


import java.math.BigInteger;
import java.util.Scanner;

public class q2B {

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

//        System.out.println(S(2197413));

//        double number = input.nextDouble();
//        double half = Math.floor(number / 2);
//        double remHalf = number - half;
//        int max = 0;
//        double i = 0;
//        while (true) {
//            int calc = S(half-i) + S(remHalf+i);
//            if (calc >= max)  max = calc;
//            else break;
//            i++;
//        }
//        System.out.println(max);

        long number = input.nextLong();
        long rem10 = number % 10;
//        System.out.println((S(9) + S(number-9)));
        int max  = 0;
        for (int i = 0; i < countS(number); i++) {
            double base  = Math.pow(10,i)-1;
            int calc =  S(base) + S(number - base);
            if (calc > max) max = calc;
        }
        System.out.println(max);

    }
    private static int S(double a)
    {
            int res = 0;
            while (a > 0)
            {
                res += a % 10;
                a = Math.floor(a/10);
            }
            return res;
    }
    private static int countS(double a)
    {
        int res = 0;
        while (a > 0)
        {
            res ++;
            a = Math.floor(a/10);
        }
        return res;
    }
}
//10000000000


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

public class B {

	static final boolean stdin = true;
	static final String filename = "";
	static FastScanner br;
	static PrintWriter pw;

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

		if (stdin) {
			br = new FastScanner();
			pw = new PrintWriter(new OutputStreamWriter(System.out));
		} else {
			br = new FastScanner(filename + ".in");
			pw = new PrintWriter(new FileWriter(filename + ".out"));
		}

		Solver solver = new Solver();
		solver.solve(br, pw);
		pw.close();
	}

	static class Solver {
		static long mod = (long) (1e9);

		public void solve(FastScanner br, PrintWriter pw) throws IOException {
			int n = br.ni();
			long out = 0;
			while(n > 0) {
				out += n;
				n-=2;
			}
			pw.println(out);
		}

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

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

		static long pow(long a, long b) {
			if (b == 0)
				return 1L;
			long val = pow(a, b / 2);
			if (b % 2 == 0)
				return val * val % mod;
			else
				return val * val % mod * a % mod;
		}

	}

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

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

		@Override
		public int compareTo(Point o) {
			return this.a - o.a;
		}

		public boolean equals(Object obj) {
			if (obj instanceof Point) {
				Point other = (Point) obj;
				return a == other.a && b == other.b;
			}
			return false;
		}

		public int hashCode() {
			return 65536 * a + b + 4733 * 0;
		}

	}

	public static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String s) {
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

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

		ArrayList<Integer>[] ng(int n, int e) {
			ArrayList<Integer>[] adj = new ArrayList[n];
			for (int i = 0; i < n; i++) {
				adj[i] = new ArrayList<Integer>();
			}
			for (int i = 0; i < e; i++) {
				int a = ni() - 1;
				int b = ni() - 1;
				adj[a].add(b);
				adj[b].add(a);
			}
			return adj;
		}

		Integer[] nIa(int n) {
			Integer[] arr = new Integer[n];
			for (int i = 0; i < n; i++) {
				arr[i] = ni();
			}
			return arr;
		}

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

		Long[] nLa(int n) {
			Long[] arr = new Long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nl();
			}
			return arr;
		}

		long[] nla(int n) {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nl();
			}
			return arr;
		}

		String[] nsa(int n) {
			String[] arr = new String[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nt();
			}
			return arr;
		}

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

		int ni() {
			return Integer.parseInt(nt());
		}

		long nl() {
			return Long.parseLong(nt());
		}

		double nd() {
			return Double.parseDouble(nt());
		}

	}
}


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

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

class TaskD {
    long pow(long a, int n, long m) {
        if (n == 0)
            return 1;
        else if (n % 2 == 1)
            return a * pow(a, n-1, m) % m;
        else
            return pow(a*a%m, n/2, m) % m;
    }

    public void solve(@SuppressWarnings("UnusedParameters") int testNumber, Scanner in, PrintWriter out) {
        int n = in.nextInt(), x = in.nextInt();
        if (!isPrime(n+1)) {
            out.println(-1);
            return;
        }

        Set<Integer> d = new HashSet<Integer>();
        for (int i = 2; i*i < n; ++i)
            if (n % i == 0 && isPrime(i))
                d.add(n / i);

        for (x--; x > 1; --x) {
            if (x % (n+1) != 0) {
                boolean ok = true;
                for (int i: d)
                    if (pow(x, i, n+1) == 1) {
                        ok = false;
                        break;
                    }
                if (ok) {
                    out.println(x);
                    return;
                }
            }
        }
        out.println(-1);
    }

    private boolean isPrime(long x) {
        for (long i = 2; i*i <= x; ++i)
            if (x % i == 0)
                return false;
        return true;
    }
}


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

public class C_submit {

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

	static long[][] c = new long[61][];
	static {
		for (int i = 0; i < c.length; i++) {
			c[i] = new long[i + 1];
			c[i][0] = c[i][i] = 1;
			for (int j = 1; j < i; j++)
				c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
		}
	}

	long c(int n, int k) {
		return n < 0 || k < 0 || k > n ? 0 : c[n][k];
	}

	void solve() throws IOException {
		long n = nextLong();
		long t = nextLong();
		if (Long.bitCount(t) != 1) {
			out.println(0);
			return;
		}

		int need = Long.numberOfTrailingZeros(t) + 1;

		// return numbers of x : 2 <= x <= n + 1, bitcount(x) = need

		n += 2;

		String s = Long.toBinaryString(n);

		int prefBitCnt = 0;

		long ans = 0;

		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '1') {
				int sufLen = s.length() - i - 1;
				ans += c(sufLen, need - prefBitCnt);
				prefBitCnt++;
			}
		}

		if (need == 1)
			ans--;

		out.println(ans);
	}

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

	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.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;

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

class Monitor {
    public void solve(int testNumber, FastScanner in, FastPrinter out) {
        int a = in.nextInt();
        int b = in.nextInt();
        int x = in.nextInt();
        int y = in.nextInt();
        int g = MathUtils.gcd(x, y);
        x /= g;
        y /= g;
        int z = Math.min(a / x, b / y);
        if (z == 0) {
            out.println("0 0");
            return;
        }
        out.println(x * z + " " + y * z);
    }
}

class FastScanner extends BufferedReader {

    boolean isEOF;

    public FastScanner(InputStream is) {
        super(new InputStreamReader(is));
    }

    public int read() {
        try {
            int ret = super.read();
            if (isEOF && ret < 0) {
                throw new InputMismatchException();
            }
            isEOF = ret == -1;
            return ret;
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    static boolean isWhiteSpace(int c) {
        return c >= -1 && c <= 32;
    }

    public int nextInt() {
        int c = read();
        while (isWhiteSpace(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int ret = 0;
        while (!isWhiteSpace(c)) {
            if (c < '0' || c > '9') {
                throw new NumberFormatException("digit expected " + (char) c
                        + " found");
            }
            ret = ret * 10 + c - '0';
            c = read();
        }
        return ret * sgn;
    }

    }

class FastPrinter extends PrintWriter {

    public FastPrinter(OutputStream out) {
        super(out);
    }

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

}

class MathUtils {

    public static int gcd(int a, int b) {
        while (b != 0) {
            int t = a % b;
            a = b;
            b = t;
        }
        return a;
    }

    }


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

public class TaskD {

    public void solve() throws IOException {
        int n = 5;
        boolean[][] h = new boolean[n][n];
        int m = in.nextInt();
        for (int i = 0; i < m; ++i) {
            h[in.nextInt() - 1][in.nextInt() - 1] = true;
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                h[i][j] |= h[j][i];
            }
        }
        boolean ans = false;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                for (int k = j + 1; k < n; ++k) {
                    ans |= (h[i][j] == h[j][k]) && (h[j][k] == h[k][i]);
                }
            }
        }
        out.println(ans ? "WIN" : "FAIL");
    }

    static FastReader in;
    static PrintWriter out;
    static PrintStream err;

    public static void main(String[] args) throws IOException {
        try {
            in = new FastReader();
            out = new PrintWriter(System.out);
            err = System.err;
            new TaskD().solve();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

class FastReader {
    BufferedReader br;
    StringTokenizer in;

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

    FastReader(String filename) {
        try {
            this.br = new BufferedReader(new FileReader(filename));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

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

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

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

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


import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int size1=sc.nextInt();
        int size2=sc.nextInt();
        int[] arr1=new int[size1];
        int[] arr2=new int[size2];
        for(int i=0;i<size1;i++)arr1[i]=sc.nextInt();
        for(int i=0;i<size2;i++)arr2[i]=sc.nextInt();
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        boolean flag2=false;
        for(int i=0;i<size1;i++){
            boolean flag=false;
            for(int j=0;j<size2;j++){
                if(arr1[i]==arr2[j]){
                    System.out.println(arr1[i]);
                    flag=true;
                    break;
                }
            }
            if(flag){flag2=true;break;}
        }
        if(!flag2){
        if(arr1[0]<arr2[0]) System.out.println(arr1[0]+""+arr2[0]);

        else if(arr1[0]>arr2[0]) System.out.println(arr2[0]+""+arr1[0]);}
    }
}


import java.util.*;
public class Div4
{
public static void main(String args[])
{
Scanner e=new Scanner(System.in);
String s=e.next();
int f=s.indexOf('1');
int x=0;
if(f==-1)
System.out.println("no");
else
{
for(int i=f;i<s.length();i++)
{
if(s.charAt(i)=='0')
x++;
}
if(x>=6)
System.out.println("yes");
else
System.out.println("no");
}
}
}


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

import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;

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

class Solver {
    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;

    void run() throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        //in = new BufferedReader(new FileReader("digit.in"));
        //out = new PrintWriter(new File("digit.out"));
        solve();
        out.close();
    }

    public void solve() throws IOException {

        String S = next();
        int k = nextInt();

        char[] st = new char[S.length()];
        st = S.toCharArray();
        int n = st.length;

        char[] ans = new char[n];
        int ptr = 0;

        while (k > 0 && ptr < n) {
            int maxi = st[ptr], pos = ptr;
            for (int i = ptr + 1; i <= ptr + k && i < n; ++i) {
                if (st[i] > maxi) {
                    maxi = st[i];
                    pos = i;
                }
            }
            for (int i = pos; i >= ptr + 1; --i) {
                st[i] = st[i - 1];
            }
            k -= pos - ptr;
            st[ptr] = (char) maxi;
            ++ptr;
        }

        out.println(st);
    }

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

    BigInteger nextBigInteger() throws IOException {
        return new BigInteger(next());
    }

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

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

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


import java.io.*;
import java.util.*;
public class GeneticEngineering
{
    public static void main(String[] args) throws IOException
    {
        //BufferedReader in = new BufferedReader(new FileReader("Sample Input.txt"));
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line = in.readLine();
        int same=1, ins=0;
        for (int i = 1; i < line.length(); i++)
        {
           if ( line.charAt(i) == line.charAt(i-1)) same++;
           else
           {
               if ( same%2 == 0)ins++;
               same=1;
           }
        }
        if ( same%2 == 0) ins++;
        System.out.println(ins);
    }

}


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

public class E {

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

    static final int[] MAGIC = { 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127,
            521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213,
            19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839,
            859433, 1257787, 1398269, 2976221, 3021377, 6972593, 13466917,
            20996011 };

    // boolean test(int z) {
    // for (int x = 1; x + 1<= z; x++) {
    // int A = x + 1;
    // int B = x >> 1;
    // if (z % A == B && z >= A + B)
    // return false;
    // }
    // return true;
    // }

    static final int MOD = 1000000007;

    int pow(int a, int b) {
        int res = 1;
        while (b != 0) {
            if ((b & 1) == 1)
                res = (int)((long)res * a % MOD);
            a = (int)((long)a * a % MOD);
            b >>= 1;
        }
        return res;
    }

    void solve() throws IOException {
        int n = nextInt() - 1;
        int res = pow(2, MAGIC[n] - 1);
        res--;
        if (res < 0)
            res += MOD;
        out.println(res);
    }

    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.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class b {

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

	      Scanner s=new Scanner(System.in);

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

	      int[] ans=new int[n];

	      for(int i=0;i<n;i++) {
	    	  ArrayList<Integer> list=new ArrayList<>();
	    	  for(int j=0;j<i;j++) {
	    		  list.add(arr[j]);
	    	  }

	    	  Collections.sort(list);
	    	  int st=0;

	    	  int sum=arr[i];
	    	  while(st<list.size()&&sum+list.get(st)<=k) {
	    		  sum=sum+list.get(st);
	    		  st++;
	    	  }
	    	  ans[i]=list.size()-st;

	      }

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

	}
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

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

	void solve()
	{
		int n = ni();
		int m = ni();
		char[][] ss = nm(n,m);
		int[][] M = new int[m][n];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				M[j][i] = ss[i][j]-'a';
			}
		}
		Result res = rank(M, 5);
		int[][] o = res.v;
		int rank = res.rank;
		outer:
		for(int Q = ni();Q > 0;Q--){
			char[] ys = ns(m);
			int[] y = new int[m];
			for(int i = 0;i < m;i++)y[i] = ys[i]-'a';
			y = mul(o, y, 5);
			for(int i = rank;i < m;i++){
				if(y[i] != 0){
					out.println(0);
					continue outer;
				}
			}
			out.println(pow(5, n-rank, 1000000007));
		}
	}

	public static long pow(long a, long n, long mod) {
		//		a %= mod;
		long ret = 1;
		int x = 63 - Long.numberOfLeadingZeros(n);
		for (; x >= 0; x--) {
			ret = ret * ret % mod;
			if (n << 63 - x < 0)
				ret = ret * a % mod;
		}
		return ret;
	}

	public static Result rank(int[][] M, int p)
	{
//		if(M.length == 0)return 0;
		int n = M.length, m = M[0].length;
		int[][] v = new int[n][n];
		for(int i = 0;i < n;i++)v[i][i] = 1;

		// Forward Elimination
		for(int i = 0;i < n;i++){
			// select pivot
			boolean pivotFound = false;
			out:
			for(int pi = i;pi < n;pi++){
				for(int pj = i;pj < m;pj++){
					if(M[pi][pj] != 0){
						// pivot found
						if(pj != i){
							// swap columns
							for(int k = 0;k < n;k++){
								int d = M[k][pj]; M[k][pj] = M[k][i]; M[k][i] = d;
							}
						}
						if(pi != i){
							// swap rows
							{int[] d = M[pi]; M[pi] = M[i]; M[i] = d;}
							{int[] d = v[pi]; v[pi] = v[i]; v[i] = d;}
						}
						pivotFound = true;
						break out;
					}
				}
			}
			if(!pivotFound){
				Result res = new Result();
				res.v = v;
				res.rank = i;
				return res;
			}

			long ID = invl(M[i][i], p);
			M[i][i] = 1;
			for(int j = i+1;j < m;j++){
				M[i][j] = (int)(M[i][j] * ID % p);
			}
			for(int j = 0;j < n;j++){
				v[i][j] = (int)(v[i][j] * ID % p);
			}

			for(int j = i+1;j < n;j++){
				long B = p-M[j][i];
				M[j][i] = 0;
				for(int k = i+1;k < m;k++){
					M[j][k] = (int)((M[j][k] + M[i][k] * B) % p);
				}
				for(int k = 0;k < n;k++){
					v[j][k] = (int)((v[j][k] + v[i][k] * B) % p);
				}
			}
		}
		Result res = new Result();
		res.v = v;
		res.rank = n;
		return res;
	}

	public static int[] mul(int[][] A, int[] v, int mod)
	{
		int m = A.length;
		int n = v.length;
		int[] w = new int[m];
		for(int i = 0;i < m;i++){
			long sum = 0;
			for(int k = 0;k < n;k++){
				sum += (long)A[i][k] * v[k];
			}
			sum %= mod;
			w[i] = (int)sum;
		}
		return w;
	}

	public static long invl(long a, long mod) {
		long b = mod;
		long p = 1, q = 0;
		while (b > 0) {
			long c = a / b;
			long d;
			d = a;
			a = b;
			b = d % b;
			d = p;
			p = q;
			q = d - c * q;
		}
		return p < 0 ? p + mod : p;
	}

	static class Result
	{
		public int[][] v;
		public int rank;
	};

	public static Result operateElementarily(int[][] M, int mod)
	{
		int n = M.length, m = M[0].length;
		int rank = n-1;
		int[][] v = new int[n][n];
		for(int i = 0;i < n;i++)v[i][i] = 1;

		// Forward Elimination
		for(int i = 0;i < n;i++){
			// select pivot
			int maxj = -1;
			for(int j = i;j < n;j++){
				if(M[j][i] != 0){
					maxj = j;
					break;
				}
			}
			if(maxj == -1){
				rank = i-1;
				break;
			}
			if(maxj != i){
				int[] dum = M[i]; M[i] = M[maxj]; M[maxj] = dum;
				int[] du = v[i]; v[i] = v[maxj]; v[maxj] = du;
			}

			long ID = invl(M[i][i], mod);
			M[i][i] = 1;
			for(int j = i+1;j < m;j++){
				M[i][j] = (int)(M[i][j] * ID % mod);
			}
			for(int j = 0;j < n;j++){
				v[i][j] = (int)(v[i][j] * ID % mod);
			}

			for(int j = i+1;j < n;j++){
				long B = mod-M[j][i];
				M[j][i] = 0;
				for(int k = i+1;k < m;k++){
					M[j][k] = (int)((M[j][k] + M[i][k] * B) % mod);
				}
				for(int k = 0;k < n;k++){
					v[j][k] = (int)((v[j][k] + v[i][k] * B) % mod);
				}
			}
		}

		// Back Substitution
		for(int i = rank;i >= 0;i--){
			for(int j = rank;j >= i+1;j--){
				long B = mod-M[i][j];
				M[i][j] = 0;
				for(int k = rank+1;k < m;k++){
					M[i][k] = (int)((M[i][k] + M[j][k] * B) % mod);
				}
				for(int k = 0;k < n;k++){
					v[i][k] = (int)((v[i][k] + v[j][k] * B) % mod);
				}
			}
		}

		Result ret = new Result();
		ret.v = v;
		ret.rank = rank+1;
		return ret;
	}

	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

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

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

	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.*;

public class MainC {
	static final StdIn in = new StdIn();
	static final PrintWriter out = new PrintWriter(System.out);
	static final long INF=(long)1e18;

	public static void main(String[] args) {
		int x=in.nextInt(), k=in.nextInt(), n=in.nextInt(), q=in.nextInt(), masks=0;
		int[] id2mask = new int[1<<k], mask2id = new int[1<<k];
		Arrays.fill(mask2id, -1);
		for(int i=1; i<1<<k; ++i)
			if(Integer.bitCount(i)==x)
				id2mask[mask2id[i]=masks++]=i;
		//System.out.println(Arrays.toString(id2mask));
		long[] c = in.readLongArray(k);
		TreeMap<Integer, Long> ps = new TreeMap<Integer, Long>();
		ps.put(0, 0L);
		ps.put(n-x, 0L);
		for(int i=0; i<q; ++i)
			ps.put(in.nextInt()-1, in.nextLong());
		long[][] tmat = new long[masks][masks], dp = new long[2][masks];
		for(int i=0; i<masks; ++i) {
			Arrays.fill(tmat[i], INF);
			if(((id2mask[i]>>(k-1))&1)!=0)
				tmat[i][mask2id[(id2mask[i]<<1)^1^(1<<k)]]=c[k-1];
			else {
				for(int j=0; j<k-1; ++j)
					if(((id2mask[i]>>j)&1)!=0)
						tmat[i][mask2id[((id2mask[i]^(1<<j))<<1)^1]]=c[j];
				tmat[i][mask2id[id2mask[i]<<1]]=0;
			}
		}
		//System.out.println(Arrays.deepToString(tmat));
		Arrays.fill(dp[0], INF);
		dp[0][0]=0;
		int lastI=0;
		for(Map.Entry<Integer, Long> entry : ps.entrySet()) {
			if(entry.getKey()>n-x)
				continue;
			//System.out.println(entry.getKey());
			if(entry.getKey()-lastI>k)
				dp[(entry.getKey()-k)&1]=matMult(matExp(tmat, entry.getKey()-lastI-k), dp[lastI&1]);
			//System.out.println(Math.max(entry.getKey()-k, lastI)+" "+Arrays.toString(dp[Math.max(entry.getKey()-k, lastI)&1]));
			for(int a=Math.max(entry.getKey()-k, lastI)+1; a<=entry.getKey(); ++a) {
				for(int i=0; i<masks; ++i) {
					if(((id2mask[i]>>(k-1))&1)!=0)
						dp[a&1][i]=dp[a&1^1][mask2id[(id2mask[i]<<1)^1^(1<<k)]]+c[k-1]+ps.getOrDefault(a+k-1, 0L);
					else {
						dp[a&1][i]=dp[a&1^1][mask2id[id2mask[i]<<1]];
						for(int j=0; j<k-1; ++j)
							if(((id2mask[i]>>j)&1)!=0)
								dp[a&1][i]=Math.min(dp[a&1^1][mask2id[((id2mask[i]^(1<<j))<<1)^1]]+c[j]+ps.getOrDefault(a+j, 0L), dp[a&1][i]);
					}
				}
				//System.out.println(a+" "+Arrays.toString(dp[a&1]));
			}
			lastI=entry.getKey();
		}
		out.println(dp[(n-x)&1][0]);
		out.close();
	}

	static long[][] matMult(long[][] x, long[][] y) {
		long[][] z = new long[x.length][x.length];
		for(int i=0; i<x.length; ++i) {
			Arrays.fill(z[i], INF);
			for(int j=0; j<x.length; ++j)
				for(int k=0; k<x.length; ++k)
					z[i][j]=Math.min(x[i][k]+y[k][j], z[i][j]);
		}
		return z;
	}

	static long[][] matExp(long[][] x, int p) {
		long[][] res = new long[x.length][x.length], pow = x;
		for(int i=0; i<x.length; ++i) {
			Arrays.fill(res[i], INF);
			res[i][i]=0;
		}
		while(p>0) {
			if(p%2==1)
				res=matMult(res, pow);
			pow=matMult(pow, pow);
			p/=2;
		}
		return res;
	}

	static long[] matMult(long[][] x, long[] y) {
		long[] z = new long[x.length];
		Arrays.fill(z, INF);
		for(int i=0; i<x.length; ++i)
			for(int j=0; j<x.length; ++j)
				z[i]=Math.min(x[i][j]+y[j], z[i]);
		return z;
	}

	interface Input {
		public String next();
		public String nextLine();
		public int nextInt();
		public long nextLong();
		public double nextDouble();
	}
	static class StdIn implements Input {
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;
		public StdIn() {
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}
		public StdIn(InputStream in) {
			try{
				din = new DataInputStream(in);
			} catch(Exception e) {
				throw new RuntimeException();
			}
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}
		public String next() {
			int c;
			while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
			StringBuilder s = new StringBuilder();
			while (c != -1)
			{
				if (c == ' ' || c == '\n'||c=='\r')
					break;
				s.append((char)c);
				c=read();
			}
			return s.toString();
		}
		public String nextLine() {
			int c;
			while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
			StringBuilder s = new StringBuilder();
			while (c != -1)
			{
				if (c == '\n'||c=='\r')
					break;
				s.append((char)c);
				c = read();
			}
			return s.toString();
		}
		public int nextInt() {
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do
				ret = ret * 10 + c - '0';
			while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}
		public int[] readIntArray(int n) {
			int[] ar = new int[n];
			for(int i=0; i<n; ++i)
				ar[i]=nextInt();
			return ar;
		}
		public long nextLong() {
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do
				ret = ret * 10 + c - '0';
			while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}
		public long[] readLongArray(int n) {
			long[] ar = new long[n];
			for(int i=0; i<n; ++i)
				ar[i]=nextLong();
			return ar;
		}
		public double nextDouble() {
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do
				ret = ret * 10 + c - '0';
			while ((c = read()) >= '0' && c <= '9');
			if (c == '.')
				while ((c = read()) >= '0' && c <= '9')
					ret += (c - '0') / (div *= 10);
			if (neg)
				return -ret;
			return ret;
		}
		private void fillBuffer() throws IOException {
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}
		private byte read() {
			try{
				if (bufferPointer == bytesRead)
					fillBuffer();
				return buffer[bufferPointer++];
			} catch(IOException e) {
				throw new RuntimeException();
			}
		}
		public void close() throws IOException {
			if (din == null)
				return;
			din.close();
		}
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.io.InputStream;

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

    static class FMulticoloredMarkers {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            long a = in.readLong(), b = in.readLong();

            //ArrayList<Long> aF = new ArrayList<>(), bF = new ArrayList<>();
            TreeSet<Long> aF = new TreeSet<>(), bF = new TreeSet<>();

            for (long i = 1; i * i <= a; ++i)
                if (a % i == 0) {
                    aF.add(i);
                    aF.add(a / i);
                }
            for (long i = 1; i * i <= b; ++i)
                if (b % i == 0) {
                    bF.add(i);
                    bF.add(b / i);
                }

            //Collections.sort(aF);
            //Collections.sort(bF);

            long ans = Long.MAX_VALUE;

            long sum = a + b;

            for (long s1 = 1; s1 * s1 <= sum; s1++) {
                if (sum % s1 == 0) {
                    long s2 = sum / s1;
                    //if (s1 + s2 == (199999999999948L/2L)) {
                    //   continue;
                    //}

                    Long factor = aF.floor(s1);
                    if (factor != null && a / factor <= s2)
                        ans = Long.min(ans, s1 + s2);

                    factor = bF.floor(s1);
                    if (factor != null && b / factor <= s2)
                        ans = Long.min(ans, s1 + s2);

                /*int idx = Collections.binarySearch(aF, s1);
                if (idx < 0) idx = - idx - 1;
                if (idx >= 0 && idx < aF.size()) {
                    if (a/aF.get(idx) <= s2) ans = Long.min(ans, s1 + s2);
                }

                idx = Collections.binarySearch(bF, s1);
                if (idx < 0) idx = - idx - 1;
                if (idx >= 0 && idx < bF.size()) {
                    if (b/bF.get(idx) <= s2) ans = Long.min(ans, s1 + s2);
                }*/
                }
            }
            out.printLine(2 * ans);
        }

    }

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

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

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

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

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

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

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

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

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

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

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

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

    }
}


import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.*;
public class EightSet
{
    static final int INF = Integer.MAX_VALUE;
    static final int NINF = Integer.MIN_VALUE;

    public static void main(String[]args)throws IOException
    {
        Input in = new Input();
        HashSet<Point> hs = new HashSet<Point>();
        int [] xs = new int[8];
        int [] ys = new int[8];
        for(int i = 0; i < 8; i++)
        {
            xs[i] = in.nextInt();
            ys[i] = in.nextInt();
            hs.add(new Point(xs[i], ys[i]));
        }
        Arrays.sort(xs);
        Arrays.sort(ys);
        for(int i = 0; i < 8; i++)
        {
            for(int j = i+1; j < 8; j++)
            {
                for(int k = j+1; k < 8; k++)
                {
                    if(xs[i] < xs[j] && xs[j] < xs[k])
                    {
                        for(int a = 0; a < 8; a++)
                        {
                            for(int b = a+1; b < 8; b++)
                            {
                                for(int c = b+1; c < 8; c++)
                                {
                                    if(ys[a] < ys[b] && ys[b] < ys[c])
                                    {
                                        if(!hs.contains(new Point(xs[b], ys[b])))
                                        {
                                            if(hs.contains(new Point(xs[a], ys[b])) && hs.contains(new Point(xs[a], ys[c])) && hs.contains(new Point(xs[b], ys[a])) && hs.contains(new Point(xs[c], ys[a])) && hs.contains(new Point(xs[b], ys[c])) && hs.contains(new Point(xs[c], ys[b])))
                                            {
                                                System.out.println("respectable");
                                                exit();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println("ugly");

    }

    public static void exit()
    {
        System.exit(0);
    }
}

class Input
{
    BufferedReader br;
    StringTokenizer st;

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

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

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

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

    public float nextFloat()throws IOException
    {
        return Float.parseFloat(next());
    }

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

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


import java.util.*;
public class b {
    static int n;
    static char[][] data;
    static int[][] masks;
    static int[][] memo;
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    n = input.nextInt();
    data = new char[n][n];
    for(int i = 0; i<n; i++) data[i] = input.next().toCharArray();
    if(n==1)
    {
        if(data[0][0] == 'a') System.out.println("FIRST");
        else if(data[0][0] == 'b') System.out.println("SECOND");
        else System.out.println("DRAW");
        return;
    }
    memo = new int[2*n+2][1<<n];
    for(int[] A: memo) Arrays.fill(A, -1);

    masks = new int[2*n+2][26];
    for(int i = 0; i<2*n; i++) //diag - sum of x and y
        for(int j = 0; j<26; j++) //letter
            for(int k = 0; k<n; k++) //x coord
            {
                int r = k, c = i-k;
                if(c>=0 && c<n && data[r][c] == 'a'+j) masks[i][j] |= (1<<k);
            }
    int res = go(0, 1, data[0][0]-'a', false);
    if(res==0) System.out.println("DRAW");
    else System.out.println(res < 0 ? "FIRST" : "SECOND");
}
static int go(int diag, int mask, int ch, boolean first)
{
    if(diag == 2*(n-1))
    {
        if(first)
        {
            if(ch == 0) return 1;
            else if(ch == 1) return -1;
            return 0;
        }
        else
        {
            if(ch==0) return -1;
            else if(ch==1) return 1;
            return 0;
        }
    }
    if(memo[diag][mask] != -1) return memo[diag][mask];
    int res = -987654321;
    for(int i =0; i<26; i++)
    {
        int nmask = (mask | (mask << 1)) & (masks[diag+1][i]);
        if(nmask > 0) res = Math.max(res, -go(diag+1, nmask, i, !first));
    }
    if(first)
    {
        if(ch == 0) return memo[diag][mask] = 1+res;
        else if(ch == 1) return memo[diag][mask] = res-1;
        return memo[diag][mask] = res;
    }
    else
    {
        if(ch==0) return memo[diag][mask] = res-1;
        else if(ch==1) return memo[diag][mask] = res+1;
        return memo[diag][mask]=res;
    }
}
}


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

public class Apples {

	public static Integer[] parse(String st) {
		String[] a = st.split(" ");
		Integer[] ar = new Integer[a.length];
		for (int i = 0; i < a.length; i++)
			ar[i] = Integer.parseInt(a[i]);
		return ar;
	}

	public static void main(String[] args) {
		try {
			InputStreamReader is = new InputStreamReader(System.in);
			BufferedReader br = new BufferedReader(is);
			Integer[] p = parse(br.readLine());
			int c = p[1];
			long n = 0;
			long tot = 0;
			ArrayList<String> arg = new ArrayList<>();
			for (int i = 0; i < p[0]; i++) arg.add(br.readLine());
			for (int i = p[0] - 1; i >= 0; i--) {
				String s = arg.get(i);
				if (s.equals("half")) {
					n *= 2;
					if (n == 0) n++;
				} else if (s.equals("halfplus")) {
					n = n*2 + 1;
					if (n == 0) n++;
				}
			}
			while (n > 0) {
				tot += (n / 2.0) * c;
				n /= 2;
			}
			System.out.println(tot);
		} catch (Exception e) {e.printStackTrace();}
	}

}


import java.util.*;

public class SquaresCubes {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        int n = 0;
        Set<Integer> s = new HashSet<>();
        while (t != 0) {
            n = sc.nextInt();
            s = new HashSet<>();
            for (int i = 1; i * i <= n; i++) {
                s.add(i * i);
            }
            for (int i = 1; i * i * i <= n; i++) {
                s.add(i * i * i);
            }
            System.out.println(s.size());
            t--;
        }
    }
}


import java.util.Scanner;

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

        int n = reader.nextInt() % 4;

        reader.close();

        if (n == 1 || n == 2)
            System.out.print(1);
        else
            System.out.println(0);
    }

}


import java.util.*;
import java.io.*;
import java.math.*;
public class task
{
	public static void main(String[] Args)
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt(),m=sc.nextInt(),i;
		int cycle=(n+1)*n/2;
		m=m%cycle;
		for (i=0;i!=50 && m>=i;++i)
			m-=i;
		System.out.println(m);
	}
}


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

public class abc {
    public static void main(String args[] ) throws Exception {

    	 Scanner mak = new Scanner(System.in);
    	 int n = mak.nextInt();
    	 int arr[] = new int[n];
    	 for(int i = 0;i<n;i++){
    		 arr[i] = mak.nextInt();
    	 }
    	 int sum = 0,sum1 = 0;
    	 for(int i = arr.length - 1;i>=0;i--){
    		 sum+=arr[i];
    		 if(sum>sum1)
    		 {
    			 sum1 = sum+sum1;
    			 sum = sum1 - sum;
    			 sum1 -= sum;
    		 }

    	 }
    	 System.out.print(sum+" "+ sum1);
    }
}


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

public class D {

	static final int P = 1_000_000_007;

	static final int N = 52;

	void submit() {
		int n = nextInt();
		int m = nextInt();

		int[][] f = new int[N + 1][N + 1];

		f[0][1] = 1;

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

			int[][] dp = new int[sz + 1][N + 1];
			dp[0][1] = 1;

			int[][] cnt = new int[sz + 1][N + 1]; // [size][cut]

			for (int szL = 0; szL + 1 <= sz; szL++) {
				for (int cutL = 1; cutL <= N; cutL++) {
					for (int szR = 0; szL + szR + 1 <= sz; szR++) {
						for (int cutR = 1; cutR <= N; cutR++) {

							int szLR = szL + szR + 1;
							int cutLR = Math.min(cutL, cutR);

							cnt[szLR][cutLR] += (int) ((long) f[szL][cutL]
									* f[szR][cutR] % P);
							if (cnt[szLR][cutLR] >= P) {
								cnt[szLR][cutLR] -= P;
							}
						}
					}
				}
			}

			for (int addSz = 1; addSz <= sz; addSz++) {
				for (int addCut = 1; addCut <= N; addCut++) {

					if (cnt[addSz][addCut] == 0) {
						continue;
					}

					int[][] nextDp = new int[sz + 1][];
					for (int i = 0; i <= sz; i++) {
						nextDp[i] = dp[i].clone();
					}

					int smth = sz / addSz;

					int[] arrWays = new int[smth + 1];
					int base = cnt[addSz][addCut];

					arrWays[0] = 1;

					for (int i = 1; i <= smth; i++) {
						arrWays[i] = (int)((long)arrWays[i - 1] * inv[i] % P * base % P);
						base++;
						if (base == P) {
							base = 0;
						}
					}

					for (int cntAdd = 1; cntAdd <= smth; cntAdd++) {

						int ways = arrWays[cntAdd];

						for (int i = sz - addSz * cntAdd; i >= 0; i--) {
							for (int j = N - addCut * cntAdd; j >= 1; j--) {

								if (i + addSz * cntAdd <= sz
										&& j + addCut * cntAdd <= N) {

									nextDp[i + addSz * cntAdd][j + addCut * cntAdd] += (int) ((long) dp[i][j]
											* ways % P);
									if (nextDp[i + addSz * cntAdd][j + addCut
											* cntAdd] >= P) {
										nextDp[i + addSz * cntAdd][j + addCut
												* cntAdd] -= P;
									}

								}

							}
						}

					}

					dp = nextDp;

				}
			}

			for (int j = 1; j <= N; j++) {
				f[sz][j] = dp[sz][j];
			}

		}

//		for (int i = 0; i <= n; i++) {
//			for (int j = 1; j <= N; j++) {
//				if (f[i][j] != 0) {
//					System.err.println(i + " " + j + " -> " + f[i][j]);
//				}
//			}
//
//			System.err.println();
//		}

		out.println(f[n][m]);

	}

	int[] inv = new int[1000];
	{
		inv[0] = 0;
		inv[1] = 1;
		for (int i = 2; i < inv.length; i++) {
			inv[i] = P - (int) ((long) (P / i) * inv[P % i] % P);
		}
	}

	void preCalc() {

	}

	void stress() {

	}

	void test() {

	}

	D() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		preCalc();
		submit();
		// stress();
		// test();
		out.close();
	}

	static final Random rng = new Random();

	static int rand(int l, int r) {
		return l + rng.nextInt(r - l + 1);
	}

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

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;

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

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

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

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

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


/* reshenie na zadachata "Avtobuska stanica" */

import java.util.Scanner;

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

		int N = sc.nextInt();
		int M = sc.nextInt();
		int avtobusi = 0;
		int popolnetost = 0;
		for(int i = 0; i < N; i++){
			int grupa = sc.nextInt();
			if(popolnetost + grupa <= M){
				//ima mesto za grupata vo segasniot avtobus
				popolnetost += grupa;
			}
			else{
				//nema mesto za grupata, mora da cekaat nov avtobus
				avtobusi++;
				popolnetost = grupa;	//koga ke vlezat vo noviot avtobus, popolnetosta ke bide samo taa grupa
			}
		}
		System.out.println(avtobusi + 1);	// plus 1 bidejki segasniot ne e dodaden vo avtobusi
	}
}


import java.util.*;
import java.io.*;
public class TestClass {

	static PrintWriter out = new PrintWriter(System.out);
	static int a[][];
	static int x,y;
	static int dirx[] = {0,1,1,1,0,-1,-1,-1};
	static int diry[] = {1,1,0,-1,-1,-1,0,1};
	static int b[];
	static boolean dp[][][][];
	public static void main(String args[] ) throws Exception {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());
        String s[] = in.readLine().split(" ");
        b = new int[n];
        for(int i=0;i<n;i++)
        {
        	b[i] = Integer.parseInt(s[i]);
        }
        int m = 320;
        a = new int[m][m];
        dp = new boolean[320][320][8][30];
        int mid = m/2;
        x = mid;y=mid;
        fun(x,y,0,0);
        int ans=0;
        for(int i=0;i<m;i++)
        {
        	for(int j=0;j<m;j++)
        	{
        		if(a[i][j]==1)
        		{
        			ans++;
        		}
        	}
        }
        out.println(ans);
        out.close();
    }

	public static void fun(int x,int y,int i,int dir)
	{
		if(i==b.length)
		{
			return;
		}
		if(dp[x][y][dir][i] == true)
		{
			return;
		}
		dp[x][y][dir][i] = true;
		for(int j=0;j<b[i];j++)
		{
			x += dirx[dir];
			y += diry[dir];
			a[x][y] = 1;
		}
		fun(x,y,i+1,(dir+1)%8);
		fun(x,y,i+1,(dir+7)%8);

	}

}


import java.util.*;

public class FoxNumberGame {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] arr = new int[n];
		for(int i = 0; i < n; i++){
			arr[i] = scan.nextInt();
		}
		int gcd = arr[0];
		for(int i = 1; i < n; i++){
			gcd = gcd(gcd, arr[i]);
		}
		System.out.println(gcd*n);
	}

	static int gcd(int p, int q) {
        if (q == 0) return p;
        else return gcd(q, p % q);
    }
}


import java.util.Scanner;
public class Solution5 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Solution5 tr = new Solution5();
		tr.test(sc);
		sc.close();
	}
	void test(Scanner sc){
		int A = sc.nextInt();
		int B = sc.nextInt();
		int X = sc.nextInt();
		int more = A>=B?A:B;
		char MORE = A>=B?'0':'1';
		int less = A+B-more;
		char LESS = MORE=='0'?'1':'0';
		char[] out = new char[A+B];

		int i = 0;
		int x=0;
		int m=0;
		int l=0;
		if(X==1){
			for ( i = 0; i < more; i=i+1) {
				out[i]=MORE;
			}
			for ( ; i < more+less; i=i+1) {
				out[i]=LESS;
			}
			for ( int ii = 0; ii < A+B; ii=ii+1) {
				System.out.print(out[ii]+"");
			}
			return;
		}

		if(X==2){
			out[i++]=MORE;
			while(l<less&& i < A+B){
				out[i++] = LESS;
				l++;
			}
			while(m<more&& i < A+B){
				out[i++] = MORE;
				m++;
			}

			for ( int ii = 0; ii < A+B; ii=ii+1) {
				System.out.print(out[ii]+"");
			}
			return;
		}

		for ( i = 0; x < X-2; i=i+2) {
			out[i]=MORE;m++;
			out[i+1]=LESS;l++;
			x= (i== 0?x+1:x+2);
		}
	//	int rem= A+B-i-2;
		int last = out[i-1];
		if(x==X-1){
			// repeat last one and then other
			if(last==MORE){
				while(m<more && i < A+B){
					out[i++] = MORE;
					m++;
				}
				while(l<less&& i < A+B){
					out[i++] = LESS;
					l++;
				}
			}
			else{

				while(l<less&& i < A+B){
					out[i++] = LESS;
					l++;
				}
				while(m<more&& i < A+B){
					out[i++] = MORE;
					m++;
				}
			}

		}else{
			// repeat other one and then last
			if(last==MORE){
				while(l<less&& i < A+B){
					out[i++] = LESS;
					l++;
				}
				while(m<more&& i < A+B){
					out[i++] = MORE;
					m++;
				}
			}
			else{
				while(m<more&& i < A+B){
					out[i++] = MORE;
					m++;
				}
				while(l<less&& i < A+B){
					out[i++] = LESS;
					l++;
				}
			}
		}
/*		for (int r=0; r < rem; r++) {
			out[i]=more;
			i++;
		}*/

		for ( i = 0; i < A+B; i=i+1) {
			System.out.print(out[i]+"");
		}

	}
}


import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

public class Main {

    void run(){
        Scanner cin=new Scanner(new BufferedInputStream(System.in));
        while( cin.hasNext() ){
            int a=cin.nextInt();
            int n=cin.nextInt();
            int b=a+n;
            int[] np=new int[11000000];
            for(int i=2;i*i<=b;i++){
                if( np[i]!=0 ) continue;
                for(int j=i*i;j<b;j+=i){
                    np[j]=i;
                }
            }
            long ans=0;
            for(int i=1;i<b;i++){
                if( np[i]==0 ){
                    np[i]=i;
                }else{
                    int fac=np[i], num=0, tmp=i;
                    while( tmp%fac==0 ){
                        tmp/=fac;
                        num++;
                    }
                    np[i]=np[tmp];
                    if( num%2==1 ) np[i]*=fac;
                }
                if( i>=a ) ans+=np[i];
            }
            System.out.println(ans);
        }
    }
    public static void main(String[] args){
        Main a=new Main();
        a.run();
    }
}


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

public class RequiredLength {
	public static void solve(FastIO io) {
		final int N = io.nextInt();
		final long X = io.nextLong();

		long target = 1;
		for (int i = 1; i < N; ++i) {
			target *= 10;
		}

		HashSet<LongKey> layer = new HashSet<>();
		layer.add(LongKey.of(X));
		for (int i = 0; i < 64; ++i) {
			HashSet<LongKey> next = new HashSet<>(layer.size());
			for (LongKey k : layer) {
				long val = k.value;
				if (val >= target) {
					io.println(i);
					return;
				}
				long num = val;
				while (num > 0) {
					long d = num % 10;
					next.add(LongKey.of(val * d));
					num /= 10;
				}
			}
			layer = next;
		}
		io.println(-1);
	}

	public static class LongKey {
		public long value;

		private LongKey(long value) {
			this.value = value;
		}

		@Override
		public int hashCode() {
			return Long.hashCode(splitmix64(value + ADD_MIX));
		}

		@Override
		public boolean equals(Object obj) {
			LongKey other = (LongKey) obj;
			return value == other.value;
		}

		@Override
		public String toString() {
			return Long.toString(value);
		}

		private static final long splitmix64(long x) {
			long z = x + 0x9E3779B97F4A7C15L;
			z = (z ^ (z >>> 30)) * 0xBF58476D1CE4E5B9L;
			z = (z ^ (z >>> 27)) * 0x94D049BB133111EBL;
			return z ^ (z >>> 31);
		}

		public static LongKey of(long value) {
			if (CACHE_MIN <= value && value < CACHE_MAX) {
				return CACHE[(int) (value - CACHE_MIN)];
			}
			return new LongKey(value);
		}

		private static final long ADD_MIX = splitmix64(System.nanoTime());

		private static final int CACHE_MIN = -256;
		private static final int CACHE_MAX = 256;
		private static final LongKey[] CACHE = new LongKey[CACHE_MAX - CACHE_MIN];
		static {
			for (int i = CACHE_MIN; i < CACHE_MAX; ++i) {
				CACHE[i - CACHE_MIN] = new LongKey(i);
			}
		}
	}

	public static class FastIO {
		private InputStream reader;
		private PrintWriter writer;

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

		public FastIO(InputStream r, OutputStream w) {
			reader = r;
			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
		}

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

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

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

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

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

		// TODO: read this byte-by-byte like the other read functions.
		public double nextDouble() {
			return Double.parseDouble(nextString());
		}

		public int[] nextIntArray(int n) {
			return nextIntArray(n, 0);
		}

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

		public long[] nextLongArray(int n) {
			return nextLongArray(n, 0);
		}

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

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

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

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

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

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

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

		public void printlnArray(int[] arr) {
			printArray(arr);
			writer.println();
		}

		public void printlnArray(long[] arr) {
			printArray(arr);
			writer.println();
		}

		public void printf(String format, Object... args) {
			print(String.format(format, args));
		}

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

	public static void main(String[] args) {
		FastIO io = new FastIO(System.in, System.out);
		solve(io);
		io.flush();
	}
}


import java.util.*;
public class Main {
public static void main(String [] args){
	Scanner in=new Scanner(System.in);
	int v1=in.nextInt();
	int v2=in.nextInt();
	int t=in.nextInt();
	int d=in.nextInt();
	if(d==0){
	System.out.println(v1*t);
	return;
	}
	if(v1 > v2){
	int temp=v1;
	v1=v2;
	v2=temp;
	}
	long ans=v1;
	int curv=v1;
	int time=-1;
	for(int i=2;i<=t;i++){
		curv+=d;
		if(Math.abs(curv-v2)*1.0/d > t-i){
		time=i;
		curv=(t-i)*d + v2;
		ans+=curv;
		//System.out.println(ans+" "+curv);
		break;
		}
		ans+=curv;
		//System.out.println(ans+" "+curv);
	}
	//System.out.println(time);
	if(time!=-1){
	for(int i=time+1;i<=t;i++){
	curv-=d;
	ans+=curv;
	}
	}
	System.out.print(ans);
	}

}


import java.util.Scanner;

public class CF209B {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long A = in.nextLong();
        long B = in.nextLong();
        long C = in.nextLong();

        long ans = Math.max(A,Math.max(B,C));
        if(A%2 == B%2) ans = Math.min(ans, Math.max(A,B));
        if(A%2 == C%2) ans = Math.min(ans, Math.max(A,C));
        if(B%2 == C%2) ans = Math.min(ans, Math.max(B,C));

        System.out.println(ans);
    }
}


import java.util.Scanner;

public class Time {

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        int count=0;
        String out="NO";
        int h=sc.nextInt();
int m=sc.nextInt();
int s=sc.nextInt();
int t1=sc.nextInt();
int t2=sc.nextInt();
        if(t1>t2){
        int temp=t1;
        t1=t2;
        t2=temp;
        }
        if(t1<=h && h<t2) count++;
        if(t1<=(double)m/5 && (double)m/5<t2) count++;
        if(t1<(double)s/5 && (double)s/5<t2) count++;

        if(count==0 || count==3)
            out="YES";
        System.out.println(out);
    }
}


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

public class Main {
    private static final int c = (int) (2e5 + 10), mod = (int) (1e9 + 7);
    static int n, q;
    static long[] a = new long[c];
    static long[] w = new long[c];
    static long[][] tre = new long[c][2];

    static void update(int i, long x, int j) {
        while (i <= n) {
            tre[i][j] += x;
            if (j == 1) tre[i][j] %= mod;
            i += i & -i;
        }
    }

    static long query(int i, int j) {
        long s = 0;
        while (i > 0) {
            s += tre[i][j];
            if (j == 1) s %= mod;
            i -= i & -i;
        }
        return s;
    }

    public static void main(String[] args) {
        IO io = new IO();
        n = io.nextInt();
        q = io.nextInt();
        for (int i = 0; i < n; i++) a[i] = io.nextLong();
        for (int i = 0; i < n; i++) {
            update(i + 1, w[i] = io.nextLong(), 0);
            update(i + 1, w[i] * (a[i] - i), 1);
        }
        while (q-- > 0) {
            int x = io.nextInt(), y = io.nextInt();
            if (x < 0) {
                x = -x - 1;
                update(x + 1, y - w[x], 0);
                update(x + 1, (y - w[x]) * (a[x] - x), 1);
                w[x] = y;
            } else {
                long s = query(y, 0) + query(x - 1, 0), c = 0;
                int mid = 0;
                for (int i = 17; i >= 0; i--)
                    if (mid + (1 << i) < n && (c + tre[mid + (1 << i)][0]) * 2 < s) {
                        mid += 1 << i;
                        c += tre[mid][0];
                    }
                long a1 = query(y, 1) - 2 * query(mid, 1) + query(x-1, 1);
                long a2 = query(y, 0) - 2 * query(mid, 0) + query(x-1, 0);
                long ans = a1 % mod - a2 % mod * (a[mid] - mid) % mod + 2 * mod;
                io.println(ans % mod );

            }
        }
    }

    static class IO {

        BufferedInputStream din;
        final int BUFFER_SIZE = 1 << 16;
        byte[] buffer;
        int byteRead, bufferPoint;

        StringBuilder builder;
        PrintWriter pw;

        public IO() {
            din = new BufferedInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPoint = byteRead = 0;

            builder = new StringBuilder();
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                    System.out
            )), true);
        }

        int read() {
            if (bufferPoint >= byteRead) {
                try {
                    byteRead = din.read(buffer, bufferPoint = 0, BUFFER_SIZE);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (byteRead == -1) buffer[0] = -1;
            }
            return buffer[bufferPoint++];
        }

        int peek() {
            if (byteRead == -1) return -1;
            if (bufferPoint >= byteRead) {
                try {
                    byteRead = din.read(buffer, bufferPoint = 0, BUFFER_SIZE);
                } catch (IOException e) {
                    return -1;
                }
                if (byteRead <= 0) return -1;
            }
            return buffer[bufferPoint];
        }

        boolean hasNext() {
            int c = peek();
            while (c != -1 && c <= ' ') {
                read();
                c = peek();
            }
            return c != -1;
        }

        char nextChar() {
            int c = read();
            while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1) {
                c = read();
            }
            return (char) c;
        }

        double nextDouble() {
            double ret = 0, div = 1;
            int c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }
            if (neg)
                return -ret;
            return ret;
        }

        String nextLine() {
            byte[] strBuf = new byte[64];
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') {
                    if (cnt == 0) {
                        continue;
                    } else {
                        break;
                    }
                }
                if (strBuf.length == cnt) {
                    strBuf = Arrays.copyOf(strBuf, strBuf.length * 2);
                }
                strBuf[cnt++] = (byte) c;
            }
            return new String(strBuf, 0, cnt);
        }

        String next() {
            byte[] strBuf = new byte[64];
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (Character.isWhitespace(c)) {
                    if (cnt == 0) {
                        continue;
                    } else {
                        break;
                    }
                }
                if (strBuf.length == cnt) {
                    strBuf = Arrays.copyOf(strBuf, strBuf.length * 2);
                }
                strBuf[cnt++] = (byte) c;
            }
            return new String(strBuf, 0, cnt);
        }

        int nextInt() {
            int ans = 0;
            int c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ans = ans * 10 + c - '0';
            } while ('0' <= (c = read()) && c <= '9');
            bufferPoint--;
            return neg ? -ans : ans;
        }

        long nextLong() {
            long ans = 0;
            int c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ans = ans * 10 + c - '0';
            } while ('0' <= (c = read()) && c <= '9');
            bufferPoint--;
            return neg ? -ans : ans;
        }

        void println(Object o) {
            pw.println(o);
        }

        void print(Object o) {
            pw.print(o);
        }

        void printf(String format, Object... objects) {
            pw.printf(format, objects);
        }

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

        void done(Object o) {
            print(o);
            close();
        }

    }

}


//package codeforcesa3;
import java.util.*;
public class DimaAndFriends {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int a[]=new int[n];
        int sum=0;
        for(int i=0;i<n;i++)
        {
            a[i]=in.nextInt();
            sum+=a[i];
        }
        int curr=sum % (n+1);
        int count=0;
        for(int i=1;i<=5;i++)
        {
            if((curr+i)%(n+1)==1){
                continue;
            }else{
                count++;
            }
        }
        System.out.println(count);
    }
}


import java.util.Scanner;

/**
 * Created by shalin on 3/7/17.
 */
public class Problem817A {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x1 = in.nextInt();
        int y1 = in.nextInt();
        int x2 = in.nextInt();
        int y2 = in.nextInt();
        int x = in.nextInt();
        int y = in.nextInt();

        int xd = Math.abs(x1 - x2), yd = Math.abs(y1 - y2);
        boolean canReach = xd % x == 0 && yd % y == 0 && Math.abs(xd / x - yd / y) % 2 == 0;
        System.out.println(canReach ? "YES" : "NO");
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class CF1384E extends PrintWriter {
	// https://codeforces.com/contest/1384/problem/E
	public Scanner sc = new Scanner(System.in);

	public CF1384E() {
		super(System.out);
	}

	public static void main(String[] args) {
		CF1384E o = new CF1384E();
		o.main();
		o.flush();
	}

	public static final int SZ = 20;
	public int[] ds = new int[SZ];

	public void main() {
		int t = sc.nextInt();
		while (t-- > 0) {
			int n = sc.nextInt();
			byte[] sa = sc.next().getBytes();
			byte[] sb = sc.next().getBytes();
			for (int id = 0; id < SZ; id++) {
				ds[id] = id;
			}
			int[] es = new int[SZ];
			for (int i = 0; i < n; i++) {
				if (sa[i] != sb[i]) {
					int u = sa[i] - 'a';
					int v = sb[i] - 'a';
					join(u, v);
					es[u] |= (1 << v);
				}
			}
			int ans = SZ * 2;
			for (int id = 0; id < SZ; id++) {
				if (ds[id] == id) {
					ans = ans - 1;
				}
			}
			boolean[] dags = new boolean[1 << SZ];
			dags[0] = true;
			int cnt_ = 0;
			for (int state = 1; state < (1 << SZ); state++) {
				int cnt = 0;
				for (int tmp = state; tmp > 0; tmp &= (tmp - 1)) {
					cnt++;
				}
				for (int id = 0; id < SZ; id++) {
					if ((state & (1 << id)) > 0) {
						int a = state ^ (1 << id);
						// chu y thu tu duyet cua cac bai toan
						if (dags[a]) {
							if ((a & es[id]) == 0) {
								dags[state] = true;
								cnt_ = Math.max(cnt_, cnt);
								break;
							}
						}
					}
				}
			}
			ans -= cnt_;
			println(ans);
		}
	}

	private void join(int u, int v) {
		u = find(u);
		v = find(v);
		if (u != v) {
			ds[u] = v;
		}

	}

	private int find(int u) {
		if (ds[u] == u) {
			return u;
		}
		ds[u] = find(ds[u]);
		return ds[u];
	}
}


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

public class _194A {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int sum = 3 * a - b;
        System.out.println(sum < 0 ? 0 : sum);
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Deque;
import java.util.ArrayDeque;
import java.io.InputStream;

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

    static class TaskE {
        Deque<Integer>[] deq;
        Deque<Integer> rowDeq;

        void insert(Integer c, int value) {
            Deque<Integer> curDeq = c == -1 ? rowDeq : deq[c];
            while (curDeq.isEmpty() == false && curDeq.getLast() > value) {
                curDeq.removeLast();
            }
            curDeq.addLast(value);
        }

        void delete(Integer c, int value) {
            Deque<Integer> curDeq = c == -1 ? rowDeq : deq[c];
            if (curDeq.isEmpty() == false && curDeq.getFirst() == value) {
                curDeq.removeFirst();
            }
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int a = in.nextInt();
            int b = in.nextInt();
            long g0 = in.nextLong();
            long x = in.nextLong();
            long y = in.nextLong();
            long z = in.nextLong();

            int[][] table = new int[n][m];
            long g = g0;
            for (int r = 0; r < n; r++) {
                for (int c = 0; c < m; c++) {
                    table[r][c] = (int) g;
                    g = (g * x + y) % z;
                }
            }

            deq = new Deque[m];
            for (int c = 0; c < m; c++) {
                deq[c] = new ArrayDeque<>();
            }

            for (int c = 0; c < m; c++) {
                for (int r = 0; r < a; r++) {
                    insert(c, table[r][c]);
                }
            }
            long ans = 0;
            for (int r = 0; r < n - a + 1; r++) {
                rowDeq = new ArrayDeque<>();
                for (int c = 0; c < b; c++) {
                    insert(-1, deq[c].getFirst());
                }
                ans += rowDeq.getFirst();
                for (int c = b; c < m; c++) {
                    delete(-1, deq[c - b].getFirst());
                    insert(-1, deq[c].getFirst());
                    ans += rowDeq.getFirst();
                }
                if (r + a < n) {
                    for (int c = 0; c < m; c++) {
                        insert(c, table[r + a][c]);
                        delete(c, table[r][c]);
                    }
                }
            }
            out.println(ans);
        }

    }

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

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

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

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

        public int nextInt() {
            int c = read();
            while (isWhitespace(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 (!isWhitespace(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isWhitespace(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 (!isWhitespace(c));
            return res * sgn;
        }

    }
}


/*
 * 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.
 */

/**
 *
 * @author dipankar12
 */
import java.io.*;
import java.util.*;
public class r187b {
    public static void main(String args[])
    {
        fastio in=new fastio(System.in);
        PrintWriter pw=new PrintWriter(System.out);

        int b=in.nextInt();
        int d=in.nextInt();
        String a=in.readString();
        String c=in.readString();
        int n=a.length();
        int m=c.length();

        int matches[]=new int[m];
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            {
                int look=(i+matches[i])%m;
                if(a.charAt(j)==c.charAt(look))
                    matches[i]++;
            }

        //pw.println("matches");
        //for(int i=0;i<m;i++)
            //pw.print(matches[i]+" ");
        //pw.println();

        int res=0;
        for(int i=0;i<b;i++)
            res+=matches[res%m];

        res/=m*d;

        pw.println(res);
        pw.close();
    }

    static class fastio {

            private final InputStream stream;
            private final byte[] buf = new byte[8192];
            private int cchar, snchar;
            private SpaceCharFilter filter;

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

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

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

            public long nextLong() {
                int c = nxt();
                while (isSpaceChar(c)) {
                    c = nxt();
                }
                int sgn = 1;
                if (c == '-') {
                    sgn = -1;
                    c = nxt();
                }
                long res = 0;
                do {
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    res *= 10;
                    res += c - '0';
                    c = nxt();
                } 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 = nxt();
                while (isSpaceChar(c)) {
                    c = nxt();
                }
                StringBuilder res = new StringBuilder();
                do {
                    res.appendCodePoint(c);
                    c = nxt();
                } while (!isSpaceChar(c));
                return res.toString();
            }

            public String nextLine() {
                int c = nxt();
                while (isSpaceChar(c))
                    c = nxt();
                StringBuilder res = new StringBuilder();
                do {
                    res.appendCodePoint(c);
                    c = nxt();
                } while (!isEndOfLine(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;
            }

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

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

}


import java.io.*;
import java.math.*;
import java.lang.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        Scanner sc=new Scanner(System.in);
        int x[]=new int[5];
        int y[]=new int[5];
        for(int i=1;i<=3;i++)
        {
            x[i]=sc.nextInt();
            y[i]=sc.nextInt();
        }
        int xx=x[1]-x[2];
        int yy=y[1]-y[2];
        int xxx=x[2]-x[3];
        int yyy=y[2]-y[3];
        out.printf("3\r\n");
        out. printf("%d %d\r\n",x[3]+xx,y[3]+yy);
        out.  printf("%d %d\r\n",x[3]-xx,y[3]-yy);
        out. printf("%d %d\r\n",x[1]+xxx,y[1]+yyy);
        out.flush();
    }
}


/*
 * Code Author: Jayesh Udhani
 * Dhirubhai Ambani Institute of Information and Communication Technology (DA-IICT ,Gandhinagar)
 */
import java.io.*;
import java.util.*;

public class Order {
	static ArrayList<Pair> al=new ArrayList<Pair>();
	static class Pair<F,S> {
		private int first;
		private int second;
		public Pair(int i, int j) {
			this.first = i;
			this.second = j;
		}
		public int getFirst() { return first; }
		public int getSecond() { return second ;}
	}
	public static void main(String args[])
	{
		InputReader in = new InputReader(System.in);
		OutputStream outputStream = System.out;
		PrintWriter out = new PrintWriter(outputStream);
		//----------My Code Starts Here----------
		int a,b,i,x=0,y=0,t=0,q1=-1,q2=-1;
		a=in.nextInt();
		b=in.nextInt();
		/*		q1= a>=0?(b>=0?1:4):(b>=0?2:3);
		if(a>0&&b==0)
			q1=5;
		if(a==0 && b>0)
			q1=6;
		if(a<0 && b==0)
			q1=7;
		if(a==0 && b<0)
			q1=8;
		if(a==0 && b==0)
			q1=9;	*/
		String str=in.next();
		for(i=0;i<str.length();i++)
		{
			if(str.charAt(i)=='U')
				y++;
			else if(str.charAt(i)=='D')
				y--;
			else if(str.charAt(i)=='L')
				x--;
			else
				x++;
			al.add(new Pair<>(x,y));
		}
		/*		q2= x>=0?(y>=0?1:4):(y>=0?2:3);
		if(x>0&&y==0)
			q2=5;
		if(x==0 && y>0)
			q2=6;
		if(x<0 && y==0)
			q2=7;
		if(x==0 && y<0)
			q2=8;
		if(x==0 && y==0)
			q2=9;	*/
//		System.out.println(x+" "+y);
		if(x==0 && y==0 && a==0 && b==0)
			t=1;
		if(x==0 && y!=0 && a==0 && (b+y)%y==0 && (long)b*y>=0)
			t=1;
		if(x!=0 && y==0 && (a+x)%x==0 && b==0 && (long)a*x>=0)
			t=1;
		if(x!=0 && y!=0 && (a+x)%x==0 && (b+y)%y==0 && (a+x)/x==(b+y)/y && (long)a*x>=0 && (long)b*y>=0)
			t=1;
		for(i=0;i<str.length()-1;i++)
		{
			if(str.charAt(i)=='U')
				y++;
			else if(str.charAt(i)=='D')
				y--;
			else if(str.charAt(i)=='L')
				x--;
			else
				x++;
			int p=al.get(i).first,q=al.get(i).second;
			int d1=x-p,d2=y-q;
			if(d1==0 && d2==0)
			{
				if(a==p && b==q)
					t=1;
			}
			else if(d1!=0 && d2==0)
			{
				if((a-(p-d1))%d1==0 && b==q && (long)(x-p)*(a-p)>=0)
					t=1;
			}
			else if(d1==0 && d2!=0)
			{
				if(a==p && (b-(q-d2))%d2==0 &&(long) (y-q)*(b-q)>=0)
					t=1;
			}
			else
			{
				if((a-(p-d1))%d1==0 && (b-(q-d2))%d2==0 && (a-(p-d1))/d1==(b-(q-d2))/d2 && (long)(x-p)*(a-p)>=0 &&(long) (y-q)*(b-q)>=0)
					t=1;
			}
		}
//		System.out.println(al.get(2).first+" "+al.get(2).second);
		//		if(q1!=q2)
		//			t=0;
//		System.out.println(q1+" "+q2);
		if(t==0)
			System.out.println("No");
		else
			System.out.println("Yes");
		out.close();
		//---------------The End———————————————————
	}
	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

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

		public String nextLine(){
			String fullLine=null;
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					fullLine=reader.readLine();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
				return fullLine;
			}
			return fullLine;
		}
		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 long nextLong() {
			return Long.parseLong(next());
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
	}
}


import java.util.Scanner;

/**
 * Created by igarus on 13.06.2016.
 */
public class CF_B {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextInt();
        long a = scanner.nextInt();
        long b = scanner.nextInt();
        long p = scanner.nextInt();
        long q = scanner.nextInt();

        long maxP = p;
        long minP = q;
        long maxA = a;
        long minA = b;
        if (q > p) {
            maxP = q;
            minP = p;
            maxA = b;
            minA = a;
        }
        long s = 0;
        long k = n / maxA;
        s += k * maxP;
        long z = n / minA - n / lcm(maxA, minA);
        s += z * minP;

        System.out.println(s);
    }

    public static long gcd(long a, long b) {
        while (b != 0) {
            long tmp = a % b;
            a = b;
            b = tmp;
        }
        return a;
    }

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