#include <bits/stdc++.h>
using namespace std;
char s[1000005], n[1000005];
long long b, c, p;
long long qpow(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res = res * a % c;
    a = a * a % c;
    n >>= 1;
  }
  return res;
}
long long phi(long long c) {
  long long res = c;
  for (long long i = 2; i * i <= c; i++) {
    if (c % i) continue;
    while (c % i == 0) c /= i;
    res = res / i * (i - 1);
  }
  if (c > 1) res = res / c * (c - 1);
  return res;
}
int main() {
  scanf("%s%s%lld", s + 1, n + 1, &c);
  p = phi(c);
  int len = strlen(s + 1);
  for (int i = 1; i <= len; i++) {
    b = (b * 10 + s[i] - '0') % c;
  }
  len = strlen(n + 1);
  for (int i = len; i >= 1; i--) {
    if (n[i] == '0')
      n[i] = '9';
    else {
      n[i]--;
      break;
    }
  }
  long long t = 0;
  bool flag = false;
  for (int i = 1; i <= len; i++) {
    if (t >= c) flag = true;
    if (flag)
      t = (t * 10 + n[i] - '0') % p;
    else
      t = (t * 10 + n[i] - '0');
  }
  if (flag) t += p;
  long long ans = qpow(b, t) * (b - 1 + c) % c;
  if (!ans) {
    cout << c << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}
