#include <bits/stdc++.h>
using namespace std;
char s1[1000006], s2[1000006];
long long Num[1000006];
long long Dec[1000006];
long long Mod, Base;
int main() {
  scanf("%s%s%I64d", s1, s2, &Mod);
  for (int i = 0; s1[i]; i++) {
    Base = Base * 10 + s1[i] - '0';
    Base %= Mod;
  }
  Dec[0] = Base;
  for (int i = 1, L = strlen(s2); i <= L; i++) {
    Dec[i] = Dec[i - 1];
    for (int j = 2; j <= 10; j++) (Dec[i] *= Dec[i - 1]) %= Mod;
  }
  for (int i = 0, j = strlen(s2) - 1; ~j; j--, i++) Num[i] = s2[j] - '0';
  Num[0]--;
  int Cnt = 0;
  while (Num[Cnt] < 0) Num[Cnt + 1]--, Num[Cnt++] += 10;
  long long Ans = 1;
  for (int i = 0; s2[i]; i++) {
    for (int j = 1; j <= Num[i]; j++) (Ans *= Dec[i]) %= Mod;
  }
  (Ans *= (Base - 1 + Mod) % Mod) %= Mod;
  if (Ans)
    printf("%I64d", Ans);
  else
    printf("%I64d", Mod);
  return 0;
}
