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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main implements Runnable {
	public static void main(String[] args) {
		new Thread(null, new Main(), "", Runtime.getRuntime().maxMemory()).start();
		// new Main().run();
	}

	ArrayList<Long> div = new ArrayList<>();

	long solve(long a, long b) {
		div.clear();
		div.add(a);
		long a_ = a;
		for (long v = 2; v * v <= a_; ++v)
			if (a_ % v == 0) {
				div.add(v);
				if (v != a_ / v)
					div.add(a_ / v);
				while (a_ % v == 0) {
					a_ /= v;
				}
			}
		Collections.sort(div);
		return f(a, b);

	}

	public void run() {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		System.out.println(solve(a, b));
	}

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

	long f(long a, long b) {
		if (a == b)
			return 1;
		if (b == 0) {
			return 0;
		}
		if (a == 1) {
			return b;
		}
		if (gcd(a, b) > 1) {
			long gcd = gcd(a, b);
			return f(a / gcd, b / gcd - 1) + 1;
		}
		long nb = 1;
		for (long i : div) {
			if (i > a)
				break;
			if (a % i == 0)
				nb = Math.max(nb, b / i * i);
		}
		if (b > a && nb < a) {
			nb = a;
		}
		// f(3,2)=f(3,1)+1=f(3,0)+2
		long c = 1 + (b - nb);
		long gcd = gcd(a, nb);
		return c + f(a / gcd, nb / gcd - 1);
	}

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

	static boolean isPrime(long n) {
		// 間違える確率4^(-k)=10^(-6)
		if (n == 1 || n % 2 == 0)
			return false;
		long z = n - 1;
		int c2 = 0;
		while (z % 2 == 0) {
			z /= 2;
			++c2;
		}
		outer: for (int k = 0; k < 10; ++k) {
			long a = (int) (Math.random() * 10000) % n;
			if (a == 0)
				continue;
			a = pow(a, z, n);
			if (a == 1 || a == n - 1)
				continue outer;
			for (int i = 0; i < c2 - 1; ++i) {
				a = (a * a) % n;
				if (a == n - 1)
					continue outer;
			}
			return false;
		}
		return true;
	}

	static long pow(long a, long n, long mod) {
		long ret = 1;
		for (; n > 0; n >>= 1, a = (a * a) % mod) {
			if (n % 2 == 1) {
				ret = (ret * a) % mod;
			}
		}
		return ret;
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}