#include #include using namespace std; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main(int argc, char *argv[]) { int N, D; cin >> N >> D; int ans = 0; D %= N; if (D > 0) { int c = N / D; int d = c * D; if (gcd(N, d) > 1) { N = c; } ans = N - 1; } cout << ans << endl; return 0; }