- 题意:求(b-1)b^(n-1)%mod 2<=b<=10(106),1<=n<=10(106),1<=c<=10^9;
//(b-1)*b^(n-1)%mod
//2<=b<=10^(10^6),1<=n<=10^(10^6),1<=c<=10^9;
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1000000+10;
ll get_phi(ll x)
{
ll res=x;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
res=res-res/i;
while(x%i==0)
x/=i;
}
}
if(x>1)res=res-res/x;
return res;
}
char b[N],n[N];
ll c;
ll quick(ll m,ll n,ll mod)
{
ll res=1;
while(n)
{
if(n&1)res=res*m%mod;
m=m*m%mod;
n>>=1;
}
return res;
}
ll cal(char *s,ll k)
{
ll res=0;
for(int i=0;s[i];i++)
{
res=(res*10LL+s[i]-'0')%k;
}
return res;
}
int main()
{
while(~scanf("%s%s%d",b,n,&c))
{
ll A=cal(b,c);
ll d=get_phi(c);
bool mark=false;
ll ans=0;
for(int i=0;n[i];i++)
{
ans=(ans*10LL+n[i]-'0');
if(ans>d)
mark=true,ans%=d;
}
ans--;
ll res=0;
if(mark)
{
res=quick(A,ans+d,c);
}
else
{
res=quick(A,ans,c);
}
ll sum=(A-1+c)%c;
sum=sum*res%c;
if(sum==0)sum=c;
printf("%lld\n",sum);
}
return 0;
}