import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np U = int(10 ** 4) phi = np.arange(U) for p in range(2, U): if phi[p] == p: phi[::p] -= phi[::p] // p def f(A, N, M): if A == 1: return 1 if N == 0: return 1 if M == 1: return 1 MM = int(phi[M]) x = f(A, N - 1, MM) if x >= MM or x >= 20: return M + pow(A, x, M) x = A ** x if x >= M: return (x % M) + M return x A, N, M = map(int, read().split()) print(f(A, N, M) % M)