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

public class C {
	BufferedReader in;
	StringTokenizer str;
	PrintWriter out;
	String SK;

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

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

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

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

	int[] len;
	int[] pos;

	int solve(int l1, int r1, int l2, int r2, int type) {
		if (l1 > r1 || l2 > r2)
			return 0;
		if (l1 > l2) {
			return solve(l2, r2, l1, r1, type);
		}
		if (r2 <= r1) {
			return r2 - l2 + 1;
		}
		if (l1 == l2) {
			return Math.min(r2, r1) - l1 + 1;
		}
		if (r1 >= pos[type] && l2 <= pos[type]) {
			return Math.max(r1 - l2 + 1,
					solve(l1, pos[type] - 1, 0, r2 - pos[type] - 1, type - 1));
		}
		if (r1 >= pos[type]) {
			return solve(len[type] - r2 - 1, len[type] - l2 - 1, len[type] - r1
					- 1, len[type] - l1 - 1, type);
		}
		if (r2 < pos[type]) {
			return solve(l1, r1, l2, r2, type - 1);
		}
		return Math.max(
				solve(l1, r1, l2, pos[type] - 1, type - 1),
				solve(l1, r1, Math.max(0, l2 - pos[type] - 1), r2 - pos[type]
						- 1, type - 1));
	}

	void solve() throws IOException {
		int m = 30;
		len = new int[m];
		pos = new int[m];
		len[0] = 1;
		pos[0] = 0;
		for (int i = 1; i < m; i++) {
			pos[i] = len[i - 1];
			len[i] = 2 * len[i - 1] + 1;
		}
		out.println(solve(nextInt() - 1, nextInt() - 1, nextInt() - 1,
				nextInt() - 1, m - 1));
	}

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

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

}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class B {
	public static void main(String[] args) {
		InputStream input;
		OutputStream output;
		try {
			input = new FileInputStream("input.txt");
			output = new FileOutputStream("output.txt");
		} catch (FileNotFoundException e) {
			input = System.in;
			output = System.out;
		}
		Kattio io = new Kattio(input, output);
		//int t=io.getInt();
		//for (int i=1; i<=t; i++)
		(new Solve(io)).main();
		io.close();

		if (input instanceof FileInputStream)
			try {
				input.close();
			} catch (IOException e) {
			}
		if (output instanceof FileOutputStream)
			try {
				output.close();
			} catch (IOException e) {
			}
	}
}

class Solve {
	Kattio io;

	long n;
	int m;
	long[] a;
	long[] b;
	Solve(Kattio io) {
		this.io = io;
	}

	void main() {
		n=io.getLong();
		m=io.getInt();
		a = new long[m];
		b = new long[m];
		for (int i=0; i<m; i++)
		{
			if (i<=n%m)
				a[i]=n/m+1;
			else
				a[i]=n/m;
		}
		a[0]--;

//		for (int i=0; i<m; i++)
//			io.println(a[i]);

		Arrays.fill(b,0);
		for (int i=0; i<m; i++)
			b[(i*i)%m]+=a[i];

		long res = 0;
		for (int i=1; i<m; i++)
			res+=b[i]*b[m-i];

		res+=(b[0]*b[0]);

		io.println(res);
	}
}

class Kattio extends PrintWriter {
	public Kattio(InputStream i) {
		super(new BufferedOutputStream(System.out));
		r = new BufferedReader(new InputStreamReader(i));
	}

	public Kattio(InputStream i, OutputStream o) {
		super(new BufferedOutputStream(o));
		r = new BufferedReader(new InputStreamReader(i));
	}

	public boolean hasMoreTokens() {
		return peekToken() != null;
	}

	public int getInt() {
		return Integer.parseInt(nextToken());
	}

	public double getDouble() {
		return Double.parseDouble(nextToken());
	}

	public long getLong() {
		return Long.parseLong(nextToken());
	}

	public String getWord() {
		return nextToken();
	}

	private BufferedReader r;
	private String line;
	private StringTokenizer st;
	private String token;

	private String peekToken() {
		if (token == null)
			try {
				while (st == null || !st.hasMoreTokens()) {
					line = r.readLine();
					if (line == null)
						return null;
					st = new StringTokenizer(line);
				}
				token = st.nextToken();
			} catch (IOException e) {
			}
		return token;
	}

	private String nextToken() {
		String ans = peekToken();
		token = null;
		return ans;
	}
}


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

public class Main {
 	public static void main(String[] args){
		FastScanner scanner = new FastScanner();
		int n = scanner.nextInt();
		int x1 = scanner.nextInt();
		int y1 = scanner.nextInt();
		int x2 = scanner.nextInt();
		int y2 = scanner.nextInt();
		int min = 10000;
		if((x1 == 0 && x2 == n) || (x1 == n && x2 == 0)){
			min = Math.min(min, y1+y2+n);
			min = Math.min(min, 3*n-y1-y2);
		}else if((y1 == 0 && y2 == n) || (y1 == n && y2 == 0)){
			min = Math.min(min, n+x1+x2);
			min = Math.min(min, 3*n-x1-x2);
		}else if(y1 == 0 && x2 == n){
			min = Math.min(min, n-x1+y2);
		}else if(y2 == 0 && x1 == n){
			min = Math.min(min,n-x2+y1);
		}else if(x1 == 0 && y2 == 0){
			min = Math.min(min,x2+y1);
		}else if(x2 == 0 && y1 == 0){
			min = Math.min(min,y2+x1);
		}else if(x1 == 0 && y2 == n){
			min = Math.min(min,n-y1+x2);
		}else if(x2 == 0 && y1 == n){
			min = Math.min(min,n-y2+x1);
		}else if(x1 == n && y2 == n){
			min = Math.min(min, n-y1+n-x2);
		}else if(x2 == n && y1 == n){
			min = Math.min(min, n-x1+n-y2);
		}else if((x1 == 0 && x2 == 0) || (x1 == n && x2 == n)){
			min = Math.min(min,Math.abs(y1-y2));
		}else if((y1 == 0 && y2 == 0) || (y1 == n && y2 == n)){
			min = Math.min(min,Math.abs(x1-x2));
		}
		System.out.println(min);
	}

	private static class FastScanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;
		private boolean hasNextByte() {
				if (ptr < buflen) {
						return true;
				}else{
						ptr = 0;
						try {
								buflen = in.read(buffer);
						} catch (IOException e) {
								e.printStackTrace();
						}
						if (buflen <= 0) {
								return false;
						}
				}
				return true;
		}
		private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
		private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
		public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
		public String next() {
				if (!hasNext()) throw new NoSuchElementException();
				StringBuilder sb = new StringBuilder();
				int b = readByte();
				while(isPrintableChar(b)) {
						sb.appendCodePoint(b);
						b = readByte();
				}
				return sb.toString();
		}
		public long nextLong() {
				if (!hasNext()) throw new NoSuchElementException();
				long n = 0;
				boolean minus = false;
				int b = readByte();
				if (b == '-') {
						minus = true;
						b = readByte();
				}
				if (b < '0' || '9' < b) {
						throw new NumberFormatException();
				}
				while(true){
						if ('0' <= b && b <= '9') {
								n *= 10;
								n += b - '0';
						}else if(b == -1 || !isPrintableChar(b)){
								return minus ? -n : n;
						}else{
								throw new NumberFormatException();
						}
						b = readByte();
				}
		}
		public int nextInt() {
				long nl = nextLong();
				if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
				return (int) nl;
		}
		public double nextDouble() { return Double.parseDouble(next());}
	}
}


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

import static java.lang.Math.*;

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

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

		public int read()
		{
			if (numChars==-1)
				throw new InputMismatchException();

			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = stream.read(buf);
				}
				catch (IOException e)
				{
					throw new InputMismatchException();
				}

				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine()
		{
			String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
		}
		public int nextInt()
		{
			int c = read();

			while(isSpaceChar(c))
				c = read();

			int sgn = 1;

			if (c == '-')
			{
				sgn = -1;
				c = read();
			}

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

			return res * sgn;
		}

		public long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			long res = 0;

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

		public double nextDouble()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.')
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

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

			return res.toString();
		}

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

		public String next()
		{
			return readString();
		}

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

	public static void main(String args[]) throws Exception
	{
		new Thread(null, new Main(),"Main",1<<26).start();
	}
	public void run()
	{
		InputReader sc= new InputReader(System.in);
		PrintWriter w= new PrintWriter(System.out);

        long n = sc.nextLong();
        long m = sc.nextLong();

        if(n <= m)
            w.print(n);

        else
        {
            long ans = (long)Math.ceil((Math.sqrt(0.25 + 2.0 * (n - m))) - 0.5);
            if(ans * (ans + 1) - 2 * (n - m) >= 0)
                w.print((ans + m));
            else
                w.print((ans + m + 1));
        }

        w.close();
	}
}


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

