longlongread(const std::string &s, longlong p){ longlong ans = 0; int len = s.length(); for (int i = 0; i < len; i++) { ans = ans * 10 + s[i] - '0'; ans %= p; } return ans; }
longlongphi(longlong a){ longlong ans = a; for (longlong i = 2; i * i <= a; i++) { if (a % i == 0) { ans = ans / i * (i - 1); while (a % i == 0) a /= i; } } if (a > 1) ans = ans / a * (a - 1); return ans; }
longlongpow(longlong a, longlong k, longlong p){ longlong ans = 1; for (; k; k >>= 1, a = a * a % p) if (k & 1) ans = ans * a % p; return ans; }
std::string bIn, nIn; longlong c; std::cin >> bIn >> nIn >> c;
longlong b = read(bIn, c), n = read(nIn, 1e9), p = phi(c); longlong k; if (n > p || nIn.length() > 9) k = (read(nIn, p) - 1 + p) % p + p; else k = n - 1; longlong ans = (b - 1 + c) % c * pow(b, k, c) % c;