#include <bits/stdc++.h>
using namespace std;
char yam[1 << 23];
int main() {
  long long b, n, c;
  gets(yam);
  char *bp = yam;
  char *np = strchr(bp, ' ') + 1;
  char *cp = strchr(np, ' ') + 1;
  c = atoi(cp);
  for (b = 0; isdigit(*bp); bp++) {
    b = (10 * b + (*bp - '0')) % c;
  }
  long long res = (b - 1 + c) % c;
  int carry = 1;
  for (np = cp - 2; isdigit(*np); np--) {
    int cur = *np - '0' - carry;
    if (cur < 0) {
      cur += 10;
      carry = 1;
    } else
      carry = 0;
    while (cur--) {
      res = (res * b) % c;
    }
    long long oldb = b;
    for (int i = 0; i < 9; i++) {
      b = (b * oldb) % c;
    }
  }
  cout << (res ?: c);
  return 0;
}
