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

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

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

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

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

            }
        }
    }

    static class IO {

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

        StringBuilder builder;
        PrintWriter pw;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    }

}


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


import java.util.Scanner;

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

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


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

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

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

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

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

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

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

	}

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


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

public class _194A {

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


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

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

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

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

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

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

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

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

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

    }

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

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

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

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

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

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

    }
}


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

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

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

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

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

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

        res/=m*d;

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

    static class fastio {

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

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

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

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

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

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

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

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

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

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

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

}


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


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

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

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

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


import java.util.Scanner;

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

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

        System.out.println(s);
    }

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

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


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.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.io.InputStream;

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

    static class _1047D {
        int n;
        int m;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.nextInt();
            m = in.nextInt();

            long ans = 0;

            if (n > 100) {
                int nn = (n - 100) % 6;
                long nq = (n - 100) / 6;
                ans += nq * 6 * m;
                n = 100 + nn;
            }

            if (m > 100) {
                int nm = (m - 100) % 6;
                long mq = (m - 100) / 6;
                ans += mq * 6 * n;
                m = 100 + nm;
            }

            int[][] mat = new int[n + 1][m + 1];

            int u = 1;
            int v = 1;
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    if ((i + j) % 2 == 0) {
                        mat[i][j] = u;
                        u++;
                    }
                }
            }

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    if ((i + j) % 2 != 0) {
                        mat[i][j] = v;
                        v++;
                    }
                }
            }

            List[] g = new List[u];

            for (int i = 0; i < u; i++) {
                g[i] = new ArrayList();
            }

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    if ((i + j) % 2 == 0) {
                        addEdge(mat, g, i, j, i - 3, j);
                        addEdge(mat, g, i, j, i - 2, j - 1);
                        addEdge(mat, g, i, j, i - 2, j + 1);
                        addEdge(mat, g, i, j, i, j - 3);
                        addEdge(mat, g, i, j, i - 1, j - 2);
                        addEdge(mat, g, i, j, i + 1, j - 2);
                        addEdge(mat, g, i, j, i, j + 3);
                        addEdge(mat, g, i, j, i - 1, j + 2);
                        addEdge(mat, g, i, j, i + 1, j + 2);
                        addEdge(mat, g, i, j, i + 3, j);
                        addEdge(mat, g, i, j, i + 2, j - 1);
                        addEdge(mat, g, i, j, i + 2, j + 1);
                    }
                }
            }

            HopcroftKarpBipartiteMatching bpm = new HopcroftKarpBipartiteMatching(u - 1, v - 1, g);

            ans += 2 * bpm.maximumMatching();

            out.println(ans);
        }

        void addEdge(int[][] mat, List[] g, int i, int j, int s, int t) {
            if (s <= 0 || t <= 0 || s > n || t > m)
                return;

            g[mat[i][j]].add(mat[s][t]);
        }

        class HopcroftKarpBipartiteMatching {
            int m;
            int n;
            List[] g;
            int[] pairU;
            int[] pairV;
            int[] dis;
            int inf = 10000000;
            int NIL = 0;

            public HopcroftKarpBipartiteMatching(int m, int n, List[] g) {
                this.m = m;
                this.n = n;
                this.g = g;
                this.pairU = new int[m + 1];
                this.pairV = new int[n + 1];
                this.dis = new int[m + 1];
            }

            int maximumMatching() {

                Arrays.fill(pairU, NIL);
                Arrays.fill(pairV, NIL);

                int maxMatching = 0;

                while (bfs()) {
                    for (int i = 1; i <= m; i++) {
                        if (pairU[i] == NIL && dfs(i))
                            maxMatching++;
                    }
                }

                return maxMatching;
            }

            boolean bfs() {

                ArrayDeque<Integer> queue = new ArrayDeque<>();

                for (int i = 1; i <= m; i++) {
                    if (pairU[i] == NIL) {
                        queue.addLast(i);
                        dis[i] = 0;
                    } else {
                        dis[i] = inf;
                    }
                }

                dis[NIL] = inf;

                while (!queue.isEmpty()) {
                    int u = queue.removeFirst();

                    if (dis[u] < dis[NIL]) {
                        for (int v : (List<Integer>) g[u]) {
                            if (dis[pairV[v]] == inf) {
                                queue.addLast(pairV[v]);
                                dis[pairV[v]] = dis[u] + 2;
                            }
                        }
                    }
                }

                return dis[NIL] < inf;
            }

            boolean dfs(int u) {
                if (u != NIL) {
                    for (int v : (List<Integer>) g[u]) {
                        if (dis[pairV[v]] == dis[u] + 2) {
                            if (dfs(pairV[v])) {
                                pairV[v] = u;
                                pairU[u] = v;
                                return true;
                            }
                        }
                    }
                    dis[u] = inf;
                    return false;
                } else
                    return true;
            }

        }

    }

    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 static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        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 boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(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 println(long i) {
            writer.println(i);
        }

    }
}


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

public class C {

    long mod = (int) 1e9 + 7;

    long binpow(long a, long n) {
        long res = 1;
        while (n > 0) {
            if ((n & 1) > 0) {
                res *= a;
                res %= mod;
            }
            a *= a;
            a %= mod;
            n >>= 1;
        }
        return res;
    }

