#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  while (a > 0 && b > 0) {
    if (a > b)
      a %= b;
    else
      b %= a;
  }
  return (a == 0 ? b : a);
}
int main() {
  long long a, b;
  cin >> a >> b;
  bool bad = false;
  for (long long i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      bad = true;
      break;
    }
  }
  if (!bad) {
    if (b > a) {
      cout << (b % a) + (b / a) << endl;
    } else {
      cout << b << endl;
    }
    return 0;
  }
  long long ans = 0;
  while (b % a != 0) {
    long long g = gcd(a, b);
    if (g != 1) {
      a /= g;
      b /= g;
      bool bad = false;
      for (long long i = 2; i * i <= a; i++) {
        if (a % i == 0) {
          bad = true;
          break;
        }
      }
      if (!bad) {
        if (b > a) {
          cout << ans + (b % a) + (b / a) << endl;
        } else {
          cout << ans + b << endl;
        }
        return 0;
      }
    }
    b--;
    ans++;
  }
  ans += b / a;
  cout << ans << endl;
  return 0;
}
