N,M=map(int,input().split()) a=1 for i in range(1,N): a=a**i%M print(a)