    public void solve() throws IOException {
        long x = nextInt(), n = nextLong();
        Map<Integer, Long> fact = new HashMap<>();
        Set<Integer> div = new HashSet<>();
        int t = (int) x;
        for (int d = 2; d * d <= t; d++) {
            while (t % d == 0) {
                fact.putIfAbsent(d, 0L);
                div.add(d);
                t /= d;
            }
        }
        if (t > 1) {
            fact.putIfAbsent(t, 0L);
            div.add(t);
        }
        for (Integer v : div) {
            BigInteger cur = BigInteger.valueOf(v);
            while (cur.compareTo(BigInteger.valueOf(n)) <= 0) {
                fact.put(v, fact.get(v) + n / cur.longValue());
                cur = cur.multiply(BigInteger.valueOf(v));
            }
        }
        BigInteger ans = BigInteger.ONE;
        for (Map.Entry<Integer, Long> e : fact.entrySet()) {
            if (e.getValue() > 0) {
                ans = ans.multiply(BigInteger.valueOf(binpow(e.getKey(), e.getValue())));
                ans = ans.mod(BigInteger.valueOf(mod));
            }
        }
        out.print(ans);
    }

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

            solve();

            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    BufferedReader br;
    StringTokenizer in;
    PrintWriter out;

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

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

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

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

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

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


import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
public class Solution{
    static InputReader sc;
    static PrintWriter wc;
    static long gcd(long a, long b){
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
    public static void main(String[] args) {
        sc=new InputReader(System.in);
        wc=new PrintWriter(System.out);
        long a=sc.nextLong(),b=sc.nextLong();
        if(a>b){
            long temp=a;
            a=b;
            b=temp;
        }
        else if(a==b){
            wc.println(0);
            wc.close();
            return;
        }
        long n=a,m=b;
        long i;
        long k=Long.MAX_VALUE,add;
        long min=Long.MAX_VALUE,lcm;
        for(i=1;i*i<=(b-a);i++){
            a=n;
            b=m;
            if((b-a)%i==0){
                if(a%i==0){
                    lcm=a*b/gcd(a,b);
                    if(min>=lcm){
                        min=lcm;
                        k=0;
                    }
                }
                else{
                    add=((a/i)+1)*i-a;
                    a+=add;
                    b+=add;
                    lcm=a*b/gcd(a,b);
                    if(min>lcm){
                        min=lcm;
                        k=add;
                    }
                    else if(min==lcm){
                        if(k>add) k=add;
                    }
                }
                //wc.println(lcm);
                a=n;
                b=m;
                long j=(b-a)/i;
                if(a%j==0){
                    lcm=a*b/gcd(a,b);
                    if(min>=lcm){
                        min=lcm;
                        k=0;
                    }
                }
                else{
                    add=((a/j)+1)*j-a;
                    a+=add;
                    b+=add;
                    lcm=a*b/gcd(a,b);
                    if(min>lcm){
                        min=lcm;
                        k=add;
                    }
                    else if(min==lcm){
                        if(k>add) k=add;
                    }
                }
                //wc.println(lcm);
                a=n;
                b=m;
            }
        }
        wc.println(k);
        wc.close();
    }
    static class InputReader {

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

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

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

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

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

        public String nextLine()
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            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);
        }
    }
}


import java.util.*;

public class StronglyConnectedCity {
    public static void main(String s[]) {
        Scanner inp = new Scanner(System.in);

        int n=inp.nextInt();
        int m=inp.nextInt();
        inp.nextLine();
        String hl = inp.nextLine();
        String vl = inp.nextLine();

        SCGraph scGraph = new SCGraph(n, m);

        for(int i=0;i<hl.length();i++) {
            if(hl.charAt(i)=='>') {
                for(int j=1;j<m;j++) {
                    scGraph.arr[scGraph.hash(i, j-1)][scGraph.hash(i, j)]=1;
                }
            }
            else if(hl.charAt(i)=='<') {
                for(int j=m-2;j>=0;j--) {
                    scGraph.arr[scGraph.hash(i, j+1)][scGraph.hash(i, j)]=1;
                }
            }
            else {
                System.out.println("Error!");
            }
        }

        for(int i=0;i<vl.length();i++) {
            if(vl.charAt(i) == 'v') {
                for(int j=1;j<n;j++) {//iterating on rows
                    scGraph.arr[scGraph.hash(j-1, i)][scGraph.hash(j, i)]=1;
                }
            }
            else if(vl.charAt(i) == '^'){
                for(int j=n-2;j>=0;j--) {
                    scGraph.arr[scGraph.hash(j+1, i)][scGraph.hash(j, i)]=1;
                }
            }
            else
                System.out.println("Error!!!");
        }

        for(int k=0;k<(m*n);k++) {
            for(int i=0;i<(n*m);i++) {
                for(int j=0;j< (n*m);j++) {
                    scGraph.arr[j][j]=1;
                    if((scGraph.arr[i][k]==1) && (scGraph.arr[k][j]==1))
                        scGraph.arr[i][j]=1;

                }
            }
        }

        /*for(int i=0;i<(n*m);i++) {
            for(int j=0;j<(n*m);j++) {
                for(int k=0;k<(n*m);k++) {
                    scGraph.arr[k][k]=1;

                    if( (scGraph.arr[i][k]==1) && (scGraph.arr[k][j]==1) )
                    {
                        scGraph.arr[i][j]=1;
                    }
                }
            }
        } */

        boolean result=true;

        for(int i=0;(i< (n*m))  && (result==true) ;i++) {
            for(int j=0;(j<(n*m)) && (result==true) ;j++) {
                if(scGraph.arr[i][j] != 1) {
                    result=false;
                }
            }
        }

        if(result==true) {
            System.out.println("YES");
        }
        else{
            System.out.println("NO");
        }

    }

}

class SCGraph {
    int n,m;
    int arr[][];//adj mtrix

    SCGraph(int n, int m) {
        this.n=n;
        this.m=m;
        arr = new int[n*m][n*m];
    }

    int hash(int i, int j) {
        return m*i+j;
    }
}


