#include <bits/stdc++.h>
using namespace std;
char sb[2000005], sn[2000005];
long long m, b, cb, r, f;
int main() {
  int i, j, k, lb, ln;
  scanf("%s%s%d", sb, sn, &k);
  m = (long long)k;
  b = 0;
  lb = strlen(sb);
  ln = strlen(sn);
  for (i = 0; i < lb; i++) {
    j = sb[i] - '0';
    b = (b * 10 + j) % m;
  }
  for (i = ln - 1; sn[i] == '0'; i--) {
  }
  sn[i]--;
  for (i++; i < ln; i++) sn[i] = '9';
  for (cb = b, r = 1, i = ln - 1; i >= 0; i--) {
    j = sn[i] - '0';
    for (k = 0; k < j; k++) r = (r * cb) % m;
    f = cb;
    cb = 1;
    for (k = 0; k < 10; k++) cb = (cb * f) % m;
  }
  r = (r * (b - 1 + m)) % m;
  if (r == 0) r = m;
  cout << r << endl;
  return 0;
}
