#include <bits/stdc++.h>
using namespace std;
long long quickMod(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % c;
    b >>= 1;
    a = a * a % c;
  }
  return ans;
}
long long Euler(long long x) {
  long long res = x;
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      res = res - res / i;
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) res = res - res / x;
  return res;
}
int main() {
  long long c, tmp, ans, phi, result;
  char b[1000005], n[1000005];
  scanf("%s%s%lld", b, n, &c);
  phi = Euler(c);
  tmp = 0;
  for (long long i = 0; b[i]; i++) tmp = (tmp * 10 + b[i] - '0') % c;
  ans = 0;
  bool flag = false;
  for (long long i = 0; n[i]; i++) {
    ans = ans * 10 + n[i] - '0';
    if (ans > phi) {
      flag = true;
      ans %= phi;
    }
  }
  if (flag) ans += phi;
  ans--;
  if (tmp == 0) tmp = c;
  result = (((tmp + c - 1) % c) * quickMod(tmp, ans, c)) % c;
  printf("%lld\n", result ? result : c);
  return 0;
}
