import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            GXMouseInTheCampus solver = new GXMouseInTheCampus();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class GXMouseInTheCampus {
        LongHashMap mu = new LongHashMap(100000, false);
        LongHashMap euler = new LongHashMap(100000, false);
        long[] primes;

        {
            mu.put(1, 1);
            euler.put(1, 1);
        }

        public long oneOfPrimeFactor(long x) {
            for (long p : primes) {
                if (x % p == 0) {
                    return p;
                }
            }
            return x;
        }

        public void populate(long x) {
            long factor = oneOfPrimeFactor(x);
            long cnt = 0;
            long y = x;
            while (y % factor == 0) {
                cnt++;
                y /= factor;
            }
            if (cnt > 1) {
                mu.put(x, 0);
            } else {
                mu.put(x, -mu(y));
            }
            euler.put(x, euler(y) * (x / y - x / y / factor));
        }

        public long mu(long x) {
            long ans = mu.getOrDefault(x, -1);
            if (ans == -1) {
                populate(x);
                ans = mu.get(x);
            }
            return ans;
        }

        public long euler(long x) {
            long ans = euler.getOrDefault(x, -1);
            if (ans == -1) {
                populate(x);
                ans = euler.get(x);
            }
            return ans;
        }

        public void solve(int testNumber, FastInput in, FastOutput out) {
            long m = in.readLong();
            long x = in.readLong();

            primes = Factorization.factorizeNumberPrime(m).toArray();
            LongList allFactorOfM = new LongList(20000);
            LongList tmpList = new LongList(20000);
            LongList allPossiblePrimeFactor = new LongList();
            for (long p : primes) {
                allPossiblePrimeFactor.add(p);
                allPossiblePrimeFactor.addAll(Factorization.factorizeNumberPrime(p - 1));
            }

            collect(allFactorOfM, m, 0);
            LongPower power = new LongPower(ILongModular.getInstance(m));

            long total = 1;
            for (int i = 0; i < allFactorOfM.size(); i++) {
                long g = allFactorOfM.get(i);
                if (g == m) {
                    continue;
                }
                long mg = m / g;
                tmpList.clear();
                collect(tmpList, mg, 0);
                long cnt = 0;
                for (int j = tmpList.size() - 1; j >= 0; j--) {
                    long t = tmpList.get(j);
                    cnt += mu(t) * ((m - 1) / (t * g));
                }

                tmpList.clear();
                long euler = euler(mg);
                LongList primeFactors = new LongList(12);
                for (int j = 0; j < allPossiblePrimeFactor.size(); j++) {
                    long p = allPossiblePrimeFactor.get(j);
                    if (euler % p == 0) {
                        primeFactors.add(p);
                    }
                }

                long n = euler;
                for (int j = 0; j < primeFactors.size(); j++) {
                    long p = primeFactors.get(j);
                    while (n % p == 0 && power.pow(x, n / p) % mg == 1) {
                        n /= p;
                    }
                }

                if (cnt % n != 0) {
                    throw new IllegalStateException();
                }
                total += cnt / n;
            }

            out.println(total);
        }

        public void collect(LongList list, long x, int i) {
            if (i == primes.length) {
                list.add(x);
                return;
            }
            collect(list, x, i + 1);
            while (x % primes[i] == 0) {
                x /= primes[i];
                collect(list, x, i + 1);
            }
        }

    }

    static class DigitUtils {
        private DigitUtils() {
        }

        public static long round(double x) {
            if (x >= 0) {
                return (long) (x + 0.5);
            } else {
                return (long) (x - 0.5);
            }
        }

        public static long mod(long x, long mod) {
            x %= mod;
            if (x < 0) {
                x += mod;
            }
            return x;
        }

        public static long mulMod(long a, long b, long mod) {
            long k = DigitUtils.round((double) a / mod * b);
            return DigitUtils.mod(a * b - k * mod, mod);
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private StringBuilder cache = new StringBuilder(10 << 20);
        private final Writer os;

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            return this;
        }

        public FastOutput println(long c) {
            cache.append(c);
            println();
            return this;
        }

        public FastOutput println() {
            cache.append(System.lineSeparator());
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static class LongPower {
        final ILongModular modular;

        public LongPower(ILongModular modular) {
            this.modular = modular;
        }

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

    }

    static class Factorization {
        public static LongList factorizeNumberPrime(long x) {
            LongList ans = new LongList();
            for (long i = 2; i * i <= x; i++) {
                if (x % i != 0) {
                    continue;
                }
                ans.add(i);
                while (x % i == 0) {
                    x /= i;
                }
            }
            if (x > 1) {
                ans.add(x);
            }
            return ans;
        }

    }

    static class LongHashMap {
        private int[] slot;
        private int[] next;
        private long[] keys;
        private long[] values;
        private int alloc;
        private boolean[] removed;
        private int mask;
        private int size;
        private boolean rehash;

        public LongHashMap(int cap, boolean rehash) {
            this.mask = (1 << (32 - Integer.numberOfLeadingZeros(cap - 1))) - 1;
            slot = new int[mask + 1];
            next = new int[cap + 1];
            keys = new long[cap + 1];
            values = new long[cap + 1];
            removed = new boolean[cap + 1];
            this.rehash = rehash;
        }

        private void doubleCapacity() {
            int newSize = Math.max(next.length + 10, next.length * 2);
            next = Arrays.copyOf(next, newSize);
            keys = Arrays.copyOf(keys, newSize);
            values = Arrays.copyOf(values, newSize);
            removed = Arrays.copyOf(removed, newSize);
        }

        public void alloc() {
            alloc++;
            if (alloc >= next.length) {
                doubleCapacity();
            }
            next[alloc] = 0;
            removed[alloc] = false;
            size++;
        }

        private void rehash() {
            int[] newSlots = new int[Math.max(16, slot.length * 2)];
            int newMask = newSlots.length - 1;
            for (int i = 0; i < slot.length; i++) {
                if (slot[i] == 0) {
                    continue;
                }
                int head = slot[i];
                while (head != 0) {
                    int n = next[head];
                    int s = hash(keys[head]) & newMask;
                    next[head] = newSlots[s];
                    newSlots[s] = head;
                    head = n;
                }
            }
            this.slot = newSlots;
            this.mask = newMask;
        }

        private int hash(long x) {
            int h = Long.hashCode(x);
            return h ^ (h >>> 16);
        }

        public void put(long x, long y) {
            put(x, y, true);
        }

        public void put(long x, long y, boolean cover) {
            int h = hash(x);
            int s = h & mask;
            if (slot[s] == 0) {
                alloc();
                slot[s] = alloc;
                keys[alloc] = x;
                values[alloc] = y;
            } else {
                int index = findIndexOrLastEntry(s, x);
                if (keys[index] != x) {
                    alloc();
                    next[index] = alloc;
                    keys[alloc] = x;
                    values[alloc] = y;
                } else if (cover) {
                    values[index] = y;
                }
            }
            if (rehash && size >= slot.length) {
                rehash();
            }
        }

        public long getOrDefault(long x, long def) {
            int h = hash(x);
            int s = h & mask;
            if (slot[s] == 0) {
                return def;
            }
            int index = findIndexOrLastEntry(s, x);
            return keys[index] == x ? values[index] : def;
        }

        public long get(long x) {
            return getOrDefault(x, 0);
        }

        private int findIndexOrLastEntry(int s, long x) {
            int iter = slot[s];
            while (keys[iter] != x) {
                if (next[iter] != 0) {
                    iter = next[iter];
                } else {
                    return iter;
                }
            }
            return iter;
        }

        public LongEntryIterator iterator() {
            return new LongEntryIterator() {
                int index = 1;
                int readIndex = -1;

                public boolean hasNext() {
                    while (index <= alloc && removed[index]) {
                        index++;
                    }
                    return index <= alloc;
                }

                public long getEntryKey() {
                    return keys[readIndex];
                }

                public long getEntryValue() {
                    return values[readIndex];
                }

                public void next() {
                    if (!hasNext()) {
                        throw new IllegalStateException();
                    }
                    readIndex = index;
                    index++;
                }
            };
        }

        public String toString() {
            LongEntryIterator iterator = iterator();
            StringBuilder builder = new StringBuilder("{");
            while (iterator.hasNext()) {
                iterator.next();
                builder.append(iterator.getEntryKey()).append("->").append(iterator.getEntryValue()).append(',');
            }
            if (builder.charAt(builder.length() - 1) == ',') {
                builder.setLength(builder.length() - 1);
            }
            builder.append('}');
            return builder.toString();
        }

    }

    static class SequenceUtils {
        public static boolean equal(long[] a, int al, int ar, long[] b, int bl, int br) {
            if ((ar - al) != (br - bl)) {
                return false;
            }
            for (int i = al, j = bl; i <= ar; i++, j++) {
                if (a[i] != b[j]) {
                    return false;
                }
            }
            return true;
        }

    }

    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

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

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

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

    }

    static interface LongEntryIterator {
        boolean hasNext();

        void next();

        long getEntryKey();

        long getEntryValue();

    }

    static class LongModular implements ILongModular {
        final long m;

        public LongModular(long m) {
            this.m = m;
        }

        public long mul(long a, long b) {
            return b == 0 ? 0 : ((mul(a, b >> 1) << 1) % m + a * (b & 1)) % m;
        }

    }

    static class LongModularDanger implements ILongModular {
        final long m;

        public LongModularDanger(long m) {
            this.m = m;
        }

        public long mul(long a, long b) {
            return DigitUtils.mulMod(a, b, m);
        }

    }

    static class LongList implements Cloneable {
        private int size;
        private int cap;
        private long[] data;
        private static final long[] EMPTY = new long[0];

        public LongList(int cap) {
            this.cap = cap;
            if (cap == 0) {
                data = EMPTY;
            } else {
                data = new long[cap];
            }
        }

        public LongList(LongList list) {
            this.size = list.size;
            this.cap = list.cap;
            this.data = Arrays.copyOf(list.data, size);
        }

        public LongList() {
            this(0);
        }

        public void ensureSpace(int req) {
            if (req > cap) {
                while (cap < req) {
                    cap = Math.max(cap + 10, 2 * cap);
                }
                data = Arrays.copyOf(data, cap);
            }
        }

        private void checkRange(int i) {
            if (i < 0 || i >= size) {
                throw new ArrayIndexOutOfBoundsException();
            }
        }

        public long get(int i) {
            checkRange(i);
            return data[i];
        }

        public void add(long x) {
            ensureSpace(size + 1);
            data[size++] = x;
        }

        public void addAll(long[] x, int offset, int len) {
            ensureSpace(size + len);
            System.arraycopy(x, offset, data, size, len);
            size += len;
        }

        public void addAll(LongList list) {
            addAll(list.data, 0, list.size);
        }

        public int size() {
            return size;
        }

        public long[] toArray() {
            return Arrays.copyOf(data, size);
        }

        public void clear() {
            size = 0;
        }

        public String toString() {
            return Arrays.toString(toArray());
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof LongList)) {
                return false;
            }
            LongList other = (LongList) obj;
            return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);
        }

        public int hashCode() {
            int h = 1;
            for (int i = 0; i < size; i++) {
                h = h * 31 + Long.hashCode(data[i]);
            }
            return h;
        }

        public LongList clone() {
            LongList ans = new LongList();
            ans.addAll(this);
            return ans;
        }

    }

    static interface ILongModular {
        long mul(long a, long b);

        static ILongModular getInstance(long mod) {
            //return new LongModularDanger(mod);
            return mod <= (1L << 54) ? new LongModularDanger(mod) : new LongModular(mod);
        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.util.TreeMap;
public class E1113
{
    static class QN{
        int index;
        long sum;
        QN(int in,long an){
            index=in;
            sum=an;
        }
    }
    static class SegmentTree{
        long sum,pmin,lazy;
        int st,en;
        boolean rem;
        SegmentTree left,right;
        SegmentTree(int _st,int _en,long _v){
            st=_st;
            en=_en;
            lazy=_v;
            rem=true;
            sum=(en-st+1)*lazy;
            pmin=Math.min(lazy,sum);
        }
        long pmin(){
            return rem?(Math.min(lazy,(en-st+1)*lazy)):(pmin);
        }
        long sum(){
            return rem?((en-st+1)*lazy):(sum);
        }
        void update(int l,int r,long v){
            //completely outside
            if(l>en||r<st)
                return;
            //completely inside
            if(l<=st&&r>=en){
                //update lazy;
                lazy=v;
                rem=true;
                return;
            }
            //partial cover
            lazyPropagate();
            left.update(l, r, v);
            right.update(l, r, v);
            sum=left.sum()+right.sum();
            pmin=Math.min(left.pmin(),left.sum()+right.pmin());
            return;
        }
        void lazyPropagate(){
            if(left==null){
                int mid=(st+en)/2;
                left=new SegmentTree(st,mid,lazy);
                right=new SegmentTree(mid+1,en,lazy);
            }else if(rem){
                left.lazy=lazy;
                right.lazy=lazy;
                left.rem=true;
                right.rem=true;
            }
            rem=false;
        }
        /**returns least index which has value <= given value**/
        QN query(int l,int r,int v,long su){
            //no cover
            if(st>r||en<l)
                return new QN(-1,0);
            //full cover
            if(l<=st&&en<=r){
                if(su+pmin()<=v)
                    return query2(v,su);
                return new QN(-1,sum());
            }
            lazyPropagate();
            sum=left.sum()+right.sum();
            pmin=Math.min(left.pmin(),left.sum()+right.pmin());
            QN q1=left.query(l,r,v,su);
            if(q1.index!=-1)
                return q1;
            q1=right.query(l,r,v,su+q1.sum);
            return q1;
        }
        QN query2(long v,long su){
            if(st==en)
                return new QN(st,su+sum());
            lazyPropagate();
            sum=left.sum()+right.sum();
            pmin=Math.min(left.pmin(),left.sum()+right.pmin());
            if(left.pmin()+su<=v)
                return left.query2(v,su);
            else
                return right.query2(v,su+left.sum());

        }

    }
    static class Scanner
    {
        BufferedReader br;
        StringTokenizer tk=new StringTokenizer("");
        public Scanner(InputStream is)
        {
            br=new BufferedReader(new InputStreamReader(is));
        }
        public int nextInt() throws IOException
        {
            if(tk.hasMoreTokens())
                return Integer.parseInt(tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return nextInt();
        }
        public long nextLong() throws IOException
        {
            if(tk.hasMoreTokens())
                return Long.parseLong(tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return nextLong();
        }
        public String next() throws IOException
        {
            if(tk.hasMoreTokens())
                return (tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return next();
        }
        public String nextLine() throws IOException
        {
            tk=new StringTokenizer("");
            return br.readLine();
        }
        public double nextDouble() throws IOException
        {
            if(tk.hasMoreTokens())
                return Double.parseDouble(tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return nextDouble();
        }
        public char nextChar() throws IOException
        {
            if(tk.hasMoreTokens())
                return (tk.nextToken().charAt(0));
            tk=new StringTokenizer(br.readLine());
            return nextChar();
        }
        public int[] nextIntArray(int n) throws IOException
        {
            int a[]=new int[n];
            for(int i=0;i<n;i++)
                a[i]=nextInt();
            return a;
        }
        public long[] nextLongArray(int n) throws IOException
        {
            long a[]=new long[n];
            for(int i=0;i<n;i++)
                a[i]=nextLong();
            return a;
        }
        public int[] nextIntArrayOneBased(int n) throws IOException
        {
            int a[]=new int[n+1];
            for(int i=1;i<=n;i++)
                a[i]=nextInt();
            return a;
        }
        public long[] nextLongArrayOneBased(int n) throws IOException
        {
            long a[]=new long[n+1];
            for(int i=1;i<=n;i++)
                a[i]=nextLong();
            return a;
        }

    }

    public static void main(String args[]) throws IOException
    {
        Scanner in=new Scanner(System.in);
        PrintWriter out=new PrintWriter(System.out);
        SegmentTree st=new SegmentTree(1,1000000000,0);
        int q=in.nextInt();
        TreeMap<Integer,Integer> mp=new TreeMap<>();
        mp.put(0,0);
        mp.put(1000000001,0);
        while(q--!=0){
            int tp=in.nextInt();
            switch(tp){
                case 1:
                    int tm=in.nextInt();
                    int vl=in.nextInt();
                    int nxt=mp.higherKey(tm)-1;
                    st.update(tm, nxt, vl);
                    mp.put(tm,vl);
                    break;
                case 2:
                    int tc=in.nextInt();
                    int vp=mp.lowerEntry(tc).getValue();
                    int net=mp.higherKey(tc)-1;
                    st.update(tc, net, vp);
                    mp.remove(tc);
                    break;
                case 3:
                    int l=in.nextInt();
                    int r=in.nextInt();
                    int v=in.nextInt();
                    if(v==0){
                        out.println(l);
                        continue;
                    }
                    if(l==r){
                        out.println(-1);
                        continue;
                    }
                    QN aa=st.query(l, r-1, -v, 0);
                    if(aa.index==-1)
                        out.println(-1);
                    else{
                        int spd=mp.floorEntry(aa.index).getValue();
                        double remv=-(v+(aa.sum-spd));
                        double anss=aa.index;
                        anss+=remv/spd;
                        out.println(anss);

                    }

            }

        }
        out.close();
    }

}


import java.util.Scanner;
public class Solution {

    public static void main(String[] args) {
        long b=0;long p=1;
        Scanner s=new Scanner(System.in);
        long m=s.nextLong();
        int x=1;
        do{
            p=(m+b)/x;
            b=10*b+10;
            x++;
        }while(p/(int)Math.pow(10, x-1)!=0);
        x--;b=b/10-1;
        b=x*p-b;
        b=m-b;
        String ok=Long.toString(p);
        System.out.println(ok.charAt((int)b));
    }
}


import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class template {
	public static void main(String[] args) throws Exception {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		int n =sc.nextInt();
		int[] o = new int[4*n+1];
		int[] p= new int[4*n+1];
		int[] r= new int[51];
		int[] d= new int[51];
		for(int i=0;i<4*n+1;i++) {
			int x=  sc.nextInt();
			int y= sc.nextInt();
			o[i]=x;
			p[i]=y;
			r[x]++;
			d[y]++;
		}
		int sx =-1;
		int ex =-1;
		int sy =-1;
		int ey =-1;
		for(int i=0;i<r.length;i++) {
			if(r[i]>=n+1) {
				if(sx==-1) {
					sx=i;
				}
				else ex =i;
			}
			if(d[i]>=n+1) {
				if(sy==-1) {
					sy=i;
				}
				else ey =i;
			}
		}
		boolean[][] x = new boolean[51][51];
		for(int i=sx;i<=ex;i++) {
			x[i][sy]=true;
			x[i][ey]=true;
		}
		for(int i=sy;i<=ey;i++) {
			x[sx][i]=true;
			x[ex][i]=true;
		}
		for(int i=0;i<4*n+1;i++) {
			if(!x[o[i]][p[i]]) {
				pw.println(o[i]+" "+p[i]);
				break;
			}
		}
		pw.close();
	}
}
@SuppressWarnings("all")
class FastScanner {
    BufferedReader br;
    StringTokenizer st;
    public FastScanner(BufferedReader d) {
        br=d;
    }
    public FastScanner(String s) {
        try {
            br = new BufferedReader(new FileReader(s));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

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

    String nextToken() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

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

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

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


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

public class Solution implements Runnable{
  static long mod = (long) 1e9 + 7;

  static long solve(int s, int steps, long dp[][], boolean vis[][]) {
    if (steps == 0) {
      if (s == 1)
        return 1;

      return 0;
    }
    if (vis[s][steps])
      return dp[s][steps];

    vis[s][steps] = true;
    long ans = 0;
    if (s == 1)
      ans = (3 * solve(1 - s, steps - 1,dp,vis)) % mod;
    else
      ans = (2 * solve(0, steps - 1,dp,vis) + solve(1, steps - 1,dp,vis)) % mod;

    return dp[s][steps] = ans;

  }
  public void run()
  {
      try{
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(bf.readLine());
    long dp[][] = new long[2][n + 1];
    boolean vis[][] = new boolean[2][n + 1];
    System.out.println(solve(1, n, dp, vis));}
    catch(Exception e)
    {

    }
  }
  public static void main(String[] ks) throws Exception {
    new Thread(null, new Solution(), "Solution", 1<<28).start();

  }
}


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int min=0;
        int max=0;
        int n=sc.nextInt();
        int m=sc.nextInt();

        if(n==0)
            System.out.println("Impossible");
        else{
        if(m==0){
            max=n;
            min=n;

            }
        if(m>=n){

            min=m;
            max=m+n-1;
            }
            else
            if(m!=0)
            {
            min=n;
            max=m+n-1;
            }
         System.out.println(min+" "+max);
        }

        }
    }


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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String a[] = in.readLine().split(" ");
        int adultos = Integer.valueOf(a[0]), ninno = Integer.valueOf(a[1]);
        if (ad > 0 || ni==0) {
            System.out.print((ninno > adultos ? ninno : adultos) + " ");
            System.out.println(ninno > 0 ? adultos + ninno - 1 : adultos);
        } else {
            System.out.println("Impossible");
        }
    }
}


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

public class B{
	static class FastReader {
	    BufferedReader br;
	    StringTokenizer st;

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

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

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

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

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

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

	public static void main(String[] args){
		FastReader in = new FastReader();
		// int t = in.nextInt();
		// while(t-- != 0){
			int n = in.nextInt(), v = in.nextInt();
			long res = Math.min(v, n - 1);

			int d = (n - 1) - v;
			int cnt = 2;
			while(d-- != 0){
				res += cnt;
				cnt++;
			}
			System.out.println(res);

			// long res = 0;
			// for(int i = 0; i < n; i++){
			// 	int a = in.nextInt();
			// 	if(a == 0) zero++;
			// 	if(a >= 0){
			// 		res += Math.abs(a - 1);
			// 	}else{
			// 		res += Math.abs((-1) - a);
			// 		neg++;
			// 	}
			// }

		// }
	}

}


import java.util.*;
public class d {
public static void main(String[] args) {
	Scanner scan= new Scanner (System.in);

	int n=scan.nextInt();

	System.out.println(calc (n));
}

	static int calc ( int n )
	{
		if(n==0)
		{
			return 1;
		}
		if(n==1)
		{
			return 4;
		}
		if(n%2==0)
		{
		return calc (n-1) + 4;
		}
		return calc(n-1)*2;
		}
}


import java.util.*;

public class helloWorld
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		boolean p = false;
		int x = 0, y = 0;
		int a, b, ans = -1;

		for(int i = 0; i < n; i++) {
			x += a = in.nextInt();
			y += b = in.nextInt();
			if( (a+b) % 2 == 0)
				p = true;
		}

		if(x % 2 == 0 && y % 2 == 0)
			ans = 0;
		if(x % 2 == 1 && y % 2 == 1 && p && n > 1)
			ans  = 1;

		System.out.println(ans);

		in.close();
	}
}


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

public class mahakal
{
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
        int n,cnt=0,usum=0,lsum=0,odd=0;
        n=sc.nextInt();
        int u,l;
        for(int i=0;i<n;i++)
        {
            u=sc.nextInt();
            l=sc.nextInt();

             usum=usum+u;
             lsum=lsum+l;
             if(u==l)
             {

                odd++;
            }
         }
            if(n==1 && usum%2==1)
            {
                System.out.println("-1");
                return;
            }

            if(usum%2==0 && lsum%2==0)
            {
                System.out.print("0");
            }
            else if(odd<n && usum%2==1 && lsum%2==1)
            {
                System.out.print("1");
            }
            else
            {
                System.out.print("-1");
            }

}
}


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

public class solution {

    static boolean IS_DEBAG = false;
    static int maxn = 17321;
    static int block = 1000000;

    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(new File("input.txt"));
        PrintWriter out = new PrintWriter("output.txt");
        long l = in.nextLong();
        long r = in.nextLong();

        boolean nprime[] = new boolean[maxn];
        boolean bl[] = new boolean[block];
        int primes[] = new int[maxn];
        int cnt = 0;

        int nsq = (int) Math.sqrt(r);

        for (int i = 2; i <= nsq; ++i) {
            if (!nprime[i]) {
                primes[cnt++] = i;
                for (int j = i + i; j <= nsq; j += i) {
                    nprime[j] = true;
                }
            }
        }

        int ans = 0;

        for (int k = 0; k <= r / block; ++k) {
            Arrays.fill(bl, false);
            int start = k * block;
            for (int i = 0; i < cnt; ++i) {
                for (int j = Math.max((start + primes[i] - 1) / primes[i], 2) * primes[i] - start; j < block; j += primes[i]) {
                    bl[j] = true;
                }
            }
            if (k == 0) {
                bl[0] = bl[1] = true;
            }
            for (int i = 0; i < block && start + i <= r; ++i) {
                if (!bl[i]) {
                    if (((start + i - 1) % 4 == 0) && (start + i >= l) && (start + i <= r)) {
                        ans++;
                    }
                }
            }
        }

        if (2 >= l && 2 <= r) {
            ans++;
        }

        out.println(ans);
        out.close();
    }
}


import java.io.*;
import java.util.*;
public class CF939_D2_F {
	public static void main(String[] args)throws Throwable {
		MyScanner sc=new MyScanner();
		PrintWriter pw=new PrintWriter(System.out);

		int n=sc.nextInt()*2;
		int k=sc.nextInt();
		int [] l=new int [k+1];
		int [] r=new int [k+1];
		SparseTable [][] seg=new SparseTable [k+1][2];
		for(int i=0;i<k;i++){
			l[i]=sc.nextInt();
			r[i]=sc.nextInt();
		}
		l[k]=n;
		for(int j=0;j<2;j++){
			int [] a=new int [n+2];
			Arrays.fill(a, inf);
			a[n/2]=0;
			seg[k][j]=new SparseTable(a);
		}
		for(int i=k-1;i>=0;i--){
			for(int j=0;j<2;j++){
				int [] a=new int [n+2];
				Arrays.fill(a, inf);
				for(int c=0;c<=l[i];c++){
					int best=seg[i+1][j].query(c+l[i+1]-l[i], c+l[i+1]-l[i]);
					best=Math.min(best, 1+seg[i+1][1-j].query(l[i+1]-(c+r[i]-l[i]), l[i+1]-(c)));
					if(r[i]>l[i])
						best=Math.min(best, 2+seg[i+1][j].query(c+1, c+r[i]-l[i]));
					a[c]=best;
				}
				seg[i][j]=new SparseTable(a);
			}
		}
		int ans=Math.min(seg[0][0].query(l[0], l[0]), seg[0][1].query(l[0], l[0]));
		if(ans>2*k)
			pw.println("Hungry");
		else{
			pw.println("Full");
			pw.println(ans);
		}
		pw.flush();
		pw.close();
	}

	static int inf=(int)1e7;

	static class SparseTable {
		int [] a;
		int [][] st;
		//st[i][j] --> minimum of sub array starting at index i and of length 2^j
		SparseTable(int [] a){
			int n=a.length;
			this.a=a;
			int k=(int)Math.floor(Math.log(n)/Math.log(2))+1;
			st=new int [n][k];
			for(int i=0;i<n;i++)
				st[i][0]=i;
			//(1<<j)===(2^j)
			for(int j=1;(1<<j)<=n;j++)
				for(int i=0;i+(1<<j)-1<n;i++)
					if(a[st[i][j-1]] < a[st[i+(1<<(j-1))][j-1]])
						st[i][j]=st[i][j-1]; //
					else
						st[i][j]=st[i+(1<<(j-1))][j-1];
		}
		int query(int i,int j){
			int k=(int)Math.floor(Math.log(j-i+1)/Math.log(2));
			return Math.min(a[st[i][k]], a[st[j-(1<<k)+1][k]]);
		}
	}

	static class MyScanner {
		BufferedReader br;
		StringTokenizer st;
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {while (st == null || !st.hasMoreElements()) {
			try {st = new StringTokenizer(br.readLine());}
			catch (IOException e) {e.printStackTrace();}}
		return st.nextToken();}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());}
		double nextDouble() {return Double.parseDouble(next());}
		String nextLine(){String str = "";
		try {str = br.readLine();}
		catch (IOException e) {e.printStackTrace();}
		return str;}
	}
}


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

