#include <bits/stdc++.h>
using namespace std;
template <typename T>
void printvec(vector<T>& vec) {
  for (int i = 0; i < vec.size(); i++) {
    cout << vec[i] << " ";
  }
  cout << "\n";
}
vector<long long> fac;
long long findgcd(long long x, long long y) {
  if (x > y) swap(x, y);
  if (x == 0)
    return y;
  else
    return findgcd(y % x, x);
}
void factorize(long long num) {
  long long i;
  for (i = 2; i * i <= num; i++) {
    while (num % i == 0) {
      fac.push_back(i);
      num /= i;
    }
  }
  if (num != 1) fac.push_back(num);
}
long long findp(long long x, long long y, long long g) {
  int i;
  long long ans = -1, steps = INT_MAX;
  steps *= INT_MAX;
  for (i = 0; i < fac.size(); i++) {
    if (x % (fac[i] * g) != 0) continue;
    long long req = (y % (fac[i] * g)) / g;
    if (steps >= req) {
      steps = req;
      ans = fac[i];
    }
  }
  return ans;
}
int main() {
  std::ios::sync_with_stdio(false);
  long long x, y;
  cin >> x >> y;
  factorize(x);
  long long g = findgcd(x, y);
  long long ans = 0;
  long long iii = INT_MAX;
  while (iii--) {
    long long p = findp(x, y, g);
    if (p == -1) {
      ans += (y / g);
      break;
    }
    ans += ((y % (p * g)) / g);
    y -= (g * ((y % (p * g)) / g));
    g = findgcd(x, y);
    if (y == 0) break;
  }
  cout << ans;
  return 0;
}