import java.util.Scanner;
public class expr {
	public static void main (String [] wee){

		Scanner sti =new Scanner(System.in);

		int a,b,c;
		a= sti.nextInt();
		b= sti.nextInt();
		c= sti.nextInt();

		int sum = 0 ;
		int high = 0 ;

		sum = a+b*c;
			if(sum > high){
				high = sum;
			}
		sum = a*(b+c);
			if(sum > high){
				high = sum;
			}
		sum = a*b*c;
			if(sum > high){
				high = sum;
			}
		sum = (a+b)*c;
			if(sum > high){
				high = sum;
			}
		sum = a+b+c;
			if(sum > high){
				high = sum;
			}

		System.out.println(high);
	}
}


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Random;

public class CF1204E {
    public static void main(String[] args) throws Exception {
        boolean local = System.getProperty("ONLINE_JUDGE") == null;
        boolean async = false;

        Charset charset = Charset.forName("ascii");

        FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset);
        Task task = new Task(io, new Debug(local));

        if (async) {
            Thread t = new Thread(null, task, "dalt", 1 << 27);
            t.setPriority(Thread.MAX_PRIORITY);
            t.start();
            t.join();
        } else {
            task.run();
        }

        if (local) {
            io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M");
        }

        io.flush();
    }

    public static class Task implements Runnable {
        final FastIO io;
        final Debug debug;
        int inf = (int) 1e8;

        public Task(FastIO io, Debug debug) {
            this.io = io;
            this.debug = debug;
        }

        @Override
        public void run() {
            solve();

//            debug.debug("bf", bruteForce(3, 1));
//            debug.debug("so", solve(3, 1));
//            debug.assertTrue(bruteForce(3, 1) == solve(3, 1));
//            for(int i = 0; i < 10; i++){
//                for(int j = 0; j < 10; j++){
//                    debug.debug("i", i);
//                    debug.debug("j", j);
//                    debug.assertTrue(bruteForce(i, j) == solve(i, j));
//                }
//            }
        }

        public int bruteForce(int n, int m) {
            return allPerm(n, m, 0, 0);
        }

        public int allPerm(int n, int m, int max, int val) {
            max = Math.max(max, val);
            if (n == 0 && m == 0) {
                return max;
            }
            int sum = 0;
            if (n > 0) {
                sum = mod.plus(sum, allPerm(n - 1, m, max, val + 1));
            }
            if (m > 0) {
                sum = mod.plus(sum, allPerm(n, m - 1, max, val - 1));
            }
            return sum;
        }

        NumberTheory.Modular mod = new NumberTheory.Modular(998244853);
        NumberTheory.Composite comp = new NumberTheory.Composite(4000, mod);

        public void solve() {
            int n = io.readInt();
            int m = io.readInt();
            io.cache.append(solve(n, m));
        }

        public int solve(int n, int m) {
            int[] leq = new int[n + 1];
            leq[0] = comp.composite(n + m, n);
            for (int i = 0; i <= n; i++) {
                leq[i] = pathNoGreaterThan(n, m, i);
            }

            debug.debug("leq", leq);
            for (int i = n; i > 0; i--) {
                leq[i] = mod.plus(leq[i], -leq[i - 1]);
            }
            debug.debug("leq", leq);

            int sum = 0;
            for (int i = 0; i <= n; i++) {
                sum = mod.plus(sum, mod.mul(leq[i], i));
            }
            return sum;
        }

        //all prefix not greater than k
        public int pathNoGreaterThan(int n, int m, int k) {
            return path(m, n, k);
        }

        //find how many permutation satisfy follow condition:
        // - n * 1
        // - m * -1
        // - all prefix greater than -k
        public int path(int n, int m, int k) {
            if (n - m < -k) {
                return 0;
            }
            return mod.plus(comp.composite(n + m, n), -comp.composite(n + m,
                    n + 1 + k));
        }
    }

    public static class NumberTheory {
        private static final Random RANDOM = new Random();

        /**
         * Extend gcd
         */
        public static class ExtGCD {
            private long x;
            private long y;
            private long g;

            public long getX() {
                return x;
            }

            public long getY() {
                return y;
            }

            /**
             * Get g = Gcd(a, b) and find a way to set x and y to match ax+by=g
             */
            public long extgcd(long a, long b) {
                if (a >= b) {
                    g = extgcd0(a, b);
                } else {
                    g = extgcd0(b, a);
                    long tmp = x;
                    x = y;
                    y = tmp;
                }
                return g;
            }

            private long extgcd0(long a, long b) {
                if (b == 0) {
                    x = 1;
                    y = 0;
                    return a;
                }
                long g = extgcd0(b, a % b);
                long n = x;
                long m = y;
                x = m;
                y = n - m * (a / b);
                return g;
            }
        }

        public static class Gcd {
            public long gcd(long a, long b) {
                return a >= b ? gcd0(a, b) : gcd0(b, a);
            }

            private long gcd0(long a, long b) {
                return b == 0 ? a : gcd0(b, a % b);
            }

            public int gcd(int a, int b) {
                return a >= b ? gcd0(a, b) : gcd0(b, a);
            }

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

        /**
         * Mod operations
         */
        public static class Modular {
            int m;

            public Modular(int m) {
                this.m = m;
            }

            public int valueOf(int x) {
                x %= m;
                if (x < 0) {
                    x += m;
                }
                return x;
            }

            public int valueOf(long x) {
                x %= m;
                if (x < 0) {
                    x += m;
                }
                return (int) x;
            }

            public int mul(int x, int y) {
                return valueOf((long) x * y);
            }

            public int mul(long x, long y) {
                x = valueOf(x);
                y = valueOf(y);
                return valueOf(x * y);
            }

            public int plus(int x, int y) {
                return valueOf(x + y);
            }

            public int plus(long x, long y) {
                x = valueOf(x);
                y = valueOf(y);
                return valueOf(x + y);
            }

            @Override
            public String toString() {
                return "mod " + m;
            }
        }

        /**
         * Bit operations
         */
        public static class BitOperator {
            public int bitAt(int x, int i) {
                return (x >> i) & 1;
            }

            public int bitAt(long x, int i) {
                return (int) ((x >> i) & 1);
            }

            public int setBit(int x, int i, boolean v) {
                if (v) {
                    x |= 1 << i;
                } else {
                    x &= ~(1 << i);
                }
                return x;
            }

            public long setBit(long x, int i, boolean v) {
                if (v) {
                    x |= 1L << i;
                } else {
                    x &= ~(1L << i);
                }
                return x;
            }

            /**
             * Determine whether x is subset of y
             */
            public boolean subset(long x, long y) {
                return intersect(x, y) == x;
            }

            /**
             * Merge two set
             */
            public long merge(long x, long y) {
                return x | y;
            }

            public long intersect(long x, long y) {
                return x & y;
            }

            public long differ(long x, long y) {
                return x - intersect(x, y);
            }
        }

        /**
         * Power operations
         */
        public static class Power {
            final Modular modular;

            public Power(Modular modular) {
                this.modular = modular;
            }

            public int pow(int x, long n) {
                if (n == 0) {
                    return 1;
                }
                long r = pow(x, n >> 1);
                r = modular.valueOf(r * r);
                if ((n & 1) == 1) {
                    r = modular.valueOf(r * x);
                }
                return (int) r;
            }

            public int inverse(int x) {
                return pow(x, modular.m - 2);
            }

            public int pow2(int x) {
                return x * x;
            }

            public long pow2(long x) {
                return x * x;
            }

            public double pow2(double x) {
                return x * x;
            }
        }

        /**
         * Log operations
         */
        public static class Log2 {
            public int ceilLog(int x) {
                return 32 - Integer.numberOfLeadingZeros(x - 1);
            }

            public int floorLog(int x) {
                return 31 - Integer.numberOfLeadingZeros(x);
            }

            public int ceilLog(long x) {
                return 64 - Long.numberOfLeadingZeros(x - 1);
            }

            public int floorLog(long x) {
                return 63 - Long.numberOfLeadingZeros(x);
            }
        }

        /**
         * Find all inverse number
         */
        public static class InverseNumber {
            int[] inv;

            public InverseNumber(int[] inv, int limit, Modular modular) {
                this.inv = inv;
                inv[1] = 1;
                int p = modular.m;
                for (int i = 2; i <= limit; i++) {
                    int k = p / i;
                    int r = p % i;
                    inv[i] = modular.mul(-k, inv[r]);
                }
            }

            public InverseNumber(int limit, Modular modular) {
                this(new int[limit + 1], limit, modular);
            }
        }

        /**
         * Factorial
         */
        public static class Factorial {
            int[] fact;
            int[] inv;

            public Factorial(int[] fact, int[] inv, InverseNumber in, int limit, Modular modular) {
                this.fact = fact;
                this.inv = inv;
                fact[0] = inv[0] = 1;
                for (int i = 1; i <= limit; i++) {
                    fact[i] = modular.mul(fact[i - 1], i);
                    inv[i] = modular.mul(inv[i - 1], in.inv[i]);
                }
            }

            public Factorial(int limit, Modular modular) {
                this(new int[limit + 1], new int[limit + 1], new InverseNumber(limit, modular), limit, modular);
            }
        }

        /**
         * Composition
         */
        public static class Composite {
            final Factorial factorial;
            final Modular modular;

            public Composite(Factorial factorial, Modular modular) {
                this.factorial = factorial;
                this.modular = modular;
            }

            public Composite(int limit, Modular modular) {
                this(new Factorial(limit, modular), modular);
            }

            public int composite(int m, int n) {
                if (n > m) {
                    return 0;
                }
                return modular.mul(modular.mul(factorial.fact[m], factorial.inv[n]), factorial.inv[m - n]);
            }
        }

        /**
         * Test whether a number is primes
         */
        public static class MillerRabin {
            Modular modular;
            Power power;

            /**
             * Check whether n is a prime s times
             */
            public boolean mr(int n, int s) {
                if (n == 2) {
                    return true;
                }
                if (n % 2 == 0) {
                    return false;
                }
                modular = new Modular(n);
                power = new Power(modular);
                for (int i = 0; i < s; i++) {
                    int x = RANDOM.nextInt(n - 2) + 2;
                    if (!mr0(x, n)) {
                        return false;
                    }
                }
                return true;
            }

            private boolean mr0(int x, int n) {
                int exp = n - 1;
                while (true) {
                    int y = power.pow(x, exp);
                    if (y != 1 && y != n - 1) {
                        return false;
                    }
                    if (y != 1 || exp % 2 == 1) {
                        break;
                    }
                    exp = exp / 2;
                }
                return true;
            }
        }
    }

    public static class FastIO {
        public final StringBuilder cache = new StringBuilder(1 << 13);
        private final InputStream is;
        private final OutputStream os;
        private final Charset charset;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastIO(InputStream is, OutputStream os, Charset charset) {
            this.is = is;
            this.os = os;
            this.charset = charset;
        }

        public FastIO(InputStream is, OutputStream os) {
            this(is, os, Charset.forName("ascii"));
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public long readLong() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            long val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public double readDouble() {
            boolean sign = true;
            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+';
                next = read();
            }

            long val = 0;
            while (next >= '0' && next <= '9') {
                val = val * 10 + next - '0';
                next = read();
            }
            if (next != '.') {
                return sign ? val : -val;
            }
            next = read();
            long radix = 1;
            long point = 0;
            while (next >= '0' && next <= '9') {
                point = point * 10 + next - '0';
                radix = radix * 10;
                next = read();
            }
            double result = val + (double) point / radix;
            return sign ? result : -result;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

        public int readLine(char[] data, int offset) {
            int originalOffset = offset;
            while (next != -1 && next != '\n') {
                data[offset++] = (char) next;
                next = read();
            }
            return offset - originalOffset;
        }

        public int readString(char[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (char) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public int readString(byte[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (byte) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public char readChar() {
            skipBlank();
            char c = (char) next;
            next = read();
            return c;
        }

        public void flush() throws IOException {
            os.write(cache.toString().getBytes(charset));
            os.flush();
            cache.setLength(0);
        }

        public boolean hasMore() {
            skipBlank();
            return next != -1;
        }
    }

    public static class Debug {
        private boolean allowDebug;

        public Debug(boolean allowDebug) {
            this.allowDebug = allowDebug;
        }

        public void assertTrue(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (!flag) {
                fail();
            }
        }

        public void fail() {
            throw new RuntimeException();
        }

        public void assertFalse(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (flag) {
                fail();
            }
        }

        private void outputName(String name) {
            System.out.print(name + " = ");
        }

        public void debug(String name, int x) {
            if (!allowDebug) {
                return;
            }

            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, long x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, double x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, int[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, long[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, double[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, Object x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, Object... x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.deepToString(x));
        }
    }
}


import java.util.Scanner;
public class cfa{
	static void solve(int a,int b){
	 int m=a;
	 if(b>m)
	 {m=b;b=a;}
	  int d=b*2;
	  if(d>=m)
	  {System.out.println((b+m)/3);return;}
     System.out.println((b+d)/3);
	}
	public static void main(String[] args)
	{
		int a,b,d,m;
		Scanner o=new Scanner(System.in);
		a=o.nextInt();
		b=o.nextInt();
		solve(a,b);
	}
}


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

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

    long MAX = 100000L, MOD = 1000000007L, INF = (long) 1e18;

    void solve(int TC) throws Exception {
    	int n = ni();
    	int[] a = new int[n];
    	for(int i=0;i<n;i++) a[i] = ni();
    	int C=0,S=0,R=0,newC=0,newS=0,newR=0;
    	for(int i=0;i<n;i++) {
    		newR = Math.max(R, Math.max(S, C));
    		newS = newR;
    		newC = newR;
    		if(a[i] == 2 || a[i] == 3) newS = Math.max(newS, Math.max(R, C) + 1);
    		if(a[i] == 1 || a[i] == 3) newC = Math.max(newC, Math.max(R, S) + 1);
    		S = newS;
    		C = newC;
    		R = newR;
    	}
    	pn(n - Math.max(Math.max(S, C), R));
    }

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

    long pow(long a, long b) {
        if(b==0 || a==1) return 1;
        long o = 1;
        for(long p = b; p > 0; p>>=1) {
            if((p&1)==1) o = (o*a) % MOD;
            a = (a*a) % MOD;
        } return o;
    }

    long inv(long x) {
        long o = 1;
        for(long p = MOD-2; p > 0; p>>=1) {
            if((p&1)==1)o = (o*x)%MOD;
            x = (x*x)%MOD;
        } return o;
    }
    long gcd(long a, long b) { return (b==0) ? a : gcd(b,a%b); }
    int gcd(int a, int b) { return (b==0) ? a : gcd(b,a%b); }
    void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
    static void dbg(Object... o){System.err.println(Arrays.deepToString(o));}

    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        int T = TestCases ? ni() : 1;
        for(int t=1;t<=T;t++) solve(t);
        out.flush();
        if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
    }

    void p(Object o) { out.print(o); }
    void pn(Object o) { out.println(o); }
    void pni(Object o) { out.println(o);out.flush(); }
    double PI = 3.141592653589793238462643383279502884197169399;

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

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

    double nd() { return Double.parseDouble(ns()); }
    char nc() { return (char)skip(); }

    int BUF_SIZE = 1024 * 8;
    byte[] inbuf = new byte[BUF_SIZE];
    int lenbuf = 0, ptrbuf = 0;

    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++];
    }

    boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

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

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

    void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class Main {

    void solve() throws IOException {
        long n = nl(), m = nl(), k = nl(), x = nl(), y = nl();

        if (n <= 2) {
            long p = k / (n * m);
            long q = k % (n * m);

            out.print((q == 0 ? p : p + 1) + " " + p + " ");
            out.println(q >= (x - 1) * m + y ? p + 1 : p);
        } else {
            long t = (n - 1) * m;
            long p = k / t;
            long q = k % t;

            long max = p;
            if (p == 0 && q > 0 || q > m) max++;
            out.print(max + " ");

            long min = p / 2;
            if (p % 2 == 1 && q >= m) min++;
            out.print(min + " ");

            if (x == 1) {
                long s = p / 2;
                if (q >= y) s++;
                out.println(s);
            } else if (x == n) {
                long s = p / 2;
                if (p % 2 == 1 && q >= y) s++;
                out.println(s);
            } else {
                long s = p;
                if (p % 2 == 0 && q >= (x - 1) * m + y
                        || p % 2 == 1 && q >= (n - x) * m + y) s++;
                out.println(s);
            }
        }
    }

    String ns() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine(), " ");
        }
        return tok.nextToken();
    }

    int ni() throws IOException {
        return Integer.parseInt(ns());
    }

    long nl() throws IOException {
        return Long.parseLong(ns());
    }

    double nd() throws IOException {
        return Double.parseDouble(ns());
    }

    String[] nsa(int n) throws IOException {
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = ns();
        }
        return res;
    }

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

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

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tok = new StringTokenizer("");
        Main main = new Main();
        main.solve();
        out.close();
    }
}


