#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > a;
long long binr(long long x, long long y) {
  long long ans = 0, l = 0, r = LLONG_MAX / x;
  while (r - l > 1) {
    long long m = (l + r) / 2;
    if (x * m <= y)
      l = m;
    else
      r = m;
  }
  return l;
}
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
  long long x = a.first * a.second, y = b.first * b.second;
  return (x == y ? a.second > b.second : x > y);
}
int main() {
  long long x, y;
  cin >> x >> y;
  for (long long i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      a.push_back(make_pair(0LL, i));
      if (i * i != x) a.push_back(make_pair(0LL, x / i));
    }
  }
  for (int i = 0; i < a.size(); i++) a[i].first = binr(a[i].second, y);
  sort(a.begin(), a.end(), comp);
  vector<pair<long long, long long> > b;
  b.clear();
  for (int i = 0; i < a.size(); i++)
    if (i == 0 || a[i].first * a[i].second != a[i - 1].first * a[i - 1].second)
      b.push_back(a[i]);
  long long ans = 0, l = 0, r = 1;
  while (r < b.size()) {
    long long sum = (b[l].first * b[l].second), sum2 = b[r].first * b[r].second;
    if ((sum - sum2) % b[l].second)
      r++;
    else
      ans += (sum - sum2) / b[l].second, l = r, r++;
  }
  ans += b[l].first;
  cout << ans;
  return 0;
}
