#include <bits/stdc++.h>
long long d[30003];
int c = 0, tc;
int main() {
  long long a, b, t, x, ans = 0, m;
  int i, k, p;
  scanf("%lld%lld", &a, &b);
  k = sqrt(a);
  for (i = 1; i <= k; i++) {
    if (a % i == 0) d[c++] = (long long)i;
  }
  i = c - 1;
  if ((long long)k * (long long)k == a) i--;
  for (; i >= 0; i--) {
    d[c++] = a / d[i];
  }
  for (i = c - 1; b % d[i]; i--)
    ;
  t = d[i];
  for (tc = 0; i < c; i++) {
    if (d[i] % t == 0) d[tc++] = d[i];
  }
  c = tc, x = b;
  while (x) {
    if (c == 1) {
      ans += x / d[0];
      break;
    }
    for (i = c - 1, m = -1; i; i--) {
      t = x / d[i] * d[i];
      if (t > m) m = t, p = i;
    }
    ans += (x - m) / d[0];
    x = m, t = d[p];
    for (tc = 0, i = p; i < c; i++) {
      if (d[i] % t == 0) d[tc++] = d[i];
    }
    c = tc;
  }
  printf("%lld\n", ans);
  return 0;
}
