p, n = map(int, input().split()) if p == 2: print(int(n != 1)) else: print(int(pow(n, (p - 1) // 2, p) != 1))