#include <bits/stdc++.h>
char B[10000000], N[10000000];
long long euler(long long n) {
  long long i, res = n;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      res = res / i * (i - 1);
      while (n % i == 0) n = n / i;
    }
  }
  if (n > 1) res = res / n * (n - 1);
  return res;
}
long long quick(long long n, long long m, long long mod) {
  long long res = 1;
  while (m) {
    if (m & 1) res = (res * n) % mod;
    n = (n * n) % mod;
    m >>= 1;
  }
  return res;
}
int main() {
  long long c1, c;
  scanf("%s %s %lld", B, N, &c1);
  c = euler(c1);
  long long b1, n1, i, b2, b = 0, n = 0;
  b1 = strlen(B);
  n1 = strlen(N);
  for (i = 0; i < b1; i++) b = (b * 10 + B[i] - '0') % c1;
  if (b == 0) b = c1;
  b1 = b - 1;
  int flag = 0;
  for (i = 0; i < n1; i++) {
    n = n * 10 + N[i] - '0';
    if (n >= c) flag = 1;
    n = n % c;
  }
  long long m = n % c + c;
  long long mm;
  if (flag)
    mm = quick(b, m - 1, c1);
  else
    mm = quick(b, n - 1, c1);
  long long ans = (b1 * mm) % c1;
  if (ans == 0) ans = c1;
  printf("%lld\n", ans);
  return 0;
}
