欧拉函数….
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int p;
int Fai(int x)
{
int t=sqrt(x),res=1;
for(int i=2;i<=t&&i<=x;i++)
if(x%i==0)
{
x/=i;
res*=i-1;
while(x%i==0)
x/=i,res*=i;
}
if(x!=1)res*=x-1;
return res;
}
int Pow(int a,int x,int M)
{
int res=1;
for(;x;x>>=1,a=a*1ll*a%M)
if(x&1)res=res*1ll*a%M;
return res;
}
int Solve(int A)
{
int de=1,d=0;
while(A%p==0)d++,A/=p,de*=p;
if(A==1)
return 0;
int t=Fai(A),dd,iv=Pow(p,t-1,A);
dd=Solve(Fai(A));
return de*((Pow(iv,d,A)*1ll*Pow(p,dd,A))%A);
}
int main()
{
int m;
int f=1;
while(scanf("%d%d",&p,&m)==2)
{
if(!f)puts("");
f=0;
int t=m;
while(--m)t*=m;
int A=Solve(t);
printf("%d\n",A);
}
return 0;
}