/**
 * Created by dalt on 2018/3/14.
 */
public class CF939F {
    public static final int INF = (int) 1e8;
    public static BlockReader input;
    public static PrintStream output;

    public static void main(String[] args) throws FileNotFoundException {
        if (System.getProperty("ONLINE_JUDGE") == null) {
            input = new BlockReader(new FileInputStream("D:\\DataBase\\TESTCASE\\codeforces\\CF939F.in"));
            output = System.out;
        } else {
            input = new BlockReader(System.in);
            output = new PrintStream(new BufferedOutputStream(System.out), false);
        }

        solve();

        output.flush();
    }

    public static void solve() {
        int n = input.nextInteger();
        int k = input.nextInteger();

        int[][] intervals = new int[k + 2][2];
        for (int i = 1; i <= k; i++) {
            intervals[i][0] = input.nextInteger();
            intervals[i][1] = input.nextInteger();
        }
        intervals[k + 1][0] = 2 * n;
        intervals[k + 1][1] = 2 * n;

        k++;

        Node[] loop = new Node[2];
        loop[0] = Node.build(0, n);
        loop[1] = Node.build(0, n);

        int[] data = new int[n + 1];

        for (int i = 1; i <= k; i++) {
            Node curDp = loop[i & 1];

            Node lastDp = loop[(i - 1) & 1];

            int[] interval = intervals[i];
            int intervalLength = interval[1] - interval[0];
            int middleLength = interval[0] - intervals[i - 1][1];

            Node.toArray(0, n, data, lastDp);
            System.arraycopy(data, 0, data, intervalLength + middleLength, n + 1 - (intervalLength + middleLength));
            Arrays.fill(data, 0, intervalLength + middleLength, INF);

            //No flip
/*            for (int j = intervalLength + middleLength; j <= n; j++) {
                int lastFryTotalTime = j - intervalLength - middleLength;
                int minNeedTime = Node.query(lastFryTotalTime, lastFryTotalTime, 0, n, lastDp);
                Node.update(j, 0, n, minNeedTime, curDp);
            }*/

            int minX = interval[1] - middleLength - intervalLength;
            int maxX = interval[1] - middleLength;
            for (int j = 0; j <= n; j++) {
                //Flip once
                int minNeedTime = Node.query(minX - j, maxX - j, 0, n, lastDp);
                data[j] = Math.min(data[j], minNeedTime + 1);
               // Node.update(j, 0, n, minNeedTime + 1, curDp);
            }

            for (int j = 0; j <= n; j++) {
                //Flip twice
                int minNeedTime = Node.query(interval[1] - intervalLength, interval[1], 0, n, lastDp);
                //Node.update(j, 0, n, minNeedTime + 2, curDp);
                data[j] = Math.min(data[j], minNeedTime + 2);
            }

            Node.fromArray(0, n, data, curDp);
        }

        int minFlipTime = Node.query(n, n, 0, n, loop[k & 1]);

        if (minFlipTime < INF) {
            output.println("Full");
            output.println(minFlipTime);
        } else {
            output.println("Hungry");
        }
    }

