import java.util.*; class Main { static void myAssert(boolean e){ if(!e)throw new RuntimeException("assertion sippai"); } static boolean isPrime(long x){ if(x<=1)return false; for(long i=2;i*i<=x;++i)if(x%i==0)return false; return true; } static long powerMod(long x, long exponent,long m) { long prod = 1; for (int i = 63; i >= 0; --i) { prod = (prod * prod) % m; if ((exponent & 1L << i) != 0) { prod = (prod * x) % m; } } return prod; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); long n=scan.nextLong(); long m=scan.nextLong(); myAssert(n>=1); myAssert(n<=1000000000000000000L); myAssert(m>=2); myAssert(m<=1100000000); myAssert(isPrime(m)); long p=m==2||m%12==1||m%12==11?m-1:m+1; long e=powerMod(2,n,p); long x=1,y=0; for(int i=31;i>=0;--i){ long z=(x*x+3*y*y)%m,w=2*x*y%m; x=z;y=w; if((e&1L<