#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
long long n = 0, a = 0, phi, p, tmp, lena, lenn, f = 0;
char s[maxn], ss[maxn];
long long qpow(long long a, long long k) {
  long long res = 1;
  while (k) {
    if (k & 1) res = res * a % p;
    a = a * a % p;
    k >>= 1;
  }
  return res;
}
int main() {
  scanf("%s%s%lld", s + 1, ss + 1, &p);
  lena = strlen(s + 1);
  lenn = strlen(ss + 1);
  phi = tmp = p;
  a = n = 0;
  for (long long i = 2; i * i <= tmp; i++)
    if (tmp % i == 0) {
      phi -= phi / i;
      while (tmp % i == 0) tmp /= i;
    }
  if (tmp > 1) phi -= phi / tmp;
  for (int i = 1; i <= lena; i++) a = (a * 10 + s[i] - '0') % p;
  for (int i = lenn; i >= 1; i--)
    if (ss[i] == '0')
      ss[i] = '9';
    else {
      ss[i]--;
      break;
    }
  for (int i = 1; i <= lenn; i++) {
    n = n * 10 + ss[i] - '0';
    if (n >= phi) f = 1;
    n %= phi;
  }
  if (f) n += phi;
  long long ans = ((a - 1) * qpow(a, n) % p + p) % p;
  if (ans == 0) ans = p;
  printf("%lld\n", ans);
  return 0;
}