    public static class Node {
        Node left;
        Node right;
        int batchSetValue;
        boolean dirty;
        int min;

        public static void setValue(int f, int t, int l, int r, int val, Node node) {
            if (f > r || t < l) {
                return;
            }
            if (f <= l && t >= r) {
                node.setValue(val);
                return;
            }

            node.pushDown();
            int m = (l + r) >> 1;
            setValue(f, t, l, m, val, node.left);
            setValue(f, t, m + 1, r, val, node.right);

            node.pushUp();
        }

        public static Node build(int l, int r) {
            Node node = new Node();
            if (l != r) {
                int m = (l + r) >> 1;
                node.left = build(l, m);
                node.right = build(m + 1, r);
            }
            return node;
        }

        public static int query(int f, int t, int l, int r, Node node) {
            if (f > r || t < l) {
                return INF;
            }
            if (f <= l && r <= t) {
                return node.min;
            }
            node.pushDown();
            int m = (l + r) >> 1;
            return Math.min(query(f, t, l, m, node.left), query(f, t, m + 1, r, node.right));
        }

        public static void update(int i, int l, int r, int val, Node node) {
            if (i > r || i < l) {
                return;
            }
            if (i <= l && i >= r) {
                node.min = Math.min(node.min, val);
                return;
            }

            node.pushDown();
            int m = (l + r) >> 1;
            update(i, l, m, val, node.left);
            update(i, m + 1, r, val, node.right);

            node.pushUp();
        }

