#include "bits/stdc++.h" using namespace std; int dfs(int A, int N, int M){ if (M <= 1) return 0; if (N == 0) return 1; if (N == 1) { return A; } vector v; if (A % M == 0) return 0; long long temp = 1; map m; bool ok = false; do{ m[temp] = v.size() + 1; v.push_back((int)temp); temp *= A; if (temp >= M * 3001){ temp %= M; temp += M * 3000; ok = true; } if (ok && temp < M * 3000){ temp %= M; temp += M * 3000; } //temp %= M; if (temp == 0) return 0; } while (m[temp] == 0); int loop = v.size() - (m[temp] - 1); int nextminus = (int)v.size() - loop; //cout << nextminus << " " << loop << endl; int r = dfs(A, N - 1, loop); if (r >= nextminus){ r %= loop; if (r < nextminus) r += loop; } //cout << r << endl; return v[r]; } int main() { int A, N, M; cin >> A >> N >> M; cout << dfs(A, N, M) % M << endl; }