import java.util.ArrayList;
import java.util.Scanner;

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

        String p = sn.next();
        int w = 0;
        for(int i = 0; i < p.length(); i++)
            if(p.charAt(i) == '4'|| p.charAt(i) == '7')
                w++;

        String c = w+"";
        for(int i = 0; i < c.length(); i++)
            if(c.charAt(i) == '7' || c.charAt(i) == '4' ){
                System.out.println("YES");
                break;
            }
            else{
                System.out.println("NO");
                break;
            }

    }

}


import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;

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

class TaskD {
	static final int MOD = (int) (1e9 + 9);

    public void solve(int testNumber, InputReader in, OutputWriter out) {
		int count = in.readInt();
		int height = in.readInt();
		int[][][][] result = new int[height + 1][height + 1][height + 1][height + 1];
		result[0][0][0][0] = 1;
		int[][][][] next = new int[height + 1][height + 1][height + 1][height + 1];
		for (int i = 0; i < count; i++) {
			for (int j = 0; j <= height; j++) {
				for (int k = j; k <= height; k++)
					Arrays.fill(next[0][j][k], k, height + 1, 0);
			}
			next[height][height][height][height] = 0;
			for (int j = 1; j < height; j++) {
				next[j][height][height][height] = 0;
				for (int k = j; k < height; k++) {
					next[j][k][height][height] = 0;
					for (int l = k; l < height; l++)
						next[j][k][l][height] = 0;
				}
			}
			for (int j = 0; j <= height; j++) {
				int nj = Math.min(j + 1, height);
				for (int k = j; k <= height; k++) {
					int nk = Math.min(k + 1, height);
					for (int l = k; l <= height; l++) {
						int nl = Math.min(l + 1, height);
						int shift = 1;
						if (j != 0 && l != height)
							shift = height;
						int start = l;
						if (l != 0 && shift == height)
							start = height;
						for (int m = start; m <= height; m += shift) {
							if (result[j][k][l][m] == 0)
								continue;
							int nm = Math.min(m + 1, height);
							if (j == height) {
								next[j][nk][nl][nm] += result[j][k][l][m];
								if (next[j][nk][nl][nm] >= MOD)
									next[j][nk][nl][nm] -= MOD;
							} else {
//								int delta;
//								if (0 == nm)
//									delta = 1;
//								else if (nk == nm)
//									delta = 4;
//								else if (nl == nm)
//									delta = 12;
//								else
//									delta = 24;
								next[0][nk][nl][nm] += result[j][k][l][m];
								if (next[0][nk][nl][nm] >= MOD)
									next[0][nk][nl][nm] -= MOD;
							}
//							if (k != j)
							if (k == height) {
//								int delta;
//								if (nj == height)
//									delta = 1;
//								else
//									delta = 4;
								next[nj][k][nl][nm] += result[j][k][l][m];
								if (next[nj][k][nl][nm] >= MOD)
									next[nj][k][nl][nm] -= MOD;
							} else {
//								int delta;
//								if (0 == nm)
//									delta = 1;
//								else if (nj == nm)
//									delta = 4;
//								else if (nl == nm)
//									delta = 12;
//								else
//									delta = 24;
								next[0][nj][nl][nm] += result[j][k][l][m];
								if (next[0][nj][nl][nm] >= MOD)
									next[0][nj][nl][nm] -= MOD;
							}
//							if (l != k)
							if (l == height) {
//								int delta;
//								if (nj == height)
//									delta = 1;
//								else if (nk == height)
//									delta = 4;
//								else
//									delta = 12;
								next[nj][nk][l][nm] += result[j][k][l][m];
								if (next[nj][nk][l][nm] >= MOD)
									next[nj][nk][l][nm] -= MOD;
							} else {
//								int delta;
//								if (0 == nm)
//									delta = 1;
//								else if (nj == nm)
//									delta = 4;
//								else if (nk == nm)
//									delta = 12;
//								else
//									delta = 24;
								next[0][nj][nk][nm] += result[j][k][l][m];
								if (next[0][nj][nk][nm] >= MOD)
									next[0][nj][nk][nm] -= MOD;
							}
//							if (m != l)
							if (m == height) {
//								int delta;
//								if (nj == height)
//									delta = 1;
//								else if (nk == height)
//									delta = 4;
//								else if (nl == height)
//									delta = 12;
//								else
//									delta = 24;
								next[nj][nk][nl][m] += result[j][k][l][m];
								if (next[nj][nk][nl][m] >= MOD)
									next[nj][nk][nl][m] -= MOD;
							} else {
//								int delta;
//								if (0 == nl)
//									delta = 1;
//								else if (nj == nl)
//									delta = 4;
//								else if (nk == nl)
//									delta = 12;
//								else
//									delta = 24;
								next[0][nj][nk][nl] += result[j][k][l][m];
								if (next[0][nj][nk][nl] >= MOD)
									next[0][nj][nk][nl] -= MOD;
							}
						}
					}
				}
			}
			int[][][][] temp = result;
			result = next;
			next = temp;
		}
		long total = 0;
		for (int i = 0; i <= height; i++) {
			for (int j = i; j <= height; j++) {
				for (int k = j; k <= height; k++) {
					for (int l = k; l <= height; l++) {
						int delta;
						if (i == l)
							delta = 1;
						else if (j == l)
							delta = 4;
						else if (k == l)
							delta = 12;
						else
							delta = 24;
						total += result[i][j][k][l];
					}
				}
			}
		}
		total -= result[height][height][height][height];
		out.printLine(total % MOD);
    }
}

