#誤解法チェック2 N,M=map(int,input().split()) n=N//3 m=M*(2*n+1) a=1 for i in range(n):a=a*(N-i)//(i+1)%m print(0if N%3 else a//(2*n+1))