from pip._vendor.distlib.compat import raw_input def find_cycle(a,m): memo ={1:0} p=1 n=1 while (a*p)%m not in memo : p=(a*p)%m memo[p]=n n=n+1 return (memo[(a*p)%m],n-memo[(a*p)%m]) def getval(a,n,s): res=1 for i in range(0,n): res=pow(a,res) if res>s: return -1 return res def modtetration(a,n,m): if m==1: return 0 if a==1 or n==0: return 1 s,T=find_cycle(a,m) g=getval(a,n-1,s) if g>=0: p=pow(a,g,m) else: p= pow(a,s+((modtetration(a, n-1, T)-s+1000*T)%T),m) return p A, N, M=map(int,raw_input().split()) print(modtetration(A, N, M))