class InputReader {

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

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

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

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

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

class OutputWriter {
	private final PrintWriter writer;

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

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

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

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

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

	}


// practice with rainboy
import java.io.*;
import java.util.*;

public class CF14E extends PrintWriter {
	CF14E() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF14E o = new CF14E(); o.main(); o.flush();
	}

	void main() {
		int n = sc.nextInt();
		int t = sc.nextInt();
		long[][][][][] dp = new long[n][t + 1][t][4][4];
		for (int y0 = 0; y0 < 4; y0++)
			for (int y1 = 0; y1 < 4; y1++)
				if (y0 != y1)
					dp[1][0][0][y0][y1] = 1;
		for (int i = 1; i < n - 1; i++)
			for (int h = 0; h <= t; h++)
				for (int d = 0; d < t; d++)
					for (int y0 = 0; y0 < 4; y0++)
						for (int y1 = 0; y1 < 4; y1++) {
							long x = dp[i][h][d][y0][y1];
							if (x == 0)
								continue;
							for (int y2 = 0; y2 < 4; y2++) {
								if (y2 == y1)
									continue;
								if (y0 < y1 && y1 < y2 || y0 > y1 && y1 > y2) {
									dp[i + 1][h][d][y1][y2] += x;
								} else if (y0 < y1 && y1 > y2) {
									if (h + 1 <= t)
										dp[i + 1][h + 1][d][y1][y2] += x;
								} else if (y0 > y1 && y1 < y2) {
									if (d + 1 < t)
										dp[i + 1][h][d + 1][y1][y2] += x;
								}
							}
						}
		long ans = 0;
		for (int y0 = 0; y0 < 4; y0++)
			for (int y1 = 0; y1 < 4; y1++)
				ans += dp[n - 1][t][t - 1][y0][y1];
		println(ans);
	}
}