        public static void toArray(int l, int r, int[] data, Node node) {
            if (l == r) {
                data[l] = node.min;
                return;
            }
            node.pushDown();
            int m = (l + r) >> 1;
            toArray(l, m, data, node.left);
            toArray(m + 1, r, data, node.right);
        }

        public static void fromArray(int l, int r, int[] data, Node node) {
            if (l == r) {
                node.min = data[l];
                return;
            }

            node.dirty = false;
            int m = (l + r) >> 1;
            fromArray(l, m, data, node.left);
            fromArray(m + 1, r, data, node.right);
            node.pushUp();
        }

        public void pushUp() {
            this.min = Math.min(left.min, right.min);
        }

        public void setValue(int val) {
            this.min = val;
            this.batchSetValue = val;
            this.dirty = true;
        }

        public void pushDown() {
            if (dirty) {
                left.setValue(batchSetValue);
                right.setValue(batchSetValue);
                dirty = false;
            }
        }

        @Override
        public String toString() {
            int t = 100;
            int[] data = new int[t + 1];
            for (int i = 0; i <= t; i++) {
                data[i] = query(i, i, 0, t, this);
            }
            return Arrays.toString(data);
        }
    }

    public static class BlockReader {
        static final int EOF = -1;
        InputStream is;
        byte[] dBuf;
        int dPos, dSize, next;
        StringBuilder builder = new StringBuilder();

        public BlockReader(InputStream is) {
            this(is, 4096);
        }

        public BlockReader(InputStream is, int bufSize) {
            this.is = is;
            dBuf = new byte[bufSize];
            next = nextByte();
        }

        public int nextByte() {
            while (dPos >= dSize) {
                if (dSize == -1) {
                    return EOF;
                }
                dPos = 0;
                try {
                    dSize = is.read(dBuf);
                } catch (Exception e) {
                }
            }
            return dBuf[dPos++];
        }

        public String nextBlock() {
            builder.setLength(0);
            skipBlank();
            while (next != EOF && !Character.isWhitespace(next)) {
                builder.append((char) next);
                next = nextByte();
            }
            return builder.toString();
        }

        public void skipBlank() {
            while (Character.isWhitespace(next)) {
                next = nextByte();
            }
        }

        public int nextInteger() {
            skipBlank();
            int ret = 0;
            boolean rev = false;
            if (next == '+' || next == '-') {
                rev = next == '-';
                next = nextByte();
            }
            while (next >= '0' && next <= '9') {
                ret = (ret << 3) + (ret << 1) + next - '0';
                next = nextByte();
            }
            return rev ? -ret : ret;
        }

        public int nextBlock(char[] data, int offset) {
            skipBlank();
            int index = offset;
            int bound = data.length;
            while (next != EOF && index < bound && !Character.isWhitespace(next)) {
                data[index++] = (char) next;
                next = nextByte();
            }
            return index - offset;
        }

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


import java.util.Scanner;

public class Magic_Box {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner r = new Scanner(System.in);
        int x,y,z,x1,y1,z1, zox,zox1, xoy,xoy1, yoz,yoz1;
        x = r.nextInt();
        y = r.nextInt();
        z = r.nextInt();
        x1 =r.nextInt();
        y1 = r.nextInt();
        z1 = r.nextInt();
        zox = r.nextInt();
        zox1 =r.nextInt();
        xoy = r.nextInt();
        xoy1 = r.nextInt();
        yoz = r.nextInt();
        yoz1 = r.nextInt();
        if (x>0 && x<x1){
            yoz=0;
            yoz1=0;
        }
        else if (x>0)
            yoz=0;
        else if (x<0)
            yoz1=0;
        else {
            yoz=0;
            yoz1=0;
        }
        if (y>0 && y<y1){
            zox=0;
            zox1=0;
        }else if (y>0)
            zox=0;
        else if (y<0)
            zox1=0;
        else{
            zox=0;
            zox1=0;
        }
        if (z>0 && z<z1){
            xoy=0;
            xoy1=0;
        }else if (z>0)
            xoy=0;
        else if (z<0)
            xoy1=0;
        else{
            xoy=0;
            xoy1=0;
        }
         long ans = zox+zox1+xoy+xoy1+ yoz+yoz1;
         System.out.println(ans);
    }

}


//package practice.E;

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

public class CF295C_ {

    static int[][][] dp1;
    static int[][][][] dp2;
    static int k;
    static int mod = 1000000007;
    static int F, H;
    static boolean[][][] vis;

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

        int n = sc.nextInt();
        k = sc.nextInt();
        for (int i = 0; i < n; i++) {
            if(sc.nextInt() == 50)
                F++;
            else
                H++;
        }

        comb = new long[100][100];
        for (int i = 0; i < comb.length; i++) {
            Arrays.fill(comb[i], -1);
        }
        dp1 = new int[2][51][51];
        for (int i = 0; i < dp1.length; i++) {
            for (int j = 0; j < dp1[i].length; j++) {
                Arrays.fill(dp1[i][j], -1);
            }
        }
        vis = new boolean[2][51][51];
        comb = new long[100][100];
        for (int i = 0; i < comb.length; i++) {
            Arrays.fill(comb[i], -1);
        }
        int min = solve(F, H, 0);
        if(min > 100000){
            System.out.println(-1);
            System.out.println(0);
            return;
        }

        dp2 = new int[2][51][51][min+1];
        for (int i = 0; i < vis.length; i++) {
            for (int j = 0; j < vis[i].length; j++) {
                Arrays.fill(vis[i][j], false);
            }
        }
        for (int i = 0; i < dp2.length; i++) {
            for (int j = 0; j < dp2[i].length; j++) {
                for (int l = 0; l < dp2[i][j].length; l++) {
                    Arrays.fill(dp2[i][j][l], -1);
                }
            }
        }

        System.out.println(min);
//        System.out.println(solve2(0, F, H, min));
        System.out.println(solve2(F, H, 0, min));

        pw.flush();
        pw.close();
    }

    static int solve(int fifty, int hundred, int side){
        if(fifty + hundred == 0 && side == 0)
            return 0;
        if(fifty == F && hundred == H && side == 1)
            return 0;
        if(dp1[side][fifty][hundred] != -1)
            return dp1[side][fifty][hundred];
        if(vis[side][fifty][hundred])
            return 1000000000;
        vis[side][fifty][hundred] = true;
        int ans = 1000000000;
        for (int ff = 0; ff <= fifty; ff++)
            for (int hh = 0; hh <= hundred; hh++) {
                if(hh + ff == 0 || hh*100+ff*50 > k)
                    continue;
                ans = Math.min(ans, 1 + solve(F-fifty+ff, H-hundred+hh, 1^side));
            }

        return dp1[side][fifty][hundred] = ans;
    }