public class First {
    private static final Scanner sc = new Scanner(System.in);
    private static final PrintWriter pw = new PrintWriter(System.out);
    private static StringBuffer ans = new StringBuffer();

    public static void main(String[] args) throws Exception {
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            long a = sc.nextLong(), b = sc.nextLong();
            if (Math.abs(a - b) == 1 && isPrime(a + b)) ans.append("YES\n");
            else ans.append("NO\n");
        }
        pw.print(ans);
        sc.close();
        pw.close();
    }

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

class Scanner {
    private final BufferedReader br;
    private StringTokenizer st;

    public Scanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(in));
        st = new StringTokenizer("");
    }

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

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

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

    public void close() throws IOException {
        br.close();
    }
}

class PrintWriter {
    private final BufferedOutputStream pw;

    public PrintWriter(OutputStream out) {
        pw = new BufferedOutputStream(out);
    }

    public void print(Object o) throws IOException {
        pw.write(o.toString().trim().getBytes());
        pw.flush();
    }

    public void println(Object o) throws IOException {
        print(o + "\n");
    }

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


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

public class Main{

    BufferedReader in;
    StringTokenizer str = null;
    PrintWriter out;

    private String next() throws Exception{
    	while (str == null || !str.hasMoreElements())
    	    str = new StringTokenizer(in.readLine());
    	return str.nextToken();
    }

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

    char [][]sc, map;
    int n, t;

    int [][]dr;
    Queue<Integer> q;

    int di[] = new int[]{-1, 1, 0, 0};
    int dj[] = new int[]{0, 0, -1, 1};

    MaxFlowMinCost mfmc;

    final static int BASE = 10;

    public void run() throws Exception{
    	in = new BufferedReader(new InputStreamReader(System.in));
    	out = new PrintWriter(System.out);

    	n = nextInt();
    	t = nextInt();
    	sc = new char[n][];
    	for (int i = 0; i < n ; ++i) {
    		sc[i] = next().toCharArray();
    	}

    	map = new char[n][];
    	for (int i = 0; i < n; ++i) {
    		map[i] = next().toCharArray();
    	}

    	dr = new int[n][n];
    	for(int []i : dr) Arrays.fill(i, oo);
    	q = new ArrayDeque<Integer>();

    	for(int i = 0; i < n; ++i) {
    		for(int j = 0; j < n; ++j) {
    			if (map[i][j] == 'Z') {
    				dr[i][j] = 0;
    				q.add(i * BASE + j);
    			}
    		}
    	}

    	while(q.size() > 0) {
    		int cur = q.poll();
    		int x = cur / BASE, y = cur % BASE;

    		for(int i = 0; i < 4; ++i) {
    			int nx = x + di[i], ny = y + dj[i];
    			add(nx, ny, x, y, dr);
    		}
    	}

    	int source = 2 * n * n, sink = 2 * n * n + 1;
    	mfmc = new MaxFlowMinCost(2 * n * n + 2, source, sink);

    	for(int i = 0; i < n; ++i) {
    		for(int j = 0; j < n; ++j) {
    			if (Character.isDigit(sc[i][j])) {
    				int flow = sc[i][j] - '0', v = i * n + j;
    				if (flow > 0) {
    					mfmc.addEdge(source, v, flow);
    				}
    			}

    			if (Character.isDigit(map[i][j])) {
    				int flow = map[i][j] - '0', v = n * n + i * n + j;
    				if (flow > 0) {
    					mfmc.addEdge(v, sink, flow);
    				}
    			}

    			if (Character.isDigit(sc[i][j])) {
    				int count = sc[i][j] - '0';
    				if (count == 0) continue;
    				int [][]d = bfs(i, j, dr);
    				// System.out.println(i + " " + j);
    				// print(d);
    				for(int x = 0; x < n; ++x) {
    					for(int y = 0; y < n; ++y) {
    						if (Character.isDigit(map[x][y])) {
    							count = map[x][y] - '0';
    							if (count == 0) continue;
    							if (d[x][y] <= t) {
    								int v = i * n + j, u = n * n + x * n + y;
    								mfmc.addEdge(v, u, oo);
    							}
    						}
    					}
    				}
    			}
    		}
    	}

    	// mfmc.print();

    	mfmc.execute();

    	out.println(mfmc.flow);
        out.close();
    }

    private void print(int [][]d) {
    	for(int i = 0; i < n; ++i) {
    		System.out.println(Arrays.toString(d[i]));
    	}
    	System.out.println();
    }

    private int[][] bfs(int sx, int sy, int [][]dr) {
    	int [][]d = new int[n][n];
    	for(int []i : d) Arrays.fill(i, oo);

    	d[sx][sy] = 0;
    	q.add(sx * BASE + sy);

    	while(q.size() > 0) {
    		int cur = q.poll();
    		int x = cur / BASE, y = cur % BASE;

    		for(int i = 0; i < 4; ++i) {
    			int nx = x + di[i], ny = y + dj[i];
    			add(nx, ny, x, y, d, dr);
    		}
    	}

    	return d;
    }

    private void add(int x, int y, int fx, int fy, int [][]d, int [][]dr) {
    	if (x < 0 || y < 0) return;
    	if (x >= n || y >= n) return;
    	if (map[x][y] == 'Y' || map[x][y] == 'Z') return;
    	if (d[x][y] > d[fx][fy] + 1) {
    		if (dr[x][y] >= d[fx][fy] + 1) {
    			d[x][y] = d[fx][fy] + 1;
    			if (dr[x][y] > d[fx][fy] + 1) {
    				q.add(x * BASE + y);
    			}
    		}
    	}
    }

    private void add(int x, int y, int fx, int fy, int [][]d) {
    	if (x < 0 || y < 0) return;
    	if (x >= n || y >= n) return;
    	if (map[x][y] == 'Y') return;
    	if (d[x][y] > d[fx][fy] + 1) {
    		d[x][y] = d[fx][fy] + 1;
    		q.add(x * BASE + y);
    	}
    }

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

    final static int oo = Integer.MAX_VALUE/2;

    class MaxFlowMinCost {

    	class Edge {
    		int from, to, cap, flow;

    		public Edge(int from, int to, int cap, int flow) {
    			this.from = from;
    			this.to = to;
    			this.cap = cap;
    			this.flow = flow;
    		}

    		public String toString() {
    			return "from=" + from + ", to=" + to + ", cap=" + cap + ", flow=" + flow;
    		}
    	}

    	int n, source, sink;
    	int flow = 0;

    	List<Integer> []g;
    	List<Edge> e;

    	int []q, from, d;
    	boolean []inqueue;

    	public MaxFlowMinCost(int n, int source, int sink) {
    		this.n = n;
    		this.source = source;
    		this.sink = sink;

    		g = new LinkedList[n];
    		for(int i = 0; i < n; ++i) g[i] = new LinkedList<Integer>();
    		e = new ArrayList<Edge>();

    		q = new int[n];
    		d = new int[n];
    		from = new int[n];
    		inqueue = new boolean[n];
    	}

    	private boolean bfs() {
    		Arrays.fill(d, oo);
    		Arrays.fill(from, -1);
    		Arrays.fill(inqueue, false);
    		int head = 0, tail = 0;

    		d[source] = 0;
    		q[tail++] = source;
    		inqueue[source] = true;

    		while(head != tail && d[sink] == oo) {
    			int x = q[head++];
    			for(int id : g[x]) {
    				Edge edge = e.get(id);
    				if (edge.cap <= edge.flow) continue;
    				if (d[edge.to] > d[x] + 1) {
    					d[edge.to] = d[x] + 1;
    					from[edge.to] = id;
    					if (inqueue[edge.to]) continue;
    					inqueue[edge.to] = true;
    					q[tail++] = edge.to;
    					if (tail == n) tail = 0;
    				}
    			}
    			if (head == n) head = 0;
    			inqueue[x] = false;
    		}

    		return d[sink] != oo;
    	}

    	private void increase() {
    		// System.out.println("INCREASING FLOW...");
    		int c = sink, delta = oo;
    		while(from[c] != -1) {
    			int id = from[c];
    			Edge edge = e.get(id);
    			// System.out.println(edge);
    			delta = Math.min(delta, edge.cap - edge.flow);
    			c = edge.from;
    		}
    		// System.out.println("delta = " + delta);
    		flow+=delta;
    		c = sink;
    		while(from[c] != -1) {
    			int id = from[c];
    			e.get(id).flow+=delta;
    			e.get(id ^ 1).flow-=delta;

    			c = e.get(id).from;
    		}
    	}

