#include <bits/stdc++.h>
int main() {
  static char bb[1000000 + 1], nn[1000000 + 1];
  int i, j, lb, ln;
  int a, b, c;
  scanf("%s%s%d", bb, nn, &c);
  lb = strlen(bb), ln = strlen(nn);
  b = 0;
  for (i = 0; i < lb; i++) b = ((long long)b * 10 + (bb[i] - '0')) % c;
  a = (b - 1 + c) % c;
  for (i = ln - 1; i >= 0; i--) {
    if (nn[i] != '0') {
      nn[i]--;
      break;
    }
    nn[i] = '9';
  }
  for (i = ln - 1; i >= 0; i--) {
    int p = 1;
    for (j = 0; j < nn[i] - '0'; j++) a = (long long)a * b % c;
    for (j = 0; j < 10; j++) p = (long long)p * b % c;
    b = p;
  }
  printf("%d\n", a == 0 ? c : a);
  return 0;
}
