#coding=utf-8 import time import math from fractions import gcd def primes(n): return list(i_primes(n)) def i_primes(n): p = [True] * (n+1) p[0] = p[1] = False max = int(math.sqrt(n+1)) for i in xrange(2,max): if p[i]: yield i for j in xrange(i*2,n+1,i): p[j] = False for i in xrange(max,n+1): if p[i]: yield i pr = primes(10**5) def euler_phi(n): ret = n for i in pr: if i*i > n: break if n % i == 0: while n%i==0: n/=i ret = ret/i*(i-1) if n != 1: ret = ret/n*(n-1) return ret def p_factor(n): ret = [] for p in pr: if p * p > n: break if n % p == 0: ret.append(p) while n % p == 0: n /= p return ret def tetration(a,b,mod): if b == 0: return 1 return pow(a,tetration(a,b-1,euler_phi(mod)),mod) def tetration2(a,b,mod): if b == 0 or a == 1: return 1 ret = 1 while ret < 20 and b > 0: ret = pow(a,ret) b -= 1 if b == 0: return ret g = gcd(a,mod) g_factor = p_factor(g) a_rem = a a_div = 1 for gf in g_factor: while a_rem % gf == 0: a_rem /= gf a_div *= gf dp = [mod] for i in xrange(1,b): dp.append(euler_phi(dp[i-1])) if dp[-1] == 1: break for i in reversed(dp): ret = pow(a_rem,ret,i) return ret * a_div % mod def main(): a,n,m = map(int,raw_input().split()) print tetration2(a,n,m) % m main()