    	public void execute() {
    		while(bfs()) {
    			increase();
    		}
    	}

    	public void addEdge(int from, int to, int cap) {
    		g[from].add(e.size());
    		e.add(new Edge(from, to, cap, 0));
    		g[to].add(e.size());
    		e.add(new Edge(to, from, cap, cap));
    	}

    	public void print() {
    		for(Edge edge : e) {
    			System.out.println(edge);
    		}
    	}

    }
}


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

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scan=new Scanner(System.in);
		int l=scan.nextInt();
		int r=scan.nextInt();
		int x=scan.nextInt();
		int y=scan.nextInt();
		long k=scan.nextInt();
        for(int i=x;i<=y;i++)
            if(k*i>=l&&k*i<=r){System.out.println("YES");return;}

        System.out.println("NO");

	}
}


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

/**
 * Created by IntelliJ IDEA.
 * User: jte
 * Date: 13.4.11
 * Time: 17.39
 * To change this template use File | Settings | File Templates.
 */

public class Main extends Thread {

    public Main() {
        this.setPriority(Thread.MAX_PRIORITY);
    }

    private int signum(int a) {
        return a < 0 ? -1 : a == 0 ? 0 : 1;
    }

    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String[] first = in.readLine().split("[ ]+");
            int n = Integer.parseInt(first[0]), m = Integer.parseInt(first[1]), k = Integer.parseInt(first[2]);
            String cur = in.readLine();
            int dx = 0;
            if (cur.compareTo("to head") == 0) {
                dx = -1;
            } else {
                dx = 1;
            }
            char[] states = in.readLine().trim().toCharArray();
            for (int i = 0; i < states.length; ++i) {
                if (states[i] == '0') {
                    if (signum(m - k) == 0) {
                        System.out.println("Controller " + i);
                        System.exit(0);
                    }
                    if (signum(m - k) != dx) {
                        k = k + dx;
                        if (k == 1 || k == n) {
                            dx = -dx;
                        }
                    } else {
                        if (Math.abs(m - k) > 1) {
                            k = k + dx;
                            if (k == 1 || k == n) {
                                dx = -dx;
                            }
                        } else {
                            m = m + dx;
                            if (m < 1)
                                m = 1;
                            if (m > n)
                                m = n;
                            k = k + dx;
                            if (k == 1 || k == n) {
                                dx = -dx;
                            }
                        }
                    }
                }
                else{
                    if (signum(m - k) == 0) {
                        System.out.println("Controller " + i);
                        System.exit(0);
                    }
                    k = k + dx;
                    if (dx == 1){
                        m = 1;
                    }
                    else{
                        m = n;
                    }
                    if (k == 1 || k == n){
                        dx = -dx;
                    }
                }
            }
            System.out.println("Stowaway");
        } catch (Throwable e) {
            System.err.println(e.getMessage());
            System.err.println(e.getStackTrace());
        }

    }

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


import java.util.Scanner;

public class Nirvana {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n=in.nextInt();
		Nirvana obj = new Nirvana();
		System.out.println(obj.maxMultiply(n));

		in.close();
	}
	int maxMultiply(int n) {
		if(n == 0)
			return 1;
		int nextNum = n/10;
		int digit = n%10;
		if(digit == 9) {
			// No need to borrow
			return 9*maxMultiply(nextNum);
		}
		int result9 = -1;
		int resultNormal = -1;
		if(nextNum>0) {
			// Can borrow
			result9 = 9*maxMultiply(nextNum-1);
		}
		resultNormal = digit*maxMultiply(nextNum);
		return Math.max(result9, resultNormal);
	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
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;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        FNezzarAndChocolateBars solver = new FNezzarAndChocolateBars();
        solver.solve(1, in, out);
        out.close();
    }

    static class FNezzarAndChocolateBars {
        static final int MAXN = 2048;
        static final int modulo = 998244353;

        public int add(int x, int y) {
            x += y;
            if (x >= modulo) x -= modulo;
            return x;
        }

        public int subtract(int x, int y) {
            x -= y;
            if (x < 0) x += modulo;
            return x;
        }

        public int multiply(int x, int y) {
            return (int) ((x * 1L * y) % modulo);
        }

        public int power(int x, long y) {
            int ans = 1;
            for (; y > 0; y >>= 1, x = (int) ((x * 1L * x) % modulo))
                if (y % 2 == 1) ans = (int) ((ans * 1L * x) % modulo);
            return ans;
        }

        public int inverse(int x) {
            return power(x, modulo - 2);
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int[] fact = new int[MAXN];
            int[] ifact = new int[MAXN];
            fact[0] = ifact[0] = 1;
            for (int i = 1; i < MAXN; i++) {
                fact[i] = multiply(fact[i - 1], i);
                ifact[i] = inverse(fact[i]);
            }
            int n = in.nextInt(), k = in.nextInt();
            int[] l = in.readIntArray(n);
            int[][][] dp = new int[2][MAXN][n + 1];
            dp[1][0][0] = 1;
            int s = 0;

            for (int p = 0; p < n; p++) {
                int L = l[p];
                int invL = inverse(L);
                int[] coeffs = new int[L / k + 1];
                int[] coeffs2 = new int[L / k + 1];
                for (int i = 0; i <= L / k; i++) {
                    if (k * i == L) continue;
                    int z = ifact[i];
                    if (i % 2 == 1) z = subtract(0, z);
                    coeffs[i] = multiply(z, power(L - k * i, i));

                    if (i >= 1) {
                        coeffs2[i] = multiply(z, multiply(i, power(L - k * i, i - 1)));
                    }
                }
                int r = p & 1;
                int h = r ^ 1;
                for (int i = 0; i <= s / k; i++) Arrays.fill(dp[r][i], 0);
                for (int i = 0; i <= s / k; i++)
                    for (int j = 0; j <= p; j++)
                        if (dp[h][i][j] != 0) {
                            int value = dp[h][i][j];
                            for (int t = 0; t <= L / k; t++) {
                                dp[r][i + t][j] = add(dp[r][i + t][j], multiply(value, coeffs[t]));
                                if (t > 0) {
                                    dp[r][i + t - 1][j + 1] = add(dp[r][i + t - 1][j + 1], multiply(value, coeffs2[t]));
                                }
                            }
                        }
                s += L;
            }
            int answer = 0;
            for (int i = 0; i <= s / k; i++)
                for (int r = 0; r <= n; r++) {
                    int value = dp[(n - 1) & 1][i][r];
                    int j = i + r;
                    if (j == 0) continue;
                    int temp = multiply(fact[i], inverse(power(k * j, i + 1)));
                    answer = add(answer, multiply(value, temp));
                }
            out.println(multiply(subtract(0, s), answer));
        }

    }

    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[] readIntArray(int size) {
            int[] array = new int[size];
            for (int i = 0; i < size; i++) {
                array[i] = readInt();
            }
            return array;
        }

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

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

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

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int 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 println(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
            writer.print('\n');
        }

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

    }
}


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

public class Main implements Runnable {

