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.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.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;

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

    static class TaskE {
        int[] primes;

        public void solve(int testNumber, InputReader in, FastWriter out) {
            primes = NumberTheory.primes(2000_000);
            //test(1000, 1000_000);
            long a = in.nextLong();
            long b = in.nextLong();
            out.println(solveTask(a, b));
        }

        private long solveTask(long a, long b) {
            if (b == 0) return 0;
            if (isPrime(a))
                return ifAPrime(a, b);
            long res = 0L;
            long d = gcd(a, b);
            for (; d == 1L; d = gcd(a, b)) {
                res++;
                b--;
            }
            a /= d;
            b /= d;
            return res + solveTask(a, b);
        }

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

        private boolean isPrime(long a) {
            for (int i = 0; i < primes.length && primes[i] * primes[i] <= a; ++i)
                if (a % primes[i] == 0) return false;
            return true;
        }

        private long ifAPrime(long a, long b) {
            if (a == b) return 1L;
            if (a > b) return b;
            return b % a + b / a;
        }

    }

    static class InputReader extends BufferedReader {
        StringTokenizer tokenizer;

        public InputReader(InputStream inputStream) {
            super(new InputStreamReader(inputStream), 32768);
        }

        public InputReader(String filename) {
            super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
        }

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

        public Long nextLong() {
            return Long.valueOf(next());
        }

    }

    static class FastWriter extends PrintWriter {
        public FastWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public FastWriter(Writer writer) {
            super(writer);
        }

        public FastWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

    }

    static class NumberTheory {
        public static int[] primes(int nInclusive) {
            boolean[] prime = new boolean[++nInclusive];
            Arrays.fill(prime, 2, prime.length, true);
            int count = 0;
            int sqrtN = (int) Math.sqrt(nInclusive) + 1;
            for (int i = 2; i < sqrtN; i++)
                if (prime[i]) {
                    count++;
                    for (int j = i * i; j < nInclusive; j += i)
                        prime[j] = false;
                }
            for (int i = sqrtN; i < nInclusive; ++i)
                if (prime[i])
                    count++;

            int[] a = new int[count];
            for (int i = 2, j = 0; i < nInclusive; i++)
                if (prime[i])
                    a[j++] = i;

            return a;
        }

    }
}