#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; vector v; long long temp = 1; do{ v.push_back((int)temp); temp *= A; temp %= M; } while (temp != 1); return v[dfs(A, N - 1, v.size()) % M]; } int main() { int A, N, M; cin >> A >> N >> M; cout << dfs(A, N, M) << endl; }