#include <bits/stdc++.h>
const int MAXL = 1000010 + 1;
int b, MOD;
char B[MAXL], N[MAXL];
inline int powMod(int a, int b) {
  int ret = 1, mul = a;
  for (; b; b >>= 1, mul = (long long)mul * mul % MOD) {
    if (b & 1) {
      ret = (long long)ret * mul % MOD;
    }
  }
  return ret % MOD;
}
int main() {
  scanf("%s%s%d", B, N, &MOD);
  int lB = (int)strlen(B), lN = (int)strlen(N);
  for (int i = 0; i < lB; i++) {
    b = (b * 10LL + B[i] - '0') % MOD;
  }
  for (int i = lN - 1; i >= 0; i--) {
    if (N[i] != '0') {
      N[i]--;
      break;
    }
    N[i] = '9';
  }
  int ret = b ? b - 1 : MOD - 1;
  for (int i = lN - 1; i >= 0; i--) {
    ret = (long long)ret * powMod(b, N[i] - '0') % MOD;
    b = powMod(b, 10) % MOD;
  }
  ret %= MOD;
  printf("%d\n", ret ? ret : MOD);
}
