#include using namespace std; int main(){ function rec = [&](int V, int N, int MOD){ if(N == 0)return 1; if(MOD == 1)return 0; vector order, tb(MOD, -1); int val = 1, tim = 0; while(tb[val] == -1){ tb[val] = tim++; order.push_back(val); (val *= V % MOD )%= MOD; } int EXP = rec(V, N - 1, tim - tb[val]); if(EXP < tim)return order[EXP]; return order[(EXP - tb[val]) % (tim - tb[val]) + tb[val]]; }; int A, N, M; cin >> A >> N >> M; cout << rec(A, N, M) << endl; }