#include <bits/stdc++.h>
using namespace std;
long long c;
char b[1000004], n[1000004];
long long qpow(long long a, long long n) {
  long long base = a;
  base %= c;
  long long ans = 1;
  for (; n > 0;) {
    if (n & 1) {
      ans *= base;
      ans %= c;
    }
    base *= base;
    base %= c;
    n >>= 1;
  }
  return ans;
}
int oula(int n) {
  int ans = 1;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans *= (i - 1);
      for (; n % i == 0;) {
        n /= i;
        ans *= i;
      }
      ans /= i;
    }
  }
  if (n > 1) ans *= (n - 1);
  return ans;
}
int main() {
  scanf("%s%s%lld", &b, &n, &c);
  long long bb = 0;
  long long nn = 0;
  for (int i = 0; i < strlen(b); i++) {
    bb = (10 * bb + (b[i] - '0')) % c;
  }
  long long m = oula(c);
  bool ok = 0;
  for (int i = 0; i < strlen(n); i++) {
    if (nn * 10 + n[i] - '0' >= m) ok = 1;
    nn = (nn * 10 + n[i] - '0') % m;
  }
  long long ans = 0;
  if (ok)
    ans = ((bb - 1) % c * qpow(bb, nn - 1 + m) % c + c) % c;
  else
    ans = ((bb - 1) % c * qpow(bb, nn - 1) % c + c) % c;
  if (ans == 0) ans = c;
  printf("%lld\n", ans);
}
