import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) def phi(n): if n == 1: return 1 res = n for i in range(2,int(n**.5)+1): if n % i == 0: res -= res//i while n % i == 0: n //= i if n == 1: break else: res -= res//n return res def tetration(A,B,M): # A^(A^(A^(...))) (mod M) if M == 1: return 0 if A == 0: return (B+1) % 2 if B == 0: return 1 if B == 1: return A % M if B == 2: return pow(A,A,M) f = phi(M) e = tetration(A,B-1,f) if e == 0: e = f return pow(A,e,M) A,N,M = MI() ans = tetration(A,N,M) print(ans)