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