#include <bits/stdc++.h>
using namespace std;
long long a, b, c, Minus, tmp, pos, lar_pos, ans;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long f(long long a, long long b) {
  if (b == 0)
    return 0ll;
  else
    return 1ll + f(a, b - gcd(a, b));
}
vector<long long> v, base;
int main() {
  scanf("%lld %lld", &a, &b);
  c = a;
  for (long long i = 2; i * i <= c; i++) {
    if (a % i == 0ll) v.push_back(i);
    while (a % i == 0ll) a /= i;
  }
  if (a != 1ll) v.push_back(a);
  for (int i = (0); i < (v.size()); i++) base.push_back(v[i]);
  a = c;
  while (b > 0) {
    Minus = gcd(a, b);
    for (int i = (0); i < (v.size()); i++)
      while (Minus % v[i] == 0) v[i] *= base[i];
    lar_pos = 0ll;
    for (int i = (0); i < (v.size()); i++) {
      if (a % v[i] != 0ll) continue;
      tmp = lcm(Minus, v[i]);
      pos = b / tmp * tmp;
      lar_pos = max(pos, lar_pos);
    }
    ans += (b - lar_pos) / Minus;
    b = lar_pos;
  }
  printf("%lld\n", ans);
  return 0;
}
