#include <bits/stdc++.h>
char strb[1000005], strn[1000005];
int C;
long long powerMod(long long a, long long b, long long c) {
  if (b == 0) return 1 % c;
  if (b & 1)
    return powerMod(a, b - 1, c) * a % c;
  else
    return powerMod(a * a % c, b / 2, c);
}
inline int calcPhi(int n) {
  int phi = 1;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      int c = 1;
      while (n % i == 0) {
        n /= i;
        c *= i;
      }
      phi *= (c - c / i);
    }
  }
  if (n > 1) phi *= (n - 1);
  return phi;
}
void solve() {
  int i;
  long long A, B, res, phi;
  A = B = 0;
  for (i = 0; i < strlen(strb); i++) {
    A = A * 10 + (strb[i] - '0');
    A %= C;
  }
  res = (A - 1 + C) % C;
  int len = strlen(strn);
  phi = calcPhi(C);
  if (len < 15) {
    for (i = 0; i < len; i++) B = B * 10 + (strn[i] - '0');
  } else {
    for (i = 0; i < len; i++) {
      B = B * 10 + (strn[i] - '0');
      B %= phi;
    }
    B += phi;
  }
  res = (res * powerMod(A, B - 1, C)) % C;
  if (res == 0) res = C;
  printf("%d\n", (int)res);
}
int main() {
  scanf("%s%s%d", strb, strn, &C);
  solve();
  return 0;
}
