Vasya is studying number theory. He has denoted a function f(a, b) such that:

  * f(a, 0) = 0; 
  * f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b. 



Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly.

Input

The first line contains two integer numbers x and y (1 ≤ x, y ≤ 1012).

Output

Print f(x, y).

Examples

Input

3 5


Output

3


Input

6 3


Output

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

public class Main {
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        new Main().run(in, out);
        out.close();
    }

    public static long mod = 17352642619633L;

    void run(FastScanner in, PrintWriter out) {

        long X = in.nextLong();
        long Y = in.nextLong();

        out.println(go(X, Y));

        // for (int i = 1; i <= 1000; i++) {
        //     for (int j = 1; j <= 1000; j++) {
        //         long a1 = go(i, j);
        //         long a2 = go2(i, j);
        //         if (a1 != a2) {
        //             System.out.println(i + " " + j + " " + a1 + " " + a2);
        //         }
        //     }
        // }

    }

    long gcd(long a, long b) {
        if (a == 0) return b;
        return gcd(b%a, a);
    }
    long go2(long a, long b) {
        if (b == 0) return 0;
        return 1+go2(a, b-gcd(a,b));
    }

    long go(long a, long b) {
        // System.out.println(a + " " + b);
        if (b == 0) return 0;

        long closest = 0;
        long minDiff = b;
        long div = 1;

        for (long p = 1; p*p <= a && p <= b; p++) {
            if (a%p != 0) continue;

            long q = p;
            for (int rep = 0; rep < 2; rep++) {
                long g = (b/q)*q;
                long diff = b-g;

                if (q != 1) {
                    if (diff < minDiff || (diff == minDiff && div < q)) {
                        minDiff = diff;
                        closest = g;
                        div = q;
                    }
                }

                q = a/q;
            }
        }

        // System.out.println("closest: " + closest);
        // System.out.println("minDiff: " + minDiff);
        // System.out.println("div: " + div);
        // System.out.println();

        // bring it to closest
        long add = b-closest;
        return add + go(a/div, closest/div);
    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
            st = null;
        }

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