    static int solve2(int fifty, int hundred, int side, int min){
        if(min < 0)
            return 0;
        if(fifty + hundred == 0 && side == 0)
            if(min == 0)
                return 1;
        if(fifty == F && hundred == H && side == 1)
            if(min == 0)
                return 1;
        if(min == 0)
            return 0;
        if(dp2[side][fifty][hundred][min] != -1)
            return dp2[side][fifty][hundred][min];
        int ans = 0;
        for (int ff = 0; ff <= fifty; ff++)
            for (int hh = 0; hh <= hundred; hh++) {
                if(hh + ff == 0 || hh*100+ff*50 > k)
                    continue;
//                if(1 + solve(F-fifty+ff, H-hundred+hh, 1^side) == solve(fifty, hundred, side))
                    ans = (int)((ans + (((1l*nCr1(fifty, ff)*nCr1(hundred, hh)%mod)*solve2(F-fifty+ff, H-hundred+hh, 1^side, min-1))%mod))%mod);
            }

        return dp2[side][fifty][hundred][min] = ans;
    }

    static long[][] comb;

    static long nCr1(int n , int k)
    {
        if(n < k)
            return 0;
        if(k == 0 || k == n)
            return 1;
        if(comb[n][k] != -1)
            return comb[n][k];
        if(n - k < k)
            return comb[n][k] = nCr1(n, n - k);
        return comb[n][k] = (nCr1(n - 1, k - 1) + nCr1(n - 1, k)) % mod;
    }

    static class Scanner
    {
        StringTokenizer st; BufferedReader br;
        public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}
        public Scanner(String s) throws FileNotFoundException {	br = new BufferedReader(new FileReader(new File(s)));}
        public String next() throws IOException {while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());return st.nextToken();}
        public int nextInt() throws IOException {return Integer.parseInt(next());}
        public long nextLong() throws IOException {return Long.parseLong(next());}
        public String nextLine() throws IOException {return br.readLine();}
        public boolean ready() throws IOException {return br.ready();}
    }
}


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

public class C {
	static int n, k, f, h;

	public static void main(String[] args) throws IOException {
		InputReader in = new InputReader();
		n = in.nextInt();
		k = in.nextInt();
		for (int i = 0; i < n; i++) {
			int w = in.nextInt();
			if (w == 100)
				h++;
			else
				f++;
		}
		nCr(55, 55);
		dp1 = new int[55][55];
		for (int i = 0; i < 55; i++)
			Arrays.fill(dp1[i], -1);
		dp2 = new long[55][55][102];
		for (int i = 0; i < 55; i++)
			for (int j = 0; j < 55; j++)
				Arrays.fill(dp2[i][j], -1);
		r1 = getMin(f, h);
		if (r1 >= 1 << 25) {
			System.out.println(-1);
			System.out.println(0);
		} else {
			long r2 = go(f, h, 0);
			System.out.println(r1);
			System.out.println(r2);
		}
	}

	static int r1;

	public static long go(int x, int y, int t) {
		if (x == f && y == h && t == r1)
			return 1;
		if(t>=r1)return 0;
		int side = t % 2;
		if (dp2[x][y][t] != -1)
			return dp2[x][y][t];
		long ret = 0;
		for (int i = 0; i <= x && i * 50 <= k; i++) {
			for (int j = 0; j <= y && i * 50 + j * 100 <= k; j++) {
				if (i + j == 0 || (i == x && j == y && side == 1))
					continue;
				ret = (ret+((go(f-x+i, h-y+j, t+1)*c[x][i])%mod)*c[y][j])%mod;
			}
		}
		return dp2[x][y][t] = ret;
	}

	static long[][][] dp2;
	static int[][] dp1;
	static int mod = 1000000007;

	public static int getMin(int x, int y) {
		if (x == 0 && y == 0)
			return 0;
		if (dp1[x][y] != -1)
			return dp1[x][y];
		int ret = 1 << 25;
		for (int i = 0; i <= x && i * 50 <= k; i++) {
			for (int j = 0; j <= y && i * 50 + j * 100 <= k; j++) {
				if (i + j == 0)
					continue;
				if (i == x && j == y) {
					ret = 1;
				} else {
					if (i != 0 && j != 0) {
						ret = Math.min(
								ret,
								2 + Math.min(getMin(x - i + 1, y - j),
										getMin(x - i, y - j + 1)));
					} else if (i > 1 && j == 0) {
						ret = Math.min(ret, 2 + getMin(x - i + 1, y));
					} else if (i == 0) {
						if (j > 1) {
							ret = Math.min(2+getMin(x - i, y - j + 1), ret);
						}
						if (f - x >= 1) {
							ret = Math.min(2+getMin(x + 1, y - j), ret);
						}
					}
				}
			}
		}
		return dp1[x][y] = ret;
	}

	static long[][] c;

	public static void nCr(int n, int r) {
		c = new long[n + 1][r + 1];
		for (int i = 0; i < n + 1; i++)
			c[i][0] = 1;

		for (int j = 1; j <= r; j++)
			for (int i = j; i <= n; i++)
				c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer st;

		public InputReader() throws IOException {
			in = new BufferedReader(new InputStreamReader(System.in));
			st = new StringTokenizer(in.readLine());
		}

		public String next() throws IOException {
			while (!st.hasMoreElements())
				st = new StringTokenizer(in.readLine());
			return st.nextToken();
		}

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

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


import java.util.Scanner;

public class CodeForces127 {
  public static void main(String argv[]) {
    Scanner scanner = new Scanner(System.in);

    long money = scanner.nextLong();
    long plastic = scanner.nextLong();
    long glassCost = scanner.nextLong();
    long glassReturn = scanner.nextLong();
    long liters = 0;

    if(glassCost - glassReturn < plastic) {
      while(money >= glassCost) {
        long token  = money;
        money = money % glassCost;
        liters += token / glassCost;
        money += glassReturn * (token / glassCost);
      }

      liters += money / plastic;
    } else {
      liters += money / plastic;
    }

    System.out.println(liters);

    scanner.close();
  }
}


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

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

class TaskD {
    int[][] t = new int[3][3];
    long[][][] hanoi = new long[40][3][3];

    public void solve(int testNumber, InputReader jin, PrintWriter jout) {
        for(long[][] grid: hanoi)
            for(long[] row: grid)
                Arrays.fill(row, -1);

        for(int i = 0; i < 3; i++)
            for(int j = 0; j < 3; j++)
                t[i][j] = jin.int32();

        jout.println(dp(jin.int32(), 0, 2));
    }

    private long dp(int n, int i, int j) {
        if(n == 0) return 0;
        if(hanoi[n][i][j] == -1) {
            int other = 3 - i - j;
            long sol1 = dp(n - 1, i, other) + t[i][j] + dp(n - 1, other, j);
            long sol2 = dp(n - 1, i, j) + t[i][other] + dp(n - 1, j, i) + t[other][j] + dp(n - 1, i, j);
            hanoi[n][i][j] = Math.min(sol1, sol2);
        }
        return hanoi[n][i][j];
    }
}

class InputReader {
    private static final int bufferMaxLength = 1024;
    private InputStream in;
    private byte[] buffer;
    private int currentBufferSize;
    private int currentBufferTop;
    private static final String tokenizers = " \t\r\f\n";

    public InputReader(InputStream stream) {
        this.in = stream;
        buffer = new byte[bufferMaxLength];
        currentBufferSize = 0;
        currentBufferTop = 0;
    }

    private boolean refill() {
        try {
            this.currentBufferSize = this.in.read(this.buffer);
            this.currentBufferTop = 0;
        } catch(Exception e) {}
        return this.currentBufferSize > 0;
    }

    private Byte readChar() {
        if(currentBufferTop < currentBufferSize) {
            return this.buffer[this.currentBufferTop++];
        } else {
            if(!this.refill()) {
                return null;
            } else {
                return readChar();
            }
        }
    }

    public String token()  {
        StringBuffer tok = new StringBuffer();
        Byte first;
        while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) != -1));
        if(first == null) return null;
        tok.append((char)first.byteValue());
        while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) == -1)) {
            tok.append((char)first.byteValue());
        }
        return tok.toString();
    }

    public Integer int32() throws NumberFormatException {
        String tok = token();
        return tok == null? null : Integer.parseInt(tok);
    }

    }


import java.io.*;
import java.lang.reflect.Parameter;
import java.nio.charset.IllegalCharsetNameException;
import java.text.CollationElementIterator;
import java.util.*;

public class Solution {
    public static void main(String[] args) throws IOException {
        OutputStream outputStream = System.out;
        FastReader in = new FastReader();
        OutputWriter out = new OutputWriter(outputStream);
        Taska solver=  new Taska();
        solver.solve(in,out);
        out.close();
    }
}
class Taska{
    public void solve(FastReader in,OutputWriter out) {
        int x=in.ni();
        int y=in.ni();
        int n=in.ni();
        long[] ar= new long[n+1];
        ar[1]=x;
        ar[2]=y;
        for(int i=3; i<=n;i++){
            ar[i]= ar[i-1]-ar[i-2];
        }
        int mod= 1000000007;
        out.println(((ar[n]%mod)+mod)%mod);
    }
}
class St_class {
    int st;
    int[] stt = new int[1000000];
}
class Node{
    int data;
    Node left, right;
    public Node(int data){
        this.data=data;
        left=null;
        right=null;
    }
}
class StringAlgorithm{
    public ArrayList<Integer> KMP(int[] text, int[] pat){
        ArrayList<Integer> res= new ArrayList<>();
        int[] lps=  lps(pat);
        int i=0;
        int j=0;
        int n=text.length;
        int m=pat.length;
        while(i<n){
            if(pat[j]==text[i]){
                i++;
                j++;
            }
            if(j==m){
                res.add(i-j);
                j=lps[j-1];
            }
            else if(i<n && pat[j]!=text[i]){
                if(j!=0){
                    j=lps[j-1];
                }
                else {
                    i=i+1;
                }
            }
        }
        return res;
    }
    public int[] lps(int[] pat){
        int[] ar= new int[pat.length];
        int len=0;
        int i=1;
        ar[0]=0;
        while(i<pat.length){
            if(pat[i]==pat[len]){
                len++;
                ar[i]=len;
                i++;
            }
            else{
                if(len!=0){
                    len= ar[len-1];
                }
                else {
                    ar[i]=0;
                    i++;
                }
            }
        }
        return ar;
    }
}
class FastReader {
    BufferedReader br;
    StringTokenizer st;

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

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

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

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

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

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

    }

}
class OutputWriter extends PrintWriter {

