题目链接:http://codeforces.com/gym/101550/attachments
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll unsigned long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
const int maxn =1e5+5;
const int ub=1e6;
const double e=2.71828;
ll powmod(ll x,ll y,ll mod){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
欧拉降幂公式:
a^b%p=a^(b%euler(p)+euler(p))%p;
*/
ll n,m;
ll euler(ll x)
{
ll ret=x;
for(int i=2;1LL*i*i<=x;i++)
{
if(x%i==0){
while(x%i==0) x/=i;
ret=ret/i*(i-1);
}
}
if(x>1) ret=ret/x*(x-1);
return ret;
}
ll solve(ll x,ll mod)
{
if(mod==1) return 0LL;
if(x==1) return 1%mod;
if(x==2) return 2%mod;
if(x==3) return 9%mod;
if(x==4) return (1<<18)%mod;
ll eu=euler(mod);
return 1LL*powmod(x,solve(x-1,eu)%eu,mod)*powmod(x,eu,mod)%mod;///精度问题
}
int main()
{
scanf("%lld%lld",&n,&m);
printf("%lld\n",solve(n,m)%m);
return 0;
}