#include using namespace std; using ll = long long; #define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i) #define all(a) (a).begin(), (a).end() int main() { cin.tie(nullptr); ll N, D; cin >> N >> D; ll g = gcd(N, D); cout << N / g - 1<< '\n'; }