import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines P, N = map(int, read().split()) if pow(N, P//2, P) == 1: print(0) else: print(1)