#include using namespace std; typedef long long ll; ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int main() { ll n, d; cin >> n >> d; ll l = lcm(n, d); cout << l / d - 1 << endl; return 0; }