#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
vector<long long> find_div(long long x) {
  vector<long long> d;
  for (long long i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      d.push_back(i);
      if (i * i != x) {
        d.push_back(x / i);
      }
    }
  }
  sort(d.begin(), d.end());
  return d;
}
int main() {
  ios_base::sync_with_stdio(false);
  long long a, b;
  cin >> a >> b;
  long long ans = 0;
  while (1) {
    long long g = gcd(a, b);
    long long _a = a / g, _b = b / g;
    vector<long long> d = find_div(_a);
    long long best = 0;
    for (auto &each : d) {
      if (each == 1) {
        continue;
      }
      if (((_b / each) * each) * g < b) {
        best = max(best, ((_b / each) * each) * g);
      }
    }
    if (best > 0) {
      long long diff = b - best;
      ans += diff / g;
      b = best;
    } else {
      ans += b / g;
      break;
    }
  }
  cout << ans << '\n';
  return 0;
}
