1 /****************************************************
2
3 Author : Coolxxx
4 Copyright 2017 by Coolxxx. All rights reserved.
5 BLOG : http://blog.csdn.net/u010568270
6
7 ****************************************************/
8 #include<bits/stdc++.h>
9 #pragma comment(linker,"/STACK:1024000000,1024000000")
10 #define abs(a) ((a)>0?(a):(-(a)))
11 #define lowbit(a) (a&(-a))
12 #define sqr(a) ((a)*(a))
13 #define mem(a,b) memset(a,b,sizeof(a))
14 const double EPS=1e-8;
15 const int J=10;
16 const int MOD=100000007;
17 const int MAX=0x7f7f7f7f;
18 const double PI=3.14159265358979323;
19 const int N=1004;
20 const int M=1004;
21 using namespace std;
22 typedef long long LL;
23 double anss;
24 LL aans;
25 int cas,cass;
26 LL n,m,lll,ans;
27 LL euler(LL x)
28 {
29 LL res=x;
30 int i;
31 for(i=2;1LL*i*i<=x;i++)
32 {
33 if(x%i==0)
34 {
35 res=res/i*(i-1);
36 while(x%i==0)x/=i;
37 }
38 }
39 if(x>1)res=res/x*(x-1);
40 return res;
41 }
42 LL mi(LL x,LL y,LL mod)
43 {
44 LL s=1;
45 while(y)
46 {
47 if(y&1)s=(s*x)%mod;
48 y/=2;
49 x=(x*x)%mod;
50 }
51 return s;
52 }
53 LL cal(LL a,LL c)
54 {
55 LL e=euler(c);
56 if(a==3 && e>9)return 9%c;
57 if(a==2 && e>2)return 2%c;
58 if(a==1)return 1;
59 if(c==1)return 0;
60 LL b=cal(a-1,e);
61 return (1LL*mi(a,b,c)*mi(a,e,c))%c;
62 }
63 int main()
64 {
65 #ifndef ONLINE_JUDGE
66 freopen("1.txt","r",stdin);
67 // freopen("2.txt","w",stdout);
68 #endif
69 int i,j,k;
70 int x,y,z;
71 // for(scanf("%d",&cass);cass;cass--)
72 // for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
73 // while(~scanf("%s",s))
74 while(~scanf("%lld",&n))
75 {
76 scanf("%lld",&m);
77 printf("%lld\n",cal(n,m));
78 }
79 return 0;
80 }
81 /*
82 //
83
84 //
85 */