    public OutputWriter(OutputStream outputStream) {
        super(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

}
class Pair<U, V> {
    final U first;
    final V second;
    Pair(U first, V second) {
        this.first = first;
        this.second = second;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        Pair<?, ?> pair = (Pair<?, ?>) o;
        if (!first.equals(pair.first))
            return false;
        return second.equals(pair.second);
    }

    @Override
    public int hashCode() {
        return 31 * first.hashCode() + second.hashCode();
    }

    @Override
    public String toString() {
        return "(" + first + ", " + second + ")";
    }
    public static <U, V> Pair <U, V> of(U a, V b) {
        return new Pair<>(a, b);
    }

}


import java.util.*;
public class file
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int x=in.nextInt();
        int y=in.nextInt();
        int n=in.nextInt();
        int a[]=new int[n];
        a[0]=x; a[1]=y;
        for (int i=2; i<n; i++)
        a[i]=a[i-1]-a[i-2];
        if(a[n-1]<0)
        {
            n=-a[n-1];
            int c= (1000000007)-n%(1000000007);
            System.out.println(c);
        }
        else
        System.out.println(a[n-1]%1000000007);
    }
}


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

public class Q13 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int mod = 1000000007;
		Scanner s = new Scanner(System.in);
		long x = s.nextLong();
		long y = s.nextLong();
		int n = s.nextInt();
		long[] arr = new long[n + 1];
		arr[1] = x;
		arr[2] = y;
		for (int i = 2; i < n; i++) {
			arr[i + 1] = (arr[i] - arr[i - 1]) % mod;
		}
		if (arr[n] < 0) {
			System.out.println(arr[n] + mod);
		} else {
			System.out.println(arr[n]);
		}
	}

}


import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
         Scanner m=new Scanner(System.in);
     int  num=m.nextInt();
     int num2=m.nextInt();int z=num2;int count2=0;int sum=0;
     sum=num+num2;int count3=0,t=0;int g=sum;
     while(g>0){
     t=g%10;
     g/=10;
     count3++;
     }
       int sumz[]=new int[count3];
       int index=sumz.length-1;int ind=0;
       while(sum>0){
           ind=sum%10;
           sumz[index]=ind;
           sum/=10;
           index--;if(index<0)break;
       }

       int ad=0;
       for(int i=sumz.length-1;i>=0;i--){
       if(sumz[i]==0){
       ad=i-1;
       for(int na=i;na>0&&ad>=0;){
       sumz[na]=sumz[ad];
       sumz[ad]=0;
       if(na<0||ad==0)
           break;

       }}
            }int sum4=0;int pow=1;
            for(int i=sumz.length-1;i>=0;i--,pow*=10){
            sum4+=sumz[i]*pow;}

     int count=0;
     int j=0;
     int x=num;
     while(x>0){
     j=x%10;
     x/=10;
     count++;}

       int a[]=new int[count];int y=0,k=a.length-1;
while(num>0){
y=num%10;
a[k]=y;
num/=10;
k--;
}int d=0;
      while(z>0){
      d=z%10;
      z/=10;
   count2++;   }
       int b[]=new int[count2];int p=b.length-1;
       while(num2>0){
        b[p]=num2%10;
       num2/=10;
       p--;
       }
int e=0;int f=0;
       for(int i=b.length-1;i>=0;i--){
       if(b[i]==0){
       e=i;f=i-1;
       for(int s=e;s>0&&f>=0;){
       b[s]=b[f];
       b[f]=0;
       f--;s--;
         if(s<0||f==0)
       break;}
       }
       }

int q=0;
for(int i=a.length-1;i>=0;i--){
if(a[i]==0){
    q=i-1;
for(int w=i;w>0&&q>=0;){
a[w]=a[q];
a[q]=0;
if(w<0||q==0)
    break;

}}
}

int sum2=0;int v=1;
for(int i=b.length-1;i>=0;i--,v*=10)
          sum2+=b[i]*v;
int r=1;int sum3=0;
for(int i=a.length-1;i>=0;i--,r*=10)
    sum3+=a[i]*r;

       if(sum2+sum3==sum4)
            System.out.println("YES");
       else
            System.out.println("NO");

    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;

public class Palindrome {

    public static long removeZeros(long x) {
        String newNumber = "";
        while (x > 0) {
            if (x % 10 != 0) {
                newNumber = (x % 10) + newNumber;
            }
            x /= 10;
        }
        return Long.valueOf(newNumber);
    }

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

        BufferedReader reader3 = new BufferedReader(new InputStreamReader(
                System.in));
        String number1 = reader3.readLine();
        String number2 = reader3.readLine();
        long res = Long.valueOf(number1) + Long.valueOf(number2);
        long res2 = removeZeros(Long.valueOf(number1))
                + removeZeros(Long.valueOf(number2));
        if (res == res2) {
            System.out.println("YES");
            return;
        }
        if (removeZeros(res) == res2) {
            System.out.println("YES");
            return;
        }
        System.out.println("NO");
    }
}


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;

public class B {

	static int N;
	static int ar[];
	static boolean ldp[][][],rdp[][][];
	static boolean [][][] LU,RU;
	public static void main(String[] args) {
		JS in = new JS();
		N = in.nextInt();
		ar = new int[N];
		int cnt[] = new int[101];
		for(int i = 0; i < N; i++) {
			ar[i] = in.nextInt();
			cnt[ar[i]]++;
		}
		Arrays.sort(ar);

		ldp = new boolean[101][101][10001];
		rdp = new boolean[101][101][10001];

		LU = new boolean[101][101][10001];
		RU = new boolean[101][101][10001];
		int res = 0;
		int id = 0;
		for(int i = 0; i <= 100; i++) {
			for(int take = 1; take <= cnt[i]; take++) {
				int curSum = i*take;

				boolean found = false;
				for(int leftSum = 0; leftSum <= curSum; leftSum++) {
					int rightSum = curSum-leftSum;
					for(int leftTake = 1; leftTake < take; leftTake++) {
						int rightTake = take-leftTake;

						if(canLeft(id-1, leftTake, leftSum) && canRight(id+cnt[i],rightTake,rightSum)) found = true;
					}
				}
				//If we find a bad thing then break out
				if(found) break;
				res = Math.max(res, take);
			}
			id += cnt[i];
		}
		System.out.println(res);
	}
	static boolean canLeft(int id, int take, int sum) {
		if(take == 0) return sum == 0;
		if(id < 0) return false;
		if(sum <= 0) return false;
		if(LU[id][take][sum]) return ldp[id][take][sum];
		boolean res = canLeft(id-1,take,sum) | canLeft(id-1,take-1,sum-ar[id]);
		LU[id][take][sum] = true;
		return ldp[id][take][sum] = res;
	}
	static boolean canRight(int id, int take, int sum) {
		if(take == 0) return sum == 0;
		if(id >= N) return false;
		if(sum <= 0) return false;
		if(RU[id][take][sum]) return rdp[id][take][sum];
		boolean res = canRight(id+1,take,sum) | canRight(id+1,take-1,sum-ar[id]);
		RU[id][take][sum] = true;
		return rdp[id][take][sum] = res;
	}

	static class JS{
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public JS() {
			in = new BufferedInputStream(System.in, BS);
		}

		public JS(String s) throws FileNotFoundException {
			in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
		    while(c!='.'&&c!='-'&&(c <'0' || c>'9')) c = nextChar();
		    boolean neg = c=='-';
		    if(neg)c=nextChar();
		    boolean fl = c=='.';
		    double cur = nextLong();
		    if(fl) return neg ? -cur/num : cur/num;
		    if(c == '.') {
			double next = nextLong();
			return neg ? -cur-next/num : cur+next/num;
		    }
		    else return neg ? -cur : cur;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
	}

}


import java.io.*;

public class chess {
		final String black = "BBBBBBBB";
		public void solve() throws IOException
		{
				//BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
				BufferedReader in = new BufferedReader(new FileReader("t.in"));
				PrintWriter out = new PrintWriter(System.out);
				char[][] board = new char[8][8];
				String line = "";
				int idx = -1;

				while(true)
				{
						line = in.readLine();
						if(line == null) break;
						board[++idx] = line.toCharArray();
				}

				int ret = 0;

				for(int i = 0; i < 8; ++i)
				{
						if(new String(board[i]).equals(black)) ++ret;
				}

				if(ret == 8)
				{
					out.println(ret);
					out.flush();
					return;
				}

				for(int j = 0; j < 8; ++j)
				{
						String temp = "";
						for(int i = 0; i < 8; ++i)
							temp += board[i][j];
						if(temp.equals(black)) ++ret;
				}

				out.println(ret);
				out.flush();
		}

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


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.io.Writer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;

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

