import math P, N = map(int, input().split()) if P == 2: print(0) else: d = math.gcd(N, P-1) ans = (P - 1 - d) % 2 print(ans)