#include <bits/stdc++.h>
using namespace std;
const int MaxLen = 1 << 20;
long long int B, N, C;
char __B[MaxLen], __N[MaxLen];
inline long long int Euler(long long int N) {
  long long int Ret = 1LL;
  for (long long int i = 2LL; i * i <= N; i++)
    if (!(N % i)) {
      Ret *= i - 1LL;
      N /= i;
      while (!(N % i)) {
        Ret *= i;
        N /= i;
      }
    }
  if (N > 1LL) Ret *= N - 1LL;
  return Ret;
}
inline long long int Pow(long long int A, long long int B, long long int M) {
  if (!B) return 1LL;
  long long int Ret = A, Base = A;
  B--;
  while (B) {
    if (B & 1LL) Ret = (Ret * Base) % M;
    B >>= 1LL;
    Base = (Base * Base) % M;
  }
  return Ret;
}
int main(void) {
  scanf("%s %s %I64d", __B, __N, &C);
  int LenB = strlen(__B), LenN = strlen(__N);
  for (int i = 0; i < LenB; i++) {
    B = B * 10LL + __B[i] - '0';
    while (B >= C) B -= C;
  }
  long long int Phi = Euler(C);
  for (int i = 0; i < LenN; i++) {
    N = N * 10LL + __N[i] - '0';
    while (N >= Phi) N -= Phi;
  }
  long long int Sol = B - 1LL + C;
  N = (N - 1LL + Phi) % Phi + Phi;
  if (LenN <= 10) {
    long long int RealN = 0LL;
    for (int i = 0; i < LenN; i++) RealN = RealN * 10LL + __N[i] - '0';
    if (RealN < Phi) N -= Phi;
  }
  Sol = (Sol * Pow(B, N, C)) % C;
  if (!Sol) Sol = C;
  printf("%I64d\n", Sol);
  return 0;
}