	public void _main() throws IOException {
		final int infinity = 1000000000;
		// A ray that is not collinear to any of the (dx, dy)`s
		final int RAY_R = 31;
		final int RAY_C = 37;
		int height = nextInt();
		int width = nextInt();
		char[][] s = new char[height][];
		int sr = -1;
		int sc = -1;
		int numTreasures = 0;
		int numBombs = 0;
		int[][] treasureId = new int[height][width];
		int[][] bombId = new int[height][width];
		for (int r = 0; r < height; r++) {
			s[r] = next().toCharArray();
			for (int c = 0; c < width; c++) {
				if (s[r][c] == 'S') {
					sr = r;
					sc = c;
					s[r][c] = '.';
				}
				treasureId[r][c] = -1;
				if (Character.isDigit(s[r][c])) {
					treasureId[r][c] = s[r][c] - '1';
					++numTreasures;
				}
				bombId[r][c] = -1;
				if (s[r][c] == 'B') {
					bombId[r][c] = numBombs;
					++numBombs;
				}
			}
		}
		int[] treasureR = new int[numTreasures];
		int[] treasureC = new int[numTreasures];
		int[] bombR = new int[numBombs];
		int[] bombC = new int[numBombs];
		for (int r = 0; r < height; r++) {
			for (int c = 0; c < width; c++) {
				if (treasureId[r][c] >= 0) {
					int id = treasureId[r][c];
					treasureR[id] = r;
					treasureC[id] = c;
				}
				if (bombId[r][c] >= 0) {
					int id = bombId[r][c];
					bombR[id] = r;
					bombC[id] = c;
				}
			}
		}
		int[] cost = new int[numTreasures];
		for (int i = 0; i < numTreasures; i++) {
			cost[i] = nextInt();
		}
		int[] value = new int[1 << (numTreasures + numBombs)];
		for (int i = 1; i < value.length; i++) {
			int j = Integer.numberOfTrailingZeros(i);
			if (j < numTreasures) {
				value[i] = cost[j] + value[i ^ (1 << j)];
			} else {
				value[i] = -infinity;
			}
		}
		int qsize = height * width * (1 << (numTreasures + numBombs));
		int[] qr = new int[qsize];
		int[] qc = new int[qsize];
		int[] qmask = new int[qsize];
		int[][][] dist = new int[height][width][1 << (numTreasures + numBombs)];
		for (int r = 0; r < height; r++) {
			for (int c = 0; c < width; c++) {
				Arrays.fill(dist[r][c], infinity);
			}
		}
		dist[sr][sc][0] = 0;
		qr[0] = sr;
		qc[0] = sc;
		qmask[0] = 0;
		int qt = 0;
		int qh = 1;
		int ans = 0;
		while (qt < qh) {
			int r = qr[qt];
			int c = qc[qt];
			int mask = qmask[qt];
			++qt;
			if (r == sr && c == sc) {
				ans = Math.max(ans, value[mask] - dist[r][c][mask]);
			}
			for (int dr = -1; dr <= 1; dr++) {
				for (int dc = -1; dc <= 1; dc++) {
					if (dr == 0 ^ dc == 0) {
						int nr = r + dr;
						int nc = c + dc;
						if (nr < 0 || nr >= height || nc < 0 || nc >= width) continue;
						if (s[nr][nc] != '.') continue;
						int nmask = mask;
						for (int i = 0; i < numTreasures + numBombs; i++) {
							int or, oc;
							if (i < numTreasures) {
								or = treasureR[i];
								oc = treasureC[i];
							} else {
								or = bombR[i - numTreasures];
								oc = bombC[i - numTreasures];
							}
							int r1 = r - or;
							int c1 = c - oc;
							int r2 = nr - or;
							int c2 = nc - oc;
							if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0) continue;
							if (Integer.signum(cross(r1, c1, RAY_R, RAY_C)) == Integer.signum(cross(RAY_R, RAY_C, r2, c2))) {
								nmask ^= 1 << i;
							}
						}
						if (dist[nr][nc][nmask] > 1 + dist[r][c][mask]) {
							dist[nr][nc][nmask] = 1 + dist[r][c][mask];
							qr[qh] = nr;
							qc[qh] = nc;
							qmask[qh] = nmask;
							++qh;
						}
					}
				}
			}
		}
		out.println(ans);
	}

	private int cross(int x1, int y1, int x2, int y2) {
		return x1*y2 - x2*y1;
	}

	private BufferedReader in;
	private PrintWriter out;
	private StringTokenizer st;

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

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

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

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

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

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			//in = new BufferedReader(new FileReader("a.in"));
			//out = new PrintWriter(new FileWriter("a.out"));

			_main();

			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(202);
		}
	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
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;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        int n;
        char[] colorCard;
        int[] costRedToken;
        int[] costBlueToken;
        final int inf = (int) 1e9;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.readInt();
            colorCard = new char[n];
            costRedToken = new int[n];
            costBlueToken = new int[n];

            int overRed = 0;
            int overBlue = 0;

            for (int i = 0; i < n; i++) {
                colorCard[i] = in.readCharacter();

                costRedToken[i] = in.readInt();
                costBlueToken[i] = in.readInt();

                overRed += Math.max(costRedToken[i] - n, 0);
                overBlue += Math.max(costBlueToken[i] - n, 0);

                costRedToken[i] = Math.min(n, costRedToken[i]);
                costBlueToken[i] = Math.min(n, costBlueToken[i]);
            }

            int over = Math.max(overRed, overBlue);

            int[][][] dp = new int[2][n * n + 1][1 << n];
            ArrayUtils.fill(dp, inf);

            if (overRed < overBlue) {
                dp[0][Math.min(over - overRed, n * n)][0] = 0;
            } else {
                dp[1][Math.min(over - overBlue, n * n)][0] = 0;
            }

            for (int msk = 0; msk < (1 << n); msk++) {
                for (int typeRemToken = 0; typeRemToken < 2; typeRemToken++) {
                    for (int countRemTokens = 0; countRemTokens <= n * n; countRemTokens++) {

                        if (dp[typeRemToken][countRemTokens][msk] == inf) {
                            continue;
                        }

                        int countRedCard = 0;
                        int countBlueCard = 0;
                        for (int card = 0; card < n; card++) {
                            if (((msk >> card) & 1) == 1) {
                                if (colorCard[card] == 'R') {
                                    ++countRedCard;
                                } else if (colorCard[card] == 'B') {
                                    ++countBlueCard;
                                } else {
                                    throw new RuntimeException();
                                }
                            }
                        }

                        for (int card = 0; card < n; card++) {
                            if (((msk >> card) & 1) == 1) {
                                continue;
                            }

                            int countHaveRedToken = typeRemToken == 0 ? countRemTokens : 0;
                            int countHaveBlueToken = typeRemToken == 1 ? countRemTokens : 0;

                            int costRed = Math.max(costRedToken[card] - countRedCard, 0);
                            int costBlue = Math.max(costBlueToken[card] - countBlueCard, 0);

                            int countBuyToken = Math.max(Math.max(costRed - countHaveRedToken, 0), Math.max(costBlue -
                                    countHaveBlueToken, 0));

                            int remRedToken = countBuyToken + countHaveRedToken - costRed;
                            int remBlueToken = countBuyToken + countHaveBlueToken - costBlue;

                            int toMsk = msk | (1 << card);
                            int toTypeRemToken = remRedToken == 0 ? 1 : 0;
                            int toCountRemTokens = Math.min(Math.max(remRedToken, remBlueToken), n * n);

                            dp[toTypeRemToken][toCountRemTokens][toMsk] = Math.min
                                    (dp[toTypeRemToken][toCountRemTokens][toMsk],
                                            dp[typeRemToken][countRemTokens][msk] + countBuyToken);

                        }
                    }
                }
            }

            int curAns = inf;
            for (int typeRemToken = 0; typeRemToken < 2; typeRemToken++) {
                for (int countRemTokens = 0; countRemTokens <= n * n; countRemTokens++) {
                    curAns = Math.min(curAns, dp[typeRemToken][countRemTokens][(1 << n) - 1]);
                }
            }
            out.printLine(n + over + curAns);
//        out.printLine(dp[0][1][0b1]);
//        out.printLine(dp[0][1][0b11]);
//        out.printLine(dp[0][1][0b111]);
        }

    }

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

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

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

        public char readCharacter() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            return (char) c;
        }

        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 close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static class ArrayUtils {
        public static void fill(int[][] array, int value) {
            for (int[] row : array) {
                Arrays.fill(row, value);
            }
        }

        public static void fill(int[][][] array, int value) {
            for (int[][] subArray : array) {
                fill(subArray, value);
            }
        }

    }
}


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

public class p4
{
	BufferedReader br;
	StringTokenizer st;
	BufferedWriter bw;
	public static void main(String[] args)throws Exception
	{
		new p4().run();
	}

	void run()throws IOException
	{
		br = new BufferedReader(new InputStreamReader(System.in));
		bw=new BufferedWriter(new OutputStreamWriter(System.out));
		solve();
	}
	void solve() throws IOException
	{
		int limit=100001;
		int fac[]=new int[limit];
		int invfac[]=new int[limit];
		fac[0]=1;
		invfac[0]=1;
		int mod=1000000007;
		for(int i=0;++i<limit;)
		{
			long x=(long)fac[i-1]*i;
			x%=mod;
			fac[i]=(int)x;
			invfac[i]=(int)nPowerM(fac[i], mod-2);
		}

		int t=1;
		while(t-->0)
		{
			int n=ni();
			int k=ni();

			long ans=1;
			k=Math.min(k, n);
			for(int i=0;++i<=k;)
			{
				long x=fac[n];
				x*=invfac[i];
				x%=mod;
				x*=invfac[n-i];
				x%=mod;

				ans+=x;
				ans%=mod;
			}

			bw.write(ans+"\n");

		}
		bw.flush();
	}

	public long mod(long a)
	{
		int mod=1000000007;
		a%=mod;
		if(a<0)
			a+=mod;

		return a;
	}

	public long nPowerM(int n, int m)
	{
		if(m==0)
			return 1L;

		int mod=1000000007;
		long ans=nPowerM(n, m/2);
		ans*=ans;
		ans%=mod;

		if(m%2==1)
		{
			ans*=n;
			ans%=mod;
		}

		return ans;
	}
	/////////////////////////////////////// FOR INPUT ///////////////////////////////////////

