#include using namespace std; int M_MAX = 2100; int c(int a, int m) { a %= m; set s; int tmp = a; while (!s.count(tmp)) { s.insert(tmp); (tmp *= a) %= m; } return (int)s.size(); } int modpow(int a, int b, int m) { a %= m; int ret = 1; while (b) { if (b & 1) (ret *= a) %= m; b >>= 1; (a *= a) %= m; } return ret; } int tetration(int a, int b, int m) { if (m == 1) return 0; else if (b == 0) return 1; else if (b == 1) return a % m; else if (b == 2) return modpow(a, a, m); else if (b == 3 && a == 2) return 16 % m; else return modpow(a, M_MAX + (tetration(a, b - 1, c(a, m)) - M_MAX) % c(a, m), m); } int main() { int A, N, M; cin >> A >> N >> M; cout << tetration(A, N, M) << endl; }