#include #include using namespace std; int solve(int P, int N) { if (P == 2) return 0; int B = 0; while (1LL * B * B < P) ++B; int pos = 0; for (int i = 1; i <= B; ++i) { int val = 1LL * i * N % P; if (val <= B) { pos = i; break; } else if (val >= P - B) { return solve(P, P - N) ^ (1LL * (P - 1) * (P - 2) / 2 % 2); } } if (pos == -1) return -1; int delta = 1LL * pos * N % P; int ans = 0; for (int i = 0; i < pos; ++i) { long long ini = 1LL * i * N % P; for (int j = 0; j < 3; ++j) { long long L = 1LL * j * P + (P + 1LL) / 2, R = 1LL * j * P + P; long long pl = max(L - ini + delta - 1, 0LL) / delta * pos + i, pr = max(R - ini + delta - 1, 0LL) / delta * pos + i; if (pl >= (P + 1) / 2) pl = 1LL * ((P + 1LL) / 2 + pos - 1 - i) / pos * pos + i; if (pr >= (P + 1) / 2) pr = 1LL * ((P + 1LL) / 2 + pos - 1 - i) / pos * pos + i; ans += (pr - pl) / pos; } } return ans % 2; } int main() { int P, N; cin >> P >> N; int ans = solve(P, N); cout << ans << endl; return 0; }