    static class TaskD {
        public void solve(int testNumber, FastScanner in, FastPrinter out) {
            int m = in.nextInt();
            int a = in.nextInt();
            int b = in.nextInt();
            int mx = Math.min(m, 2 * (a + b));
            final int[] d = new int[mx + 1];
            Arrays.fill(d, Integer.MAX_VALUE);
            d[0] = 0;
            TreeSet<Integer> q = new TreeSet<>(new Comparator<Integer>() {

                public int compare(Integer o1, Integer o2) {
                    int c = Integer.compare(d[o1], d[o2]);
                    if (c != 0) return c;
                    return Integer.compare(o1, o2);
                }
            });
            q.add(0);
            int[] ans = new int[mx + 1];
            while (!q.isEmpty()) {
                int v = q.pollFirst();
                ans[d[v]]++;
                if (v - b >= 0 && d[v - b] == Integer.MAX_VALUE) {
                    d[v - b] = d[v];
                    q.add(v - b);
                }
                if (v + a <= mx && d[v + a] == Integer.MAX_VALUE) {
                    d[v + a] = Math.max(d[v], v + a);
                    q.add(v + a);
                }
            }
            long answer = 0;
            long current = 0;
            for (int i = 0; i <= mx; i++) {
                current += ans[i];
                answer += current;
            }
            if (mx == 2 * (a + b) && mx / MathUtils.gcd(a, b) + 1 != current) throw new AssertionError();
            int i = mx + 1;
            int g = MathUtils.gcd(a, b);
            while (i <= m && i % g != 0) {
                answer += i / g + 1;
                ++i;
            }
            int last = m / g * g;
            int from = i / g + 1;
            int to = last / g + 1;
            answer += ((long) to * (to - 1) - (long) from * (from - 1)) / 2L * g;
            while (last <= m) {
                answer += last / g + 1;
                last++;
            }
            out.println(answer);
        }

    }

    static class FastPrinter extends PrintWriter {
        public FastPrinter(OutputStream out) {
            super(out);
        }

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

    }

    static class MathUtils {
        public static int gcd(int a, int b) {
            if (a < 0) a = -a;
            if (b < 0) b = -b;
            while (b != 0) {
                int t = a % b;
                a = b;
                b = t;
            }
            return a;
        }

    }

    static class FastScanner extends BufferedReader {
        public FastScanner(InputStream is) {
            super(new InputStreamReader(is));
        }

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

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

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

        public String readLine() {
            try {
                return super.readLine();
            } catch (IOException e) {
                return null;
            }
        }

    }
}


/*
    https://codeforces.com/contest/1146/problem/B
 */

import java.util.Scanner;

public class ForethoughtFutureCup2019ERD {
    private static long gcd = 1;
    private static long delta = 0;

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

        long sum = 0;

        for (long i = 0; i <= a + b - 1 && i <= m; i++) {
            sum += f(i, a, b);
        }
        long a0 = a + b;
        if (m > a0 - 1) {
            long k = (long) Math.ceil(a0 / gcd);
            long z = (long) Math.ceil(m / gcd) - 1;
            sum += (k + z) * (z - k + 1) / 2 * gcd;

            for (long i = a0; i < k * gcd; i++) {
                sum += i / gcd;
            }
            for (long i = (z + 1) * gcd; i <= m; i++) {
                sum += i / gcd;
            }
            sum += m - a0 + 1;
        }
        System.out.println(sum);
    }

    private static long f(long x, long a, long b) {
        if (x >= a + b - 1) {
            return 1 + x / gcd;
        } else {
            long result = 1;
            long pos = 0;
            while (true) {
                if (pos + a > x) {
                    pos -= b;
                    if (pos <= 0) {
                        return result;
                    } else {
                        result++;
                    }
                } else {
                    pos += a;
                    result++;
                }
            }
        }
    }

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

}


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

/*
 * 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 ddebettencourt20
 */
public class Nirvana {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        int n = sc.nextInt();
        int max = 0;
        if (n > 1000000000){
            System.out.println("387420489");
            System.exit(0);
        } else {
        if (n >= 9999999 && n < 26000000){
            System.out.println("4782969");
            System.exit(0);
        }
        if (n >= 99999999 && n < 260000000){
            System.out.println("43046721");
            System.exit(0);
        }
        int min = n-100000005;
        if (min < 0){
            min = 0;
        }
        for (int i = min; i<=n; i++){
        if (findProduct(i) > max){
            max = findProduct(i);
           // System.out.println("new max: " + max + ", " + i);
        }
        }
        }
        System.out.println(max);
    }
    private static int findProduct(int n){
        int product = 1;
        while (n >= 1){
            product *= n % 10;
            n = n / 10;
        }
        return product;
    }
    static class MyScanner {
      BufferedReader br;
      StringTokenizer st;

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

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

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

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

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

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

   }
}


import java.util.*;

/**
 * Created by zhengyanglzy on 2017/6/24.
 */
public class Main {
    private static final int MAXIMUM_IND = 65;
    private static final long[] POWER;
    private static final long[] POWER_SUM;
    private static final Map<Long, Long> SPACE;
    static Random random = new Random(System.currentTimeMillis());

    static {
        POWER = new long[MAXIMUM_IND];
        POWER_SUM = new long[MAXIMUM_IND];
        POWER[0] = POWER_SUM[0] = 0;
        POWER[1] = POWER_SUM[1] = 1;
        for (int i = 2; i < MAXIMUM_IND; ++i) {
            POWER[i] = POWER[i - 1] * 2;
            POWER_SUM[i] = POWER_SUM[i - 1] + POWER[i];
        }
        SPACE = new HashMap<Long, Long>();
        SPACE.put(0L, 0L);
        SPACE.put(1L, 0L);
        SPACE.put(2L, 0L);
    }

    public static void main(String[] args) {
        // testSO(5000);
        // compare(51, 400);
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            long houseNum = sc.nextLong();
            long k = sc.nextLong();
            System.out.println(cal(houseNum, k));
        }
    }

    public static void compare(int start, int end) {
        for (int i = start; i <= end; ++i) {
            List<Integer> res = generate(i);
            assert res.size() == i;
            boolean isMatch = true;
            for (int j = 0; j < res.size(); ++j) {
                long got = cal(i + 2, j + 3);
                long expected = res.get(j) + 1;
                if (expected != got) {
                    isMatch = false;
                    System.err.printf("(%d, %d) expected %d, got %d\n", i + 2, j + 3, expected, got);
                }
            }
            if (!isMatch) {
                break;
            }
            System.out.printf("complete %d\n", i);
        }
        System.out.println("over");
    }

    public static void testSO(int casenum) {
        while (casenum -- > 0) {
            long n = random.nextLong();
            long k = random.nextLong();
            if (k > n) {
                long tmp = n;
                n = k;
                k = tmp;
            }
            try {
                findLabel(n, k);
            } catch (StackOverflowError e) {
                System.err.printf("%d %d", n, k);
                break;
            }
        }
        System.out.println("over");
    }

    public static List<Integer> generate(int houseNum) {
        TreeSet<Seg> segs = new TreeSet<Seg>();
        segs.add(new Seg(1, houseNum));
        List<Integer> ret = new ArrayList<Integer>();
        while (!segs.isEmpty()) {
            Seg seg = segs.first();
            segs.remove(seg);
            int pos = (seg.length + 1) / 2 + seg.start - 1;
            ret.add(pos);
            if (pos - seg.start > 0) {
                segs.add(new Seg(seg.start, pos - seg.start));
            }
            if (seg.length + seg.start - pos - 1> 0) {
                segs.add(new Seg(pos + 1, seg.length + seg.start - pos - 1));
            }
        }
        return ret;
    }

    static long cal(long houseNum, long k) {
        if (k == 1) {
            return 1;

        } else if (k == 2) {
            return 2;
        }
        return 1 + findLabel(houseNum - 2, k - 2);
    }

    static long findSpace(long num) {
        if (SPACE.containsKey(num)) {
            return SPACE.get(num);
        }
        long medium = (num + 1) / 2;
        long res = 1 + findSpace(medium - 1) + findSpace(num - medium);
        SPACE.put(num, res);
        return res;
    }

    static long findLabel(long houseNum, long k) {
        // System.out.printf("%d %d\n", houseNum, k);
        long medium = (houseNum + 1) / 2;
        if (k == 1) {
            return medium;
        }
        /*
        long totalSpace = findSpace(houseNum);
        if (totalSpace < k) {
            // 慢慢填
            if (totalSpace == 0) {
                return k;
            }
            k -= totalSpace;
            long left = medium - 1 - findSpace(medium - 1);
            if (k <= left) {
                return findLabel(medium - 1, k + findSpace(medium - 1));
            }
            return medium + findLabel(houseNum - medium, k - left + findSpace(houseNum - medium));
        }*/
        k -= 1;
        Part leftPart = new Part(medium - 1);
        Part rightPart = new Part(houseNum - medium);
        long leftTotal = 0;
        long rightTotal = 0;
        while (true) {
            long left = leftPart.peek();
            long right = rightPart.peek();
            if (left == 0 && right == 0) {
                break;
            }
            if (k > left + right) {
                k -= left + right;
                leftTotal += left;
                rightTotal += right;
                leftPart.split();
                rightPart.split();
                continue;
            }
            leftPart.collapse();
            rightPart.collapse();
            TreeSet<Long> set = new TreeSet<Long>();
            set.addAll(leftPart.segs.keySet());
            set.addAll(rightPart.segs.keySet());
            for (long len : set.descendingSet()) {
                if (leftPart.segs.containsKey(len)) {
                    if (leftPart.segs.get(len) >= k) {
                        return findLabel(medium - 1, leftTotal + k);
                    } else {
                        k -= leftPart.segs.get(len);
                        leftTotal += leftPart.segs.get(len);
                    }
                }
                if (rightPart.segs.containsKey(len)) {
                    if (rightPart.segs.get(len) >= k) {
                        return medium + findLabel(houseNum - medium, rightTotal + k);
                    } else {
                        k -= rightPart.segs.get(len);
                        rightTotal += rightPart.segs.get(len);
                    }
                }
            }
        }
        // 只剩1和2
        if (medium - 1 - leftTotal >= k) {
            return findLabel(medium - 1, leftTotal + k);
        }
        return medium + findLabel(houseNum - medium, rightTotal + k - (medium - 1 - leftTotal));
    }

    static class Part {
        Map<Long, Long> segs;

        Part(long num) {
            segs = new HashMap<Long, Long>();
            segs.put(num, 1L);
        }

        void split() {
            Map<Long, Long> newMap = new HashMap<Long, Long>();
            for (Map.Entry<Long, Long> entry : segs.entrySet()) {
                if (entry.getKey() <= 2) {
                    update(newMap, entry.getKey(), entry.getValue());
                    continue;
                }
                long medium = (entry.getKey() + 1) / 2;
                update(newMap, medium - 1, entry.getValue());
                update(newMap, entry.getKey() - medium, entry.getValue());
            }
            segs = newMap;
        }

        long peek() {
            long ret = 0;
            for (Map.Entry<Long, Long> entry : segs.entrySet()) {
                if (entry.getKey() > 2) {
                    ret += entry.getValue();
                }
            }
            return ret;
        }

        void collapse() {
            Map<Long, Long> newMap = new HashMap<Long, Long>();
            for (Map.Entry<Long, Long> entry : segs.entrySet()) {
                update(newMap, (entry.getKey() + 1) / 2, entry.getValue());
            }
            segs = newMap;
        }

        static void update(Map<Long, Long> map, long key, long inc) {
            if (!map.containsKey(key)) {
                map.put(key, 0L);
            }
            map.put(key, map.get(key) + inc);
        }
    }

    static class Seg implements Comparable<Seg> {
        int start;
        int length;

        public Seg(int start, int length) {
            this.start = start;
            this.length = length;
        }

        public int compareTo(Seg o) {
            int diff = (length - 1) / 2 - (o.length -1) / 2;
            if (diff != 0) {
                return -diff;
            }
            return start - o.start;
        }
    }
}


