#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1000010;
char b[Maxn], n[Maxn];
inline long long pow(long long a, int b, int mod) {
  long long res = 1;
  for (int i = 0; i < b; i++) res = res * a % mod;
  return res;
}
int main() {
  int c, len1, len2;
  long long bm = 0, res = 1;
  scanf("%s %s %d", b, n, &c);
  len1 = strlen(b);
  len2 = strlen(n);
  for (int i = 0; i < len1; i++) bm = (bm * 10 + b[i] - '0') % c;
  for (int i = len2 - 1; i >= 0; i--)
    if (n[i] == '0')
      n[i] = '9';
    else {
      n[i]--;
      break;
    }
  for (int i = 0; i < len2; i++)
    res = pow(res, 10, c) * pow(bm, n[i] - '0', c) % c;
  res = (bm + c - 1) * res % c;
  if (res == 0)
    printf("%d\n", c);
  else
    printf("%d\n", res);
  return 0;
}
