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
#include <bits/stdc++.h>
using namespace std;
long long llgcd(long long a, long long b) { return (b ? llgcd(b, a % b) : a); }
long long f(long long a, long long b) {
  if (b == 0) return 0;
  long long tmp = 1 + f(a, b - llgcd(a, b));
  return tmp;
}
struct _fac {
  long long prime;
  long long count;
};
bool isprime[1000006];
vector<long long> primelist;
void seive() {
  for (long long i = 2; i <= 1000000; i++) isprime[i] = true;
  for (long long i = 2; i <= 1000; i++) {
    if (!isprime[i]) continue;
    for (long long j = 2 * i; j <= 1000000; j += i) {
      isprime[j] = false;
    }
  }
  for (long long i = 2; i <= 1000000; i++) {
    if (isprime[i]) primelist.push_back((long long)i);
  }
}
vector<_fac> factorize_result;
void factorize(long long a) {
  factorize_result.clear();
  for (long long i = 0; i < primelist.size(); i++) {
    long long count = 0;
    while (a % primelist[i] == 0) {
      count++;
      a /= primelist[i];
    }
    if (count > 0) {
      _fac tmp;
      tmp.count = count;
      tmp.prime = primelist[i];
      factorize_result.push_back(tmp);
    }
  }
  if (a > 1) {
    factorize_result.push_back(_fac{a, 1});
  }
}
long long solve(long long a, long long b) {
  long long index = -1;
  long long min_diff = b;
  for (long long i = 0; i < factorize_result.size(); i++) {
    if (factorize_result[i].count > 0 &&
        b % factorize_result[i].prime < min_diff) {
      index = i;
      min_diff = b % factorize_result[i].prime;
    }
  }
  if (index == -1) {
    return b;
  } else {
    factorize_result[index].count--;
    return solve(a / factorize_result[index].prime,
                 b / factorize_result[index].prime) +
           min_diff;
  }
}
int32_t main() {
  long long a, b;
  cin >> a >> b;
  seive();
  factorize(a);
  cout << solve(a, b);
  return 0;
}