import java.util.Scanner;

/**
 * Created by zhengyanglzy on 2017/6/24.
 */
public class Main {
    private static final int MAXIMUM_IND = 65;
    private static final long[] POWER;
    private static final long[] POWER_SUM;

    static {
        POWER = new long[MAXIMUM_IND];
        POWER_SUM = new long[MAXIMUM_IND];
        POWER[0] = POWER_SUM[0] = 0;
        POWER[1] = POWER_SUM[1] = 1;
        for (int i = 2; i < MAXIMUM_IND; ++i) {
            POWER[i] = POWER[i - 1] * 2;
            POWER_SUM[i] = POWER_SUM[i - 1] + POWER[i];
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            long houseNum = sc.nextLong();
            long k = sc.nextLong();
            if (k == 1) {
                System.out.println(1);
                continue;
            } else if (k == 2) {
                System.out.println(houseNum);
                continue;
            }
            System.out.println(1 + findLabel(houseNum - 2, k - 2));
        }
    }

    static long findLabel(long houseNum, long k) {
        long medium = (houseNum + 1) / 2;
        if (k == 1) {
            return medium;
        }
        k -= 1;
        Part left = new Part(0, medium - 1);
        Part right = new Part(medium, houseNum - medium);
        boolean strict = left.houseNum % 2 == 0 && right.houseNum - left.houseNum == 1;
        if (left.houseNum <= 2) {
            if (strict && k == 1) {
                return right.start + findLabel(right.houseNum, k);
            }
            if (k - (strict ? 1 : 0) <= left.houseNum) {
                return k - (strict ? 1 : 0);
            }
            return right.start + findLabel(right.houseNum, k - left.houseNum);
        }
        if (strict) {
            Part tmp = left;
            left = right;
            right = tmp;
        }
        int ind;
        for (ind = 1; k > 2 * POWER_SUM[ind]; ++ind) {
        }
        k -= 2 * POWER_SUM[ind - 1];
        if (left.houseNum - POWER_SUM[ind - 1] <= 2 && k <= 2) {
            // 此时不满足规律
            return left.start + findLabel(left.houseNum, POWER_SUM[ind - 1] + k);
        }
        // if (k <= 2 && k == left.houseNum - POWER_SUM[ind - 1])
        if (k <= POWER[ind] && POWER_SUM[ind - 1] + k <= left.houseNum) {
            // left
            return left.start + findLabel(left.houseNum, POWER_SUM[ind - 1] + k);
        }
        // right
        long newK = POWER_SUM[ind - 1] + k - POWER[ind];
        if (POWER_SUM[ind - 1] + k > left.houseNum) {
            // 左边剩余的房子不一定够POWER[ind]
            newK = POWER_SUM[ind - 1]+ k - (left.houseNum - POWER_SUM[ind - 1]);
        }
        return right.start + findLabel(right.houseNum, newK);
    }

    static class Part {
        long start;
        long houseNum;

        Part(long start, long houseNum) {
            this.start = start;
            this.houseNum = houseNum;
        }
    }
}


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

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

	void main() {
		int a = sc.nextInt();
		int b = sc.nextInt();
		if (a > b) {
			int tmp = a; a = b; b = tmp;
		}
		int ab = a + b;
		double x = (double) a / ab;
		int ans = 0;
		for (int cd = 2; cd <= ab; cd++) {
			int c = (int) (cd * x + 0.5);
			if (c == 0)
				continue;
			int d = cd - c;
			int k = a / c, l = b / d;
			if (k == l || k == l + 1 && a % c == 0 || k == l - 1 && b % d == 0)
				ans++;
		}
		println(ans);
	}
}


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

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

	int compress(int[] aa, int n) {
		Random rand = new Random();
		for (int i = 0; i < n; i++) {
			int j = rand.nextInt(i + 1);
			int tmp = aa[i]; aa[i] = aa[j]; aa[j] = tmp;
		}
		Arrays.sort(aa, 0, n);
		int m = 1;
		for (int i = 1; i < n; i++)
			if (aa[i] != aa[i - 1])
				aa[m++] = aa[i];
		return m;
	}
	void main() {
		int a = sc.nextInt();
		int b = sc.nextInt();
		int n = a + b;
		int s = (int) Math.sqrt(n) + 1;
		int[] gg = new int[s + s];
		int m = 0;
		for (int g = 1; g <= s; g++)
			gg[m++] = g;
		for (int k = 1; k <= s; k++) {
			int g = n / k;
			gg[m++] = g;
		}
		m = compress(gg, m);
		int ans = 0;
		for (int h = 0; h < m; h++) {
			int g = gg[h];
			int l = Math.max(n / (g + 1) + 1, (int) ((a + (long) g) / (g + 1) + (b + (long) g) / (g + 1)));
			int r = Math.min(n / g, a / g + b / g);
			if (l <= r)
				ans += r - l + 1;
		}
		println(ans);
	}
}


package Practice;
import java.util.*;

public class DreamoonandStairs_476A {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt(),m=sc.nextInt();
		int i,j,min=100000;
		j= (n%2==0)?n/2:(n/2)+1;
		for(i=n;i>=j;i--) {
				if(i%m==0)
					min=Math.min(min,i);
			}
		if(min==100000) System.out.println(-1);
		else System.out.println(min);

		}

	}


import java.util.*;
import java.math.*;
import java.io.*;
public class Main
	{
	public static void main(String args[])	throws IOException
		{
		Scanner c=new Scanner(System.in);
		String s=c.nextLine();
		int max=-1;
		for(int i=0;i<s.length();i++)
			{
			for(int j=i+1;j<s.length();j++)
				{
				String s1=s.substring(0,i);
				String s2=s.substring(i,j);
				String s3=s.substring(j,s.length());
				if(s1.length()==0||s2.length()==0||s3.length()==0)
					continue;
				if((s1.charAt(0)=='0'&&s1.length()>=2)||(s2.charAt(0)=='0'&&s2.length()>=2)||(s3.charAt(0)=='0'&&s3.length()>=2))
					continue;
				else
					{
					int c1=Integer.parseInt(s1);
					int c2=Integer.parseInt(s2);
					int c3=Integer.parseInt(s3);
					if(c1>1000000 ||c2>1000000 ||c3>1000000)
						continue;
					else
						{
						if(c1+c2+c3>max)
							max=c1+c2+c3;
						}
					}
				}
			}
		System.out.println(max);

		}

	}

//must declare new classes here


import java.util.Scanner ;
public class RopeWalkers{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        int A[] = new int[4];

        System.out.println("Enter Values :");

        //for (int n = 0; n < A.length ; n++){
        //	A[n] = input.nexxtInt();
        //}

        // for (int i =1;i < 3 ; i++){
        	int j = i -1;
        	int key = A[i];

        	while (A[j]> key && j >=0){
        		A[j +1 ] =A[j];
        		j-- ;
        	}
        	A[j]= key;
        //}
        //for (elem : A){
        //	System.out.print(elem);
        //}

    }
}


3 1 5 6
import java.io.*;
import java.util.*;

public class solution{

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

        Scanner sc=new Scanner(System.in);
        int a1=sc.nextInt();
        int b=sc.nextInt();
        int c=sc.nextInt();
        int d=sc.nextInt();
        int a[]={a1,b,c};
        Arrays.sort(a);
        long ans=0;
        ans+=Math.max(0,a[0]-(a[1]-d));
        ans+=Math.max(0,a[1]+d-a[2]);
        System.out.println(ans);
    }
}


import java.io.*;
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        long x=input.nextLong();
        long y=input.nextLong();
        System.out.println(solve(n,x,y));
    }
    static long solve(int n,long x,long y) {
        long dp[][]=new long[3][2*n+2];   //0->add    1->multiply     2->subtract
        for(int i=1;i<2*n+2;i++) {
            dp[0][i]=dp[1][i]=dp[2][i]=Long.MAX_VALUE-1;
            if(i%2==0) {
                dp[0][i]=Math.min(dp[0][i-1],dp[1][i-1])+x;
                dp[1][i]=Math.min(Math.min(dp[0][i/2],dp[1][i/2]),dp[2][i/2])+y;
            }
            else {
                dp[0][i]=Math.min(dp[0][i-1],dp[1][i-1])+x;
            }
            dp[2][i-1]=Math.min(dp[0][i], dp[1][i])+x;
        }
        return Math.min(Math.min(dp[0][n],dp[1][n]),dp[2][n]);
    }
}