#include <bits/stdc++.h>
using namespace std;
vector<long long> vec;
int main() {
  long long x, y;
  cin >> x >> y;
  for (long long i = 1; i <= sqrt(x); i++) {
    if (x % i == 0) {
      vec.push_back(i);
      if (i != x / i) vec.push_back(x / i);
    }
  }
  sort(vec.begin(), vec.end(), greater<long long>());
  long long ans = 0;
  int idx = 0;
  while (y) {
    for (int i = 0; i < vec.size(); i++) {
      if (y % vec[i] == 0) {
        idx = i;
        break;
      }
    }
    long long mn = y;
    for (int i = 0; i < idx; i++) {
      if ((y % vec[i]) % vec[idx] == 0) mn = min(y % vec[i], mn);
    }
    ans += mn / vec[idx];
    y -= mn / vec[idx] * vec[idx];
  }
  cout << ans << "\n";
  return 0;
}
