#include <bits/stdc++.h>
using namespace std;
long long get_pow(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
      ans = ans % c;
    }
    a = a * a;
    a = a % c;
    b = b / 2;
  }
  return ans;
}
char b[1200000], n[1200000];
int main() {
  long long c;
  scanf("%s%s%lld", b, n, &c);
  long long lenb;
  long long lenn;
  lenb = strlen(b);
  lenn = strlen(n);
  long long i;
  for (i = lenn - 1; i >= 0; i--) {
    if (n[i] == '0')
      n[i] = '9';
    else {
      n[i]--;
      break;
    }
  }
  long long bb = 0;
  for (i = 0; i < lenb; i++) {
    bb = (bb * 10 + b[i] - '0') % c;
  }
  long long ans = 1;
  for (i = 0; i < lenn; i++) {
    ans = get_pow(ans, 10, c);
    ans = ans * get_pow(bb, n[i] - '0', c);
    ans = ans % c;
  }
  if (bb == 0) bb = c;
  ans = ans * (bb - 1) % c;
  if (ans == 0)
    printf("%lld\n", c);
  else
    printf("%lld\n", ans);
}
