#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
long long a, b, ans;
vector<long long> f;
vector<long long> inline factorize(long long a) {
  vector<long long> res;
  for (long long d = 1; d * d <= a; ++d) {
    if (a % d == 0) {
      res.push_back(d);
      if (a / d != d) {
        res.push_back(a / d);
      }
    }
  }
  sort(res.begin(), res.end());
  return res;
}
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
int main() {
  ios::sync_with_stdio(0);
  cin >> a >> b;
  f = factorize(a);
  for (long long g = gcd(a, b); b;) {
    long long mre = inf, ng = g;
    vector<long long> cur = f;
    f.clear();
    for (int i = 0; i < cur.size(); ++i) {
      if (cur[i] % g || cur[i] / g <= 1) continue;
      if (cur[i] / g <= 1) continue;
      long long del = b % cur[i];
      if (mre > del) {
        mre = del;
        ng = cur[i];
      } else if (mre == del && ng < cur[i] && ng <= b) {
        ng = cur[i];
      }
      f.push_back(cur[i]);
    }
    if (mre == inf) {
      ans += b / g;
      break;
    }
    ans += mre / g;
    b -= mre;
    g = ng;
  }
  cout << ans << '\n';
}
