#include <bits/stdc++.h>
using namespace std;
const int M = 1e6 + 7;
inline long long stoll(char* s, const long long& p, bool f = 0) {
  long long a = 0;
  bool fl = 0;
  for (; *s; ++s) a = a * 10 + *s - 48, fl |= (f && a >= p), a %= p;
  return a + fl * p;
}
long long b, n, p, phi;
char B[M], N[M];
inline long long fp(long long x, long long k, const long long& p) {
  long long r = 1;
  for (; k; k >>= 1, x = x * x % p)
    if (k & 1) r = r * x % p;
  return r;
}
inline long long getphi(long long n) {
  long long r = n, nn = n;
  for (long long i = 2; i * i <= nn; ++i)
    if (!(n % i))
      for (r = r / i * (i - 1); !(n % i); n /= i)
        ;
  if (n > 1) r = r / n * (n - 1);
  return r;
}
inline long long mod(long long x, long long p) {
  return x < p ? x : x % p + p;
};
inline char* N_1() {
  register int s = strlen(N) - 1;
  for (; s >= 0 && !N[s]; --s)
    ;
  --N[s];
  return N;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> B >> N >> p;
  phi = getphi(p);
  b = stoll(B, p), n = stoll(N_1(), phi, 1);
  long long ans = ((b - 1) * fp(b, n, p) + p) % p;
  if (!(ans)) ans = p;
  cout << ans;
  return 0;
}