/*
 ID: tommatt1
 LANG: JAVA
 TASK:
*/
import java.util.*;
import java.io.*;
public class cf1237e{

	public static void main(String[] args)throws IOException {
		PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer(bf.readLine());
		int n=Integer.parseInt(st.nextToken());
		int x=1;
		while(x<=n) {
			if(n==x||n==x+1) {
				out.println(1);
				out.close();
				return;
			}
			if((x&1)==0) {
				x=2*x+1;
			}
			else {
				x=2*x+2;
			}
		}
		out.println(0);
		out.close();
	}

}


import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.abs;
import static java.lang.System.exit;
import static java.util.Arrays.sort;

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

public class B3 {

	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer tok;

	static void solve() throws Exception {
		while (nextInt() != 0) {
			int n = nextInt();
			final int pi[] = new int[n];
			final int pj[] = new int[n];
			for (int i = 0; i < n; i++) {
				pi[i] = nextInt();
				pj[i] = nextInt();
			}
			int startPoint = 0, startI = pi[0], startJ = pj[0];
			for (int i = 1; i < n; i++) {
				if (pi[i] < startI || (pi[i] == startI && pj[i] < startJ)) {
					startPoint = i;
					startI = pi[i];
					startJ = pj[i];
				}
			}
			Integer idx[] = new Integer[n - 1];
			for (int i = 0, j = 0; i < n; i++) {
				if (i != startPoint) {
					idx[j++] = i;
				}
			}
			final int fStartI = startI, fStartJ = startJ;
			sort(idx, new Comparator<Integer>() {
				public int compare(Integer o1, Integer o2) {
					int i1 = pi[o1] - fStartI, j1 = pj[o1] - fStartJ;
					int i2 = pi[o2] - fStartI, j2 = pj[o2] - fStartJ;
					int c = Long.compare((long) j1 * i2, (long) i1 * j2);
					if (c != 0) {
						return c;
					}
					return i1 + abs(j1) - (i2 + abs(j2));
				}
			});
			int hull[] = new int[n];
			int hullSize = 1;
			hull[0] = startPoint;
			for (int i: idx) {
				int ni = pi[i], nj = pj[i];
				while (hullSize >= 2) {
					int cur = hull[hullSize - 1];
					int ci = pi[cur], cj = pj[cur];
					int old = hull[hullSize - 2];
					int oi = pi[old], oj = pj[old];
					if ((long) (nj - cj) * (ci - oi) > (long) (ni - ci) * (cj - oj)) {
						break;
					}
					--hullSize;
				}
				hull[hullSize++] = i;
			}
			int ansX[] = new int[hullSize], ansY[] = new int[hullSize];
			int ansSize = 0;
			int start = 0, startX = pi[hull[0]], startY = pj[hull[0]];
			for (int i = 1; i < hullSize; i++) {
				int cx = pi[hull[i]], cy = pj[hull[i]];
				if (cx < startX || (cx == startX && cy < startY)) {
					start = i;
					startX = cx;
					startY = cy;
				}
			}
//			System.err.println(startX + " " + startY);
//			System.err.println(hullSize);
//			for (int i = 0; i < hullSize; i++) {
//				System.err.println((1 + pj[hull[i]]) + " " + (m - pi[hull[i]]));
//			}
			int q = 0;
			for (int i = start;;) {
//				System.err.println(i + " " + (1 + pj[hull[i]]) + " " + (m - pi[hull[i]]) + " " + q);
				ansX[ansSize] = pi[hull[i]] + ADDX[q];
				ansY[ansSize] = pj[hull[i]] + ADDY[q];
				if (ansSize == 0 || ansX[ansSize] != ansX[ansSize - 1] || ansY[ansSize] != ansY[ansSize - 1]) {
					++ansSize;
				}
				int j = i;
				i = (i + hullSize - 1) % hullSize;
				if (i == start) {
					break;
				}
				if (pi[hull[i]] == pi[hull[j]] || pj[hull[i]] == pj[hull[j]]) {
					++q;
				}
			}
			if (ansX[ansSize - 1] == ansX[0] && ansY[ansSize - 1] == ansY[0]) {
				--ansSize;
			}
			out.println(ansSize);
			for (int i = 0; i < ansSize; i++) {
				out.println(ansX[i] + " " + ansY[i]);
			}
		}
	}

