#include <bits/stdc++.h>
using namespace std;
int c, ten[1000001];
char b[1000001], n[1000001];
int pw(int n, int k) {
  int ret = 1;
  while (k > 0) {
    if (k & 1) ret = (long long)ret * n % c;
    k >>= 1;
    n = (long long)n * n % c;
  }
  return ret;
}
int main() {
  int i, k, l, B, ret;
  scanf("%s %s %d", b, n, &c);
  B = 0;
  l = strlen(b);
  for (int i = 0; i < l; i++) B = (10LL * B + b[i] - '0') % c;
  l = strlen(n);
  for (i = l - 1;; i--)
    if (n[i] != '0') {
      n[i]--;
      for (k = i + 1; k < l; k++) n[k] = '9';
      break;
    }
  ten[0] = B;
  ret = pw(B, n[l - 1] - '0');
  for (i = 1; i < l; i++) {
    ten[i] = pw(ten[i - 1], 10);
    ret = (long long)ret * pw(ten[i], n[l - i - 1] - '0') % c;
  }
  ret = (long long)ret * (B - 1 + c) % c;
  printf("%d", ret ? ret : c);
  return 0;
}
