#include <bits/stdc++.h>
using namespace std;
long long bmc;
long long ans, c;
int lenb, lenn;
char b[1000005], n[1000005];
long long p(long long base, int times) {
  long long res = 1;
  for (int i = 0; i < times; ++i) res = res * base % c;
  return res;
}
int main(void) {
  scanf("%s %s %I64d", b, n, &c);
  lenb = strlen(b);
  lenn = strlen(n);
  for (int i = 0; i < lenb; ++i) bmc = (bmc * 10 + b[i] - '0') % c;
  for (int i = lenn - 1; i >= 0 && n[i]-- == '0'; n[i--] = '9')
    ;
  ans = 1;
  for (int i = (n[i] != '0' ? 0 : 1); i < lenn; ++i)
    ans = p(ans, 10) * p(bmc, n[i] - '0') % c;
  ans = ans * (bmc - 1 + c) % c;
  printf("%d\n", (ans + c - 1) % c + 1);
  return 0;
}
