#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 66;
char b[MAXN], n[MAXN];
long long Eular(long long c) {
  long long phi = c;
  for (int i = 2; i * i <= c; ++i) {
    if (c % i == 0) {
      phi = phi - phi / i;
      while (c % i == 0) c = c / i;
    }
  }
  if (c > 1) phi = phi - phi / c;
  return phi;
}
long long quick_pow(long long a, long long x, long long mod) {
  long long ans = 1;
  while (x) {
    if (x & 1) ans = ans * a % mod;
    a = a * a % mod;
    x = x >> 1;
  }
  return ans % mod;
}
long long cal(char *s, long long mod) {
  long long res = 0;
  for (int i = 0; i < strlen(s); ++i) res = (s[i] - '0' + res * 10) % mod;
  return res;
}
int main() {
  int c;
  scanf("%s %s %d", b, n, &c);
  long long bb = cal(b, c);
  long long phi = Eular(c);
  bool vis = false;
  long long nn = 0;
  for (int i = 0; i < strlen(n); ++i) {
    nn = n[i] - '0' + nn * 10;
    if (nn >= phi + 1) {
      vis = true;
      nn = nn % phi;
    }
  }
  if (vis) nn += phi;
  long long ans = (quick_pow(bb, nn - 1, c) * (bb - 1) + c) % c;
  if (ans)
    cout << ans;
  else
    cout << c;
  return 0;
}
