#include <bits/stdc++.h>
using namespace std;
bool isPrime[1000005];
vector<long long> fact;
void sieve_fac(long long a) {
  memset(isPrime, 1, sizeof(isPrime));
  isPrime[0] = isPrime[1] = 0;
  for (long long i = 2; i <= sqrt(a); i++) {
    if (!isPrime[i]) continue;
    bool flag = 0;
    while (a % i == 0) {
      a /= i;
      flag = 1;
    }
    if (flag) fact.push_back(i);
    for (long long j = i * i; j <= sqrt(a); j += i) isPrime[j] = 0;
  }
  if (a != 1) fact.push_back(a);
  return;
}
long long gcd(long long a, long long b) {
  return ((b == 0) ? a : gcd(b, a % b));
}
int main() {
  long long a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
  scanf("%lld %lld", &a, &b);
  sieve_fac(a);
  c = 0;
  while (b > 0) {
    d = gcd(a, b);
    g = 1000000000000000000;
    if (a % b == 0) {
      c++;
      break;
    } else if (b % a == 0) {
      c += b / a;
      break;
    }
    for (auto p : fact) {
      if (a % (d * p) != 0) continue;
      if (b % (d * p) != 0)
        e = b / (d * p);
      else
        e = b / (d * p) - 1;
      f = (b - e * (d * p)) / d;
      if (f < g) g = f;
    }
    c += g;
    b -= g * d;
  }
  printf("%lld\n", c);
  return 0;
}