	int[] nai(int n) { int a[]=new int[n]; for(int i=-1;++i<n;)a[i]=ni();	return a;}
	Integer[] naI(int n) { Integer a[]=new Integer[n]; for(int i=-1;++i<n;)a[i]=ni();	return a;}
	long[] nal(int n) { long a[]=new long[n]; for(int i=-1;++i<n;)a[i]=nl();	return a;}
	char[] nac()	{char c[]=nextLine().toCharArray();	return c;}
	char [][] nmc(int n)	{char c[][]=new char[n][]; for(int i=-1;++i<n;)c[i]=nac();	return c;}
	int[][] nmi(int r, int c)	{int a[][]=new int[r][c]; for(int i=-1;++i<r;)a[i]=nai(c);	return a;}
	String next()
	{
			while (st == null || !st.hasMoreElements()) {
					try {st = new StringTokenizer(br.readLine());}
					catch (IOException e) {e.printStackTrace();}
				}
			return st.nextToken();
	}
	int ni() { return Integer.parseInt(next()); }
	byte nb() { return Byte.parseByte(next()); }
	short ns() { return Short.parseShort(next()); }
	long nl() { return Long.parseLong(next()); }
	double nd()	{	return Double.parseDouble(next());	}
	String nextLine()
	{
			String str = "";
			try {str = br.readLine();}
			catch (IOException e) {e.printStackTrace();}
			return str;
	}
}


import java.util.*;

public class Main {

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

        int count = 0;

        if(str.contains("Danil")){
        	if(str.indexOf("Danil") != str.lastIndexOf("Danil")){
        		System.out.println("NO");
        		return;
        	}

        	count++;
        }
        if(str.contains("Olya")){
        	if(str.indexOf("Olya") != str.lastIndexOf("Olya")){
        		System.out.println("NO");
        		return;
        	}
        	count++;
        	if(count>1){
        		System.out.println("NO");
        		return;
        	}
        }
        if(str.contains("Slava")){
        	if(str.indexOf("Slava") != str.lastIndexOf("Slava")){
        		System.out.println("NO");
        		return;
        	}
        	count++;
        	if(count>1){
        		System.out.println("NO");
        		return;
        	}
        }
        if(str.contains("Ann")){
        	if(str.indexOf("Ann") != str.lastIndexOf("Ann")){
        		System.out.println("NO");
        		return;
        	}
        	count++;
        	if(count>1){
        		System.out.println("NO");
        		return;
        	}
        }
        if(str.contains("Nikita")){
        	if(str.indexOf("Nikita") != str.lastIndexOf("Nikita")){
        		System.out.println("NO");
        		return;
        	}
        	count++;
        	if(count>1){
        		System.out.println("NO");
        		return;
        	}
        }

        if(count == 1)
        	System.out.println("YES");
        else
        	System.out.println("NO");
	}

}


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

public class Main {

    private static Reader in;
    private static PrintWriter out;

    private static int n;
    private static int[] upper, lower, f;

    public static void main(String[] args) throws IOException {
        in = new Reader(System.in);
        out = new PrintWriter(System.out);

        upper = new int[20];
        lower = new int[20];
        int l1 = to_digit(in.nextLong(), lower);
        int l2 = to_digit(in.nextLong(), upper);

        n = l2;
        f = new int[20];
        out.println( dfs(0, n) );

        out.close();
    }

    private static int to_digit(long x, int[] a) {
        int n = 0;
        while (x != 0) {
            ++n;
            a[n] = (int) (x % 10);
            x /= 10;
        }
        return n;
    }

    private static int dfs(int step, int range) {
        if (step == 9) {
            f[step] = range;
            /*
              for (int i = n; i > 0; --i)
              out.print(f[i] + " ");
              out.println();
            */
            return check(n, true, true) ? 1 : 0;
        }
        int res = 0;
        for (int i = 0; i <= range; ++i) {
            f[step] = i;
            res += dfs(step + 1, range - i);
        }
        return res;
    }

    private static boolean check(int step, boolean left_flag, boolean right_flag) {
        if (step == 0) return true;
        if (!left_flag && !right_flag) return true;

        //out.println(step + " " + left_flag + " " + right_flag);

        int left = left_flag ? lower[step] : 0;
        int right = right_flag ? upper[step] : 9;

        //out.println(step + " " + left + " " + right);
        for (int i = left; i <= right; ++i)
            if (f[i] != 0) {
                --f[i];
                boolean res = check(step - 1, left_flag && i == left, right_flag && i == right);
                ++f[i];
                if (res) return true;
            }
        return false;
    }

}

/** Class for buffered reading int and double values */
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    Reader(InputStream input) {
        init(input);
    }

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
            new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                reader.readLine() );
        }
        return tokenizer.nextToken();
    }

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

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

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

}


import java.util.*;

public class Solution {

	public static void main(String[] args) {

	        Scanner in = new Scanner(System.in);
	        int n=in.nextInt();
	        int a1=in.nextInt();
	        int b1=in.nextInt();
	        int c1=in.nextInt();

	        long a=a1,b=b1,c=c1;

	        int m=n%4;
	        if(m==0){
	        	System.out.println(0);
	        	return;
	        }

	        if(m==3){
	       	long q=Math.min(a, b+c);

	        	long w=Math.min(3*c, q);
	        	System.out.println(w);
	        	return;
	        }
	        else if(m==2){
	        	long q=Math.min(2*a, b);
	        	long w=Math.min(2*c, q);
	        	System.out.println(w);
	        }

	        if(m==1){
	        	long q=Math.min(3*a, a+b);
	        	long z=Math.min(q,c);
	        	System.out.println(z);
	        }

 }
}


import java.util.*;

public class bucky{

public static void main (String args[]) {

Scanner input=new Scanner(System.in);

HashMap<String, Integer> map= new HashMap<String, Integer>();
HashMap<String, String> map2= new HashMap<String, String>();

		int n=input.nextInt();
		double max=0;double contor=0; int a=0;

		int nums[]=new int[n-1];

	int start;

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

		if(i==0) a=input.nextInt();

		else nums[i-1]=input.nextInt();

		}

		   Arrays.sort(nums);

	start=a;

    while(start<=nums[n-2]){

        start++;
        nums[n-2]--;
        Arrays.sort(nums);

    }

	int result=start-a;
	System.out.println(result);

		}

}


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

public class B implements Runnable {
    private Scanner in;
    private PrintWriter out;

    private void solve() {
        int n = in.nextInt();
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        int ans = 0;
        for (int n2 = 0; n2 <= c && 2 * n2 <= n; ++n2) {
            for (int n1 = 0; n1 <= b && 2 * n2 + n1 <= n; ++n1) {
                int nh = 2 * (n - 2 * n2 - n1);
                if (nh <= a) {
                    ++ans;
                }
            }
        }
        out.println(ans);
    }

    @Override
    public void run() {
        Locale.setDefault(Locale.US);
        in = new Scanner(System.in);
        out = new PrintWriter(System.out);
        solve();
        in.close();
        out.close();
    }

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


/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
     public static void main (String[] args) throws java.lang.Exception
     {
     	Scanner in =new Scanner(System.in);
		int t=1,n,i,j,min,max,k,p=0,x,o,y,a1,a2,k1,k2;
		long d;
		String s="",st;
		for(i=0;i<t;i++)
		{
		     min=max=0;
		    a1=in.nextInt();
		    a2=in.nextInt();
		    k1=in.nextInt();
		    k2=in.nextInt();
		    n=in.nextInt();
		    if(k2<k1)
		    {
		         p=a2;k=n;j=a1;
		         while(p>0 && k>=k2)
		         {
		              k-=k2;
		              max++;
		              p--;
		         }
		         while(k>=k1 && j>0)
		         {
		              k=k-k1;
		              j--;
		              max++;
		         }
		    }
		    else
		    {
		         p=a1;k=n;j=a2;
		         while(p>0 && k>=k1)
		         {
		              k-=k1;
		              max++;
		              p--;
		         }
		         while(k>=k2 && j>0)
		         {
		              k=k-k2;
		              j--;
		              max++;
		         }
		    }
	         k=n-(k2-1)*a2-(k1-1)*a1;
	         min=Math.max(0,k);
	         System.out.println(min+" "+max);
		}
	}
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;

public class Main {

	private static final int MAXN = 5000;
	private static final String NO = "NO";
	private static final String YES = "YES";
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	private static final long MOD = 1000000007L;
	private static final int MAX = Integer.MAX_VALUE / 4;

