import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines """ ・A[n+1] = A[n]^2 + 4 ・B[n] := A[n] + 2 ・B[n+1] = B[n]^2 - 2 ・F_p or F_{p^2} の元 C[n] について、B[n] = C[n] + 1/(C[n]) → B[n+1] = C[n]^2 + 1/(C[n])^2 ・4 = x + (1/x) となる x をとり、C[n+1] = C[n]^2 としていけばよい """ N,MOD = map(int,read().split()) def power(n): # x^n = a + bx mod (x^2 - 4x + 1) if n == 0: return 1,0 a,b = power(n//2) a,b,c = a*a, 2*a*b, b*b # a + bx + cx^2 b += 4*c; a -= c if not n&1: return a%MOD, b%MOD # ax + bx^2 return (-b)%MOD, (a+4*b)%MOD # k = 2^N k = pow(2,N,MOD * MOD - 1) a,b = power(k) # (a + bx_1) + (a + bx_2) answer = 2 * a + b * 4 - 2 answer %= MOD print(answer)