def primeFactor(N): i, n, ret, d, sq = 2, N, {}, 2, 99 while i <= sq: k = 0 while n % i == 0: n, k, ret[i] = n//i, k+1, k+1 if k > 0 or i == 97: sq = int(n**(1/2)+0.5) if i < 4: i = i * 2 - 1 else: i, d = i+d, d^6 if n > 1: ret[n] = 1 return ret # Euler's Totient Function def ETF(N): pf = primeFactor(N) a = 1 for p in pf: a *= (p-1) * (p ** (pf[p] - 1)) return a def calc(a, n, m): if n == 0: return 1 if m == 1: return 0 s = calc(a, n - 1, ETF(m)) if s <= 50: return a ** s return pow(a, s, m) + m * 50 A, N, M = map(int, input().split()) print(calc(A, N, M) % M)