	void solve() {
		int H = ni() % 12;
		int M = ni();
		int S = ni();
		int T1 = ni();
		int T2 = ni();
		double a[] = new double[3];
		a[0] = S * 360.0 / 60;
		a[1] = (M + S / 60.0) * 360 / 60;
		a[2] = (H + (M + S /60.0) /60) * 360.0 / 12;
//		tr(a);
		Arrays.sort(a);
//		tr(a);
		out.print(get(a, T1) == get(a, T2) ? YES : NO);
	}

	private int get(double[] a, int t) {
		double b = t*360.0/12;
//		tr(b);
		if (b<=a[1] && b>=a[0])
			return 0;
		if (b<=a[2] && b>=a[1])
			return 1;
		return 2;
	}

	long power(long a, long b) {
		long x = 1, y = a;
		while (b > 0) {
			if (b % 2 != 0) {
				x = (x * y) % MOD;
			}
			y = (y * y) % MOD;
			b /= 2;
		}
		return x % MOD;
	}

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

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

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

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

	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	private boolean vis[];

	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) {
			if (!(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 Integer[] na2(int n) {
		Integer[] a = new Integer[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}

	private int[][] na(int n, int m) {
		int[][] a = new int[n][];
		for (int i = 0; i < n; i++)
			a[i] = na(m);
		return a;
	}

	private Integer[][] na2(int n, int m) {
		Integer[][] a = new Integer[n][];
		for (int i = 0; i < n; i++)
			a[i] = na2(m);
		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(int n) {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nl();
		return a;
	}

	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 static void tr(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
}


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

public class Main
{
	static final long mod=(int)1e9+7;
	public static void main(String[] args) throws Exception
	{
		FastReader in=new FastReader();
		PrintWriter pw=new PrintWriter(System.out);
		int n=in.nextInt();
		long m=in.nextInt();
		int k=in.nextInt();
		long[][] dp=new long[n+1][k+1];
		long ans=0;
		for(int i=1;i<=n;i++)
		{
			dp[i][0]=m;

			for(int j=1;j<Math.min(i,k+1);j++)
			{
				dp[i][j]=dp[i-1][j]+dp[i-1][j-1]*(m-1);
				dp[i][j]%=998244353;
			}
		}
		pw.println(dp[n][k]);
		pw.flush();
	}
}

class FastReader
{
	BufferedReader br;
	StringTokenizer st;

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

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

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

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


import	java.util.*;
public	class	Main{
	public	static	void	main(String[]args){
		Scanner	cin=new	Scanner(System.in);
		long	s=0,n=cin.nextLong(),m,x=2,y,z,a,b,c;
		if(0==n%6)	for(n/=3;x*x*x<=n;++x)	if(1>n%x)
			for(z=2-x,y=Math.max(x,(int)Math.ceil((z+Math.sqrt(z*z+((m=n/x)<<2)))/2));y*y<=m;++y)
				if(1>m%y&&1>(1&(x^(z=m/y)^y)))
				{a=x+y-z;s+=(c=(y<<1)-a)==a?1:(b=(x<<1)-a)==a||b==c?3:6;}
		System.out.print(s);
	}
}


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.io.InputStream;
/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Alex
 */
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);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }
    static class TaskD {
        long MOD = 1000 * 1000 * 1000 + 7;
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int A = in.readInt();
            int B = in.readInt();
            long n = in.readLong();
            int x = in.readInt();
            int[][] pow = new int[][] {{A, B}, {0, 1}};
            pow = pow(pow, n);
            int[][] init = new int[][] {{x, 0}, {1, 0}};
            out.printLine(mult(pow, init)[0][0]);
        }
        private int[][] mult(int[][] a, int[][] b) {
            int[][] res = new int[2][2];
            for (int firstrow = 0; firstrow < 2; firstrow++) {
                for (int secondcol = 0; secondcol < 2; secondcol++) {
                    int sum = 0;
                    for (int i = 0; i < 2; i++) {
                        sum += ((long) a[firstrow][i] * b[i][secondcol]) % MOD;
                        if (sum >= MOD) sum -= MOD;
                    }
                    res[firstrow][secondcol] = sum;
                }
            }
            return res;
        }
        private int[][] pow(int[][] mat, long pow) {
            if (pow == 0) return new int[][] {{1, 0}, {0, 1}};
            if (pow == 1) return mat;
            int[][] half = pow(mat, pow / 2);
            int[][] full = mult(half, half);
            if (pow % 2 == 1) {
                full = mult(full, mat);
            }
            return full;
        }
    }
    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 close() {
            writer.close();
        }
        public void printLine(int i) {
            writer.println(i);
        }
    }
    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 int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
        public long readLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return isWhitespace(c);
        }
        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}


import java.util.*;

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

import static java.lang.Math.*;

public class Solution implements Runnable {

	long mod = (long) 1e9 + 7;
	int mod2 = 998244353;

	public void solve() throws Exception {
		long x = sc.nextLong();
		long k =sc.nextLong();
		//out.println(calculate(x));
		if(x==0) {
			out.println(0);
			return;
		}
		out.println(((((((calculate(k+1)%mod)*(x%mod))%mod)+mod)%mod - (calculate(k))%mod + 1l)%mod+mod)%mod);

	}

	public long calculate(long pow) {
		if(pow==1) return 2l;
		if(pow==0) return 1l;
		long temp = calculate(pow/2);
		if(pow%2==1)
		{
			return ((((temp%mod)*(temp%mod)*2l)%mod)+mod)%mod;
		}
		else
		{
			return ((((temp%mod)*(temp%mod))%mod)+mod)%mod;
		}
	}

	static void sort(int[] a) {
		ArrayList<Integer> l = new ArrayList<>();
		for (int i : a)
			l.add(i);
		Collections.sort(l);
		for (int i = 0; i < a.length; i++)
			a[i] = l.get(i);
	}

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

	static long ncr(int n, int r, long p) {
		if (r > n)
			return 0l;
		if (r > n - r)
			r = n - r;

		long C[] = new long[r + 1];

		C[0] = 1;

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

			for (int j = Math.min(i, r); j > 0; j--)
				C[j] = (C[j] + C[j - 1]) % p;
		}
		return C[r] % p;
	}

	public long power(long x, long y, long p) {
		long res = 1;
		// out.println(x+" "+y);
		x = x % p;
		if (x == 0)
			return 0;

		while (y > 0) {
			if ((y & 1) == 1)
				res = (res * x) % p;
			y = y >> 1;
			x = (x * x) % p;
		}
		return res;
	}

	static Throwable uncaught;

	BufferedReader in;
	FastScanner sc;
	PrintWriter out;

	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			sc = new FastScanner(in);
			solve();
		} catch (Throwable uncaught) {
			Solution.uncaught = uncaught;
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) throws Throwable {
		Thread thread = new Thread(null, new Solution(), "", (1 << 26));
		thread.start();
		thread.join();
		if (Solution.uncaught != null) {
			throw Solution.uncaught;
		}
	}

}

class FastScanner {

	BufferedReader in;
	StringTokenizer st;

	public FastScanner(BufferedReader in) {
		this.in = in;
	}

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

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

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

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

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

}


// Let f(x) = # of sequences a_i such that gcd() = 1, sum(a_i)=x. We want f(y - x). This is a hard AF function to calculate: easiest recurrence would be sum(f_i) where i<t, gcd(t,i)=1,
// but that's not fast enough at all.

// Trick from editorial: Relate a function to an easier function with a relationship IN EITHER DIRECTION. Here, let g(t) = # of sequences w/o gcd()=1 condition.
// Then, we realize that a_i can onyl have so many gcd's, so g(t)=sum_(d|t) f(t/d), f(t)=g(t)-sum_(d|t, d not t) f(t).

// Notice the repeats in f(t). That means we can DP it. Let's code!

import java.util.*;
public class Unusual {
	private static final int N = 1000000007;
	public static long pow(int n) {
		if (n == 0) return 1;
		long x = pow(n / 2);
		x = (x * x) % N;
		if (n % 2 == 1) x = (x * 2) % N;
		return x;
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int x = in.nextInt(), y = in.nextInt(), t = y / x, i;
		if (y % x != 0) {
			System.out.println(0);
			System.exit(0);
		}
		ArrayList<Integer> divisors = new ArrayList<Integer>();
		for (i = 1; i * i < t; i++) {
			if (t % i == 0) {
				divisors.add(i); divisors.add(t / i);
			}
		}
		if (i * i == t) divisors.add(i);
		int[] d = new int[divisors.size()];
		for (i = 0; i < d.length; i++) {
			d[i] = divisors.get(i);
		}
		Arrays.sort(d);
		long[] f = new long[divisors.size()];
		f[0] = 1;
		for (i = 1; i < d.length; i++) {
			f[i] = pow(d[i] - 1);
			for (int j = 0; j < i; j++) {
				if (d[i] % d[j] == 0) f[i] = (f[i] - f[j] + N) % N;
			}
		}
//		System.out.println(Arrays.toString(d));
//		System.out.println(Arrays.toString(f));
		System.out.println(f[d.length - 1]);
	}
}


