#include <bits/stdc++.h>
using namespace std;
long long c;
long long quick_pow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) {
      ans = a * ans % c;
      b--;
    }
    a = a * a % c;
    b >>= 1;
  }
  return ans;
}
int main() {
  char b[1000005], n[1000005];
  scanf("%s%s%lld", b, n, &c);
  long long lb = strlen(b), ln = strlen(n);
  for (long long i = ln - 1; i >= 0; i--) {
    if (n[i] == '0') {
      n[i] = '9';
    } else {
      n[i]--;
      break;
    }
  }
  long long bb = 0, ans = 1;
  for (int i = 0; i < lb; i++) {
    bb *= 10;
    bb += b[i] - '0';
    bb %= c;
  }
  for (int i = 0; i <= ln - 1; i++) {
    ans = quick_pow(ans, 10);
    ans = ans * quick_pow(bb, n[i] - '0');
    ans %= c;
  }
  if (bb >= 1)
    ;
  else
    bb += c;
  ans *= (bb - 1);
  ans %= c;
  printf("%lld\n", (ans == 0) ? c : ans);
}