	static final int ADDX[] = {0, 0, -1, -1, 0};
	static final int ADDY[] = {0, -1, -1, 0, 0};

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

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

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

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

	public static void main(String[] args) {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(new OutputStreamWriter(System.out));
			solve();
			in.close();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			exit(1);
		}
	}
}


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

public class Solution implements Runnable {

	static final long MODULO = 1000000007;

	void solve() throws Exception {
		String from = nextToken(), to = nextToken();
		int operations = nextInt();
		int n = from.length();
		boolean[] isGoodStart = new boolean[n];
		for (int i = 0; i < n; ++i) {
			String cut = from.substring(i) + from.substring(0, i);
			isGoodStart[i] = cut.equals(to);
		}
		long goodCount = 0;
		for (boolean a : isGoodStart) {
			if (a) {
				++goodCount;
			}
		}
		long badCount = n - goodCount;
		long[][] ways = new long[2][2];
		int cur = 0, ne = 1;
		if (isGoodStart[0]) {
			++ways[cur][1];
		} else {
			++ways[cur][0];
		}
		for (int op = 0; op < operations; ++op) {
			Arrays.fill(ways[ne], 0);
			ways[ne][0] = (ways[cur][0] * (badCount - 1)) % MODULO + (ways[cur][1] * badCount) % MODULO;
			ways[ne][0] %= MODULO;
			ways[ne][1] = (ways[cur][1] * (goodCount - 1)) % MODULO + (ways[cur][0] * goodCount) % MODULO;
			ways[ne][1] %= MODULO;
			cur ^= 1;
			ne ^= 1;
		}
		out.print(ways[cur][1]);
	}