import java.util.Scanner;

public class E {
    public static boolean solve(long x, long y, long c, long d) {
        if (x == 0 && y == 0)
            return c == 0 && d == 0;
        if (x == 0)
            return c % y == 0 && d % y == 0;
        if (y == 0)
            return c % x == 0 && d % x == 0;
        if ((c * x + d * y) % (x * x + y * y) != 0)
            return false;
        long a = (c * x + d * y) / (x * x + y * y);
        return (d - a * y) % x == 0;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long ax = in.nextInt();
        long ay = in.nextInt();
        long bx = in.nextInt();
        long by = in.nextInt();
        long cx = in.nextInt();
        long cy = in.nextInt();
        if (solve(cx, cy, bx - ax, by - ay) || solve(cx, cy, bx - ay, by + ax)
                || solve(cx, cy, bx + ax, by + ay)
                || solve(cx, cy, bx + ay, by - ax))
            System.out.println("YES");
        else
            System.out.println("NO");
    }

}


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

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

public class D {

	final static boolean autoflush = false;

	final static int MOD = (int) 1e9 + 7;
	final static double eps = 1e-9;
	final static int INF = (int) 1e9;

	Integer [] A;
	int N; int [] Q; int [][] R;

	public D () {
		N = sc.nextInt();
		A = sc.nextInts();

		if (N == 1)
			exit(1);

		next: for (int i : rep(1, N)) {
			for (int j : rep(i))
				for (int k : rep(i))
					if (A[j] + A[k] == A[i])
						continue next;
			exit(-1);
		}

		Map<Integer, Integer> M = new TreeMap<Integer, Integer>();
		for (int i : rep(N))
			M.put(A[i], i);

		Q = new int [N];
		R = new int [N][N];

		for (int i : rep(N))
			for (int j : rep(i))
				if (M.containsKey(A[i] - A[j])) {
					int k = M.get(A[i] - A[j]);
					Q[i] |= (1 << j);
					R[i][j] = k;
				}

		H = new int [1 << (N-1)]; fill(H, -1);

		int res = solve(1, 1);
		assert res <= N;
		exit(res);
	}

	int [] H;

	int solve(int X, int J) {
		int B = (X & Q[J]);
		if (B == 0)
			return H[X] = INF;

		for (int i = 0; i < J; ++i) {
			int U = (1 << i), V = (1 << R[J][i]);
			if ((B&U) != 0 && (B&V) != 0)
				break;
			if (i == J-1)
				return H[X] = INF;
		}

		if (J == N-1)
			return H[X] = Integer.bitCount(X);

		int Y = (X | (1 << J)), res = INF;
		for (int j = 0; j < J; ++j) {
			int W = (1 << j);
			if ((Y & W) != 0) {
				int V = Y & (~W), R = H[V] == -1 ? solve(V, J+1) : H[V];
				res = min(res, R);
			}
		}
		int R = H[Y] == -1 ? solve(Y, J+1) : H[Y];
		res = min(res, R);
		return H[X] = res;
	}

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

