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