	public void run() {
		try {
			solve();
		} catch (Exception e) {
			NOO(e);
		} finally {
			out.close();
		}
	}

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

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

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

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

	BufferedReader in;
	PrintWriter out;
	StringTokenizer stringTokenizer;

	void NOO(Exception e) {
		e.printStackTrace();
		System.exit(42);
	}

	public Solution(String name) {
		try {
			in = new BufferedReader(new FileReader("input.txt"));
			out = new PrintWriter(new FileWriter("output.txt"));
			stringTokenizer = new StringTokenizer("");
		} catch (Exception e) {
			NOO(e);
		}
	}

	public Solution() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			stringTokenizer = new StringTokenizer("");
		} catch (Exception e) {
			NOO(e);
		}
	}

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


//package ;
import java.io.*;
import java.util.*;

public class D
{
	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner();
		PrintWriter pw = new PrintWriter(System.out);
		int n=sc.nextInt();
		HashSet<Long>hs=new HashSet<>();
		if(n<=13)
		{	for(int i=0;i<=n;i++)
				for(int j=0;j+i<=n;j++)
					for(int k=0;k+j+i<=n;k++)
						for(int z=0;z+k+j+i<=n;z++)
							if(z+k+j+i==n)
								hs.add((long)z*1 + (long)k*5 +(long)j*10 + (long)i*50);
			System.out.println(hs.size());
		}
		else
		{
			long m=(long)(n-13)*49;
			System.out.println(390+m);
		}
		pw.close();
	}
	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

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

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

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

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

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

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

	}
}


import java.util.*;

public class caps {
	public static void main(String[] args)
	{
	Scanner in= new Scanner( System.in);
	String a= in.next();
	int state =0;
	for(int i = 0; i<a.length();i++)
	{
		if(state==0&&a.charAt(i)=='h')
		{
			state=1;
		}
		if(state ==1 && a.charAt(i)=='e')
		{
			state=2;
		}
		if(state ==2 && a.charAt(i)=='l')
		{
			state=3;
		}
		else if(state ==3 && a.charAt(i)=='l')
		{
			state=4;
		}
		if(state ==4 && a.charAt(i)=='o')
		{
			state=5;
			break;
		}

	}
	if(state >= 5)
	{
		System.out.println("YES");
	}
	else
	{
		System.out.println("NO");
	}
	}
}


import java.util.Scanner;
import java.io.IOException;

public class IOI{
	public static void main(String args[]){
		int a,b,c,d,e,n,m,k;
		Scanner in=new Scanner(System.in);
		n=in.nextInt(); m=in.nextInt();
		k=in.nextInt();
		e=0;
		d=0;
		int ar[]=new int[10000];
		for(a=0;;a++){
			d++;
			if(((n*m)%2)==1) ar[d]=(n*m)/2 +1 ;
			else ar[d]=(n*m)/2;
			n=n-2;
			m=m-2;
			if(d==k+1) break;
			if(n<=0 || m<=0) break;
		}
		if(k>d) e=0;
		else e=ar[k]-ar[k+1];
		System.out.println(e+"");
	}
}


//package tes7;
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 P15 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		int n = ni();
		int mod = 1000000009;
		long[] dp = new long[n+1];
		if(5 <= n)dp[5] = 5;
		for(int i = 7;i <= n;i+=2){
			dp[i] = ((dp[i-2] + 1) * 2L + 1) % mod;
		}

		long ret = 1;
		for(int i = n;i >= 3;i--){
			if(dp[i] == 0){
				ret = ret + 2;
				if(ret >= mod)ret -= mod;
			}else{
				ret = ((ret+1)*dp[i]+1) % mod;
			}
		}
		ret = ((ret+1)*(ret+1)+1)%mod;
		out.println(ret*2%mod);
	}

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

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

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

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


import java.util.Scanner;
public class A {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		String result = "";
		String binary = Integer.toBinaryString(n);
		// System.out.println(binary);
		int power = binary.length();
		for (int x = 0; x < binary.length(); ++x) {
			if (binary.charAt(x) == '1') {
				result += power + " ";
			}
			power--;
		}
		System.out.println(result.substring(0,result.length()-1));
	}
}