	static int [] rep(int N) { return rep(0, N); }
	static int [] rep(int S, int T) { int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
	static int [] req(int S, int T) { return rep(S, T+1); }

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

	/* Dear hacker, don't bother reading below this line, unless you want to help me debug my I/O routines :-) */

	final static MyScanner sc = new MyScanner();

	static class MyScanner {
		public String next() {
			newLine();
			return line[index++];
		}

		public char nextChar() {
			return next().charAt(0);
		}

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

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

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

		public String nextLine() {
			line = null;
			return readLine();
		}

		public String [] nextStrings() {
			line = null;
			return readLine().split(" ");
		}

		public char [] nextChars() {
			return next ().toCharArray ();
		}

		public Integer [] nextInts() {
			String [] L = nextStrings();
			Integer [] res = new Integer [L.length];
			for (int i : rep(L.length))
				res[i] = Integer.parseInt(L[i]);
			return res;
		}

		public Long [] nextLongs() {
			String [] L = nextStrings();
			Long [] res = new Long [L.length];
			for (int i : rep(L.length))
				res[i] = Long.parseLong(L[i]);
			return res;
		}

		public Double [] nextDoubles() {
			String [] L = nextStrings();
			Double [] res = new Double [L.length];
			for (int i : rep(L.length))
				res[i] = Double.parseDouble(L[i]);
			return res;
		}

		public String [] next (int N) {
			String [] res = new String [N];
			for (int i : rep(N))
				res[i] = sc.next();
			return res;
		}

		public Integer [] nextInt (int N) {
			Integer [] res = new Integer [N];
			for (int i : rep(N))
				res[i] = sc.nextInt();
			return res;
		}

		public Long [] nextLong (int N) {
			Long [] res = new Long [N];
			for (int i : rep(N))
				res[i] = sc.nextLong();
			return res;
		}

		public Double [] nextDouble (int N) {
			Double [] res = new Double [N];
			for (int i : rep(N))
				res[i] = sc.nextDouble();
			return res;
		}

		public String [][] nextStrings (int N) {
			String [][] res = new String [N][];
			for (int i : rep(N))
				res[i] = sc.nextStrings();
			return res;
		}

		public Integer [][] nextInts (int N) {
			Integer [][] res = new Integer [N][];
			for (int i : rep(N))
				res[i] = sc.nextInts();
			return res;
		}

		public Long [][] nextLongs (int N) {
			Long [][] res = new Long [N][];
			for (int i : rep(N))
				res[i] = sc.nextLongs();
			return res;
		}

		public Double [][] nextDoubles (int N) {
			Double [][] res = new Double [N][];
			for (int i : rep(N))
				res[i] = sc.nextDoubles();
			return res;
		}

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

		private boolean eol() {
			return index == line.length;
		}

		private String readLine() {
			try {
				return r.readLine();
			} catch (Exception e) {
				throw new Error (e);
			}
		}
		private final BufferedReader r;

		MyScanner () {
			this(new BufferedReader(new InputStreamReader(System.in)));
		}

		MyScanner (BufferedReader r) {
			try {
				this.r = r;
				while (!r.ready())
					Thread.sleep(1);
				start();
			} catch (Exception e) {
				throw new Error(e);
			}
		}

		private String [] line;
		private int index;

		private void newLine() {
			if (line == null || eol()) {
				line = readLine().split(" ");
				index = 0;
			}
		}
	}

	static void print (Object o, Object... a) {
		printDelim(" ", o, a);
	}

	static void cprint (Object o, Object... a) {
		printDelim("", o, a);
	}

	static void printDelim (String delim, Object o, Object... a) {
		pw.println(build(delim, o, a));
	}

	static void exit (Object o, Object... a) {
		print(o, a);
		exit();
	}

	static void exit() {
		pw.close();
		System.out.flush();
		System.err.println("------------------");
		System.err.println("Time: " + ((millis() - t) / 1000.0));
		System.exit(0);
	}

	static void NO() {
		throw new Error("NO!");
	}

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

	static String build (String delim, Object o, Object... a) {
		StringBuilder b = new StringBuilder();
		append(b, o, delim);
		for (Object p : a)
			append(b, p, delim);
		return b.toString().trim();
	}

	static void append(StringBuilder b, Object o, String delim) {
		if (o.getClass().isArray()) {
			int L = Array.getLength(o);
			for (int i : rep(L))
				append(b, Array.get(o, i), delim);
		} else if (o instanceof Iterable<?>) {
			for (Object p : (Iterable<?>)o)
				append(b, p, delim);
		} else
			b.append(delim).append(o);
	}

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

	static void statics() {
		abs(0);
		valueOf(0);
		asList(new Object [0]);
		reverseOrder();
	}

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

	static void start() {
		t = millis();
	}

	static PrintWriter pw = new PrintWriter(System.out, autoflush);

	static long t;

	static long millis() {
		return System.currentTimeMillis();
	}
}


/*package whatever //do not write package name here */

import java.io.*;
import java.util.*;
public class benn {
	public static void main (String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)
		a[i]=sc.nextInt();
		ArrayList<Integer> ar=new ArrayList<>();
		int s=0;
		for(int i=0;i<n;i++){
		     s+=a[i];
		    if(s<=m)
		    {
		    	//sum+=t[i];
		        System.out.print(0+" ");
		    }
		    else
		    {
		    	//System.out.println(sum+" "+a);
		        Collections.sort(ar);
		        int x=0,y=0;
		        for(int j=ar.size()-1;j>=0;j--)
		        {
		            x+=ar.get(j);
		            if(s-x<=m)
		            {
		            	y++;
		                break;
		            }
		            else
		            y++;
		        }
		        System.out.print(y+" ");

		    }
		    ar.add(a[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 P633A {
  InputStream is;
  PrintWriter out;
  String INPUT = "6 11 6";

  void solve() {
    int a = ni();
    int b = ni();
    int c = ni();
    int tmp = 0;
    while (tmp < c) {
      if ((c - tmp) % b == 0) {
        System.out.println("Yes");
        return;
      }
      tmp += a;
    }
    tmp = 0;
    while (tmp < c) {
      if ((c - tmp) % a == 0) {
        System.out.println("Yes");
        return;
      }
      tmp += b;
    }
    System.out.println("No");
  }

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

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

  private byte[] inbuf = new byte[1024];
  private 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));
  }
}


/**
 * BaZ :D
 */
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main
{
    static Reader scan;
    static PrintWriter pw;
    static long MOD = 1_000_000_007;
    static long INF = 1_000_000_000_000_000_000L;
    static long inf = 2_000_000_000;
    public static void main(String[] args) {
        new Thread(null,null,"BaZ",1<<25)
        {
            public void run()
            {
                try
                {
                    solve();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }.start();
    }
    static void solve() throws IOException
    {
        scan = new Reader();
        pw = new PrintWriter(System.out,true);
        StringBuilder sb = new StringBuilder();
        long nCr[][] = new long[101][101];
        for(int i=0;i<=100;++i) {
            nCr[i][0] = 1;
        }
        for(int i=1;i<=100;++i) {
            for(int j=1;j<=100;++j) {
                nCr[i][j] = (nCr[i-1][j] + nCr[i-1][j-1])%MOD;
            }
        }
        int freq[] = new int[101];
        int n = ni();
        int arr[] = new int[n];
        int sum = 0;
        for(int i=0;i<n;++i) {
            arr[i] = ni();
            sum+=arr[i];
            ++freq[arr[i]];
        }
        long ways0[][] = new long[n+1][sum+1];
        long ways1[][] = new long[n+1][sum+1];
        ways0[0][0] = 1;
        for(int i=0;i<n;++i) {
            ways1[0][0] = 1;
            for(int nele=1;nele<=(i+1);nele++) {
                for(int s=0;s<=sum;++s) {
                    if(s<arr[i]) {
                        ways1[nele][s] = ways0[nele][s];
                    }
                    else {
                        ways1[nele][s] = (ways0[nele][s] + ways0[nele-1][s-arr[i]])%MOD;
                    }
                }
            }
            for(int nele=0;nele<=(i+1);nele++) {
                for(int s=0;s<=sum;++s) {
                    ways0[nele][s] = ways1[nele][s];
                }
            }
        }
        int ans = 0;
        int cnt_distinct = 0;
        for(int i=1;i<=100;++i) {
            if(freq[i]>0) {
                cnt_distinct++;
            }
            for(int j=1;j<=freq[i];++j) {
                if(ways0[j][i*j]==nCr[freq[i]][j]) {
                    ans = max(ans, j);
                }
            }
        }
        if(cnt_distinct<=2) {
            pl(n);
        }
        else {
            pl(ans);
        }
        pw.flush();
        pw.close();
    }
    static int ni() throws IOException
    {
        return scan.nextInt();
    }
    static long nl() throws IOException
    {
        return scan.nextLong();
    }
    static double nd() throws IOException
    {
        return scan.nextDouble();
    }
    static void pl()
    {
        pw.println();
    }
    static void p(Object o)
    {
        pw.print(o+" ");
    }
    static void pl(Object o)
    {
        pw.println(o);
    }
    static void psb(StringBuilder sb)
    {
        pw.print(sb);
    }
    static void pa(String arrayName, Object arr[])
    {
        pl(arrayName+" : ");
        for(Object o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, int arr[])
    {
        pl(arrayName+" : ");
        for(int o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, long arr[])
    {
        pl(arrayName+" : ");
        for(long o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, double arr[])
    {
        pl(arrayName+" : ");
        for(double o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, char arr[])
    {
        pl(arrayName+" : ");
        for(char o : arr)
            p(o);
        pl();
    }
    static void pa(String listName, List list)
    {
        pl(listName+" : ");
        for(Object o : list)
            p(o);
        pl();
    }
    static void pa(String arrayName, Object[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(Object o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, int[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(int o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, long[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(long o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, char[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(char o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, double[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(double o : arr[i])
                p(o);
            pl();
        }
    }
    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64];
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            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 long nextLong() throws IOException {
            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 double nextDouble() throws IOException {
            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() throws IOException {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }

        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.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Vadim Semenov
 */
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);
        TaskF solver = new TaskF();
        solver.solve(1, in, out);
        out.close();
    }

    static final class TaskF {
        public void solve(int __, InputReader in, PrintWriter out) {
            String s = in.next();
            String t = in.next();
            int[][] result = solve(s.toCharArray(), t.toCharArray());
            out.println(result.length);
            for (int[] pair : result) {
                out.print(pair[0]);
                out.print(' ');
                out.println(pair[1]);
            }
        }

        private int[][] solve(char[] s, char[] t) {
            int[][] result = solveInternal(s, t);
            int[][] anotherResult = solveInternal(t, s);
            if (anotherResult.length < result.length) {
                result = anotherResult;
                for (int[] pair : result) {
                    ArrayUtils.swap(pair, 0, 1);
                }
            }
            return result;
        }

        private int[][] solveInternal(char[] s, char[] t) {
            List<Integer> sChangings = getChangings(s, 0, s.length, s, 0, 0, 'a');
            List<Integer> tChangings = getChangings(t, 0, t.length, t, 0, 0, 'b');
            int[][] result = solveGreedy(s, 0, s.length, s, 0, 0, t, 0, t.length, t, 0, 0, 0, Integer.MAX_VALUE);
            assert result != null;
            int balance = (sChangings.size() - tChangings.size()) / 2;
            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    int sp = (balance > 0 ? balance : 0) + i;
                    int tp = (balance < 0 ? -balance : 0) + j;
                    if ((sp != 0 && sp >= sChangings.size()) || (tp != 0 && tp >= tChangings.size())) continue;
                    int sSep = sp < sChangings.size() ? sChangings.get(sp) : 0;
                    int tSep = tp < tChangings.size() ? tChangings.get(tp) : 0;
                    int[][] current = solveGreedy(t, 0, tSep, s, sSep, s.length, s, 0, sSep, t, tSep, t.length, 1, result.length);
                    if (current != null) {
                        result = current;
                        current[0][0] = sSep;
                        current[0][1] = tSep;
                    }
                }
            }
            return result;
        }

        private int[][] solveGreedy(char[] s1, int s1from, int s1to,
                                    char[] s2, int s2from, int s2to,
                                    char[] t1, int t1from, int t1to,
                                    char[] t2, int t2from, int t2to,
                                    int offset, int limit) {
            List<Integer> ss = getChangings(s1, s1from, s1to, s2, s2from, s2to, 'a');
            List<Integer> tt = getChangings(t1, t1from, t1to, t2, t2from, t2to, 'b');
            int length = Math.max(ss.size(), tt.size());
            if (offset + length >= limit) {
                return null;
            }
            int[][] result = new int[offset + length][2];
            for (int i = 0; i < length; ++i) {
                result[offset + i][0] = pop(ss);
                result[offset + i][1] = pop(tt);
                List<Integer> tmp = ss;
                ss = tt;
                tt = tmp;
            }
            return result;
        }

        private List<Integer> getChangings(char[] s1, int s1from, int s1to, char[] s2, int s2from, int s2to, char target) {
            List<Integer> changings = new ArrayList<>();
            for (int i = s2to; i-- > s2from; ) {
                char current = s2[i];
                if (current != target) {
                    changings.add(s1to - s1from + i - s2from + 1);
                    target = current;
                }
            }
            for (int i = s1to; i-- > s1from; ) {
                char current = s1[i];
                if (current != target) {
                    changings.add(i - s1from + 1);
                    target = current;
                }
            }
            Collections.reverse(changings);
            return changings;
        }

        private static int pop(List<Integer> stack) {
            return stack.isEmpty() ? 0 : stack.remove(stack.size() - 1);
        }

    }

    static class InputReader {
        private final BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

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

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

    }

    static class ArrayUtils {
        private ArrayUtils() {
        }

        public static void swap(int[] array, int i, int j) {
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }

    }
}


import java.util.Scanner;

public class D {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] mas = {0,1,0,18,0,1800,0,670320,0,734832000,0,890786230,0,695720788,0,150347555};
		int n = sc.nextInt();
		if (n%2 == 0) {
			System.out.println(0);
		}else{
			System.out.println(mas[n]);
		}
	}
}