#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long n, long long m) {
  long long r = 1;
  while (n) {
    if (n & 1) r = r * x % m;
    n >>= 1;
    x = x * x % m;
  }
  return r;
}
char b[1100000], n[1100000];
int c;
int main() {
  scanf(" %s %s %d", b, n, &c);
  long long x = 1, bb = 0;
  for (int i = 0; b[i]; i++) bb = (10 * bb + b[i] - '0') % c;
  int pos = 0;
  while (n[pos]) pos++;
  pos--;
  while (n[pos] == '0') {
    n[pos] = '9';
    pos--;
  }
  n[pos]--;
  for (int i = 0; n[i]; i++) {
    x = power(x, 10, c) * power(bb, n[i] - '0', c) % c;
  }
  x = x * (bb + c - 1) % c;
  if (!x) x = c;
  printf("%d\n", (int)x);
}
