#include #include void swap(long long int *a, long long int *b) { long long int temp; temp = *a; *a = *b; *b = temp; } int lcm(long long int m, long long int n) { long long int temp; if(m < n)swap(m, n); while(n != 0){ temp = n; n = m % n; m = temp; } return m; } int main(void) { int i; int pos; int foot[20] = {0}; long long int n, d; int cnt; /* for(n = 1;n < 10;n++){ for(d = 1;d <= n;d++){ printf("n = %lld, d = %lld : ", n, d); memset(foot, (int)0, sizeof(foot)); pos = 1; cnt = 0; while(foot[pos] != 1){ printf("%d ", pos); foot[pos] = 1; cnt++; pos = (pos - 1 + d) % n + 1; } printf(" %d times\n", cnt - 1); } printf("\n"); } */ //lcm(8, 6); scanf("%lld%lld", &n, &d); if(n == d)printf("0\n"); else if(d == 1 || n - 1 == d)printf("%lld\n", n - 1); else if(lcm(n, d) == 1)printf("%lld\n", n - 1); else { printf("%lld\n", n / lcm(n, d) - 1); } return 0; }