#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int lowbit(int x) { return x & -x; }
int read() {
  int v = 0, f = 1;
  char c = getchar();
  while (c < 48 || 57 < c) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (48 <= c && c <= 57) v = v * 10 + c - 48, c = getchar();
  return v * f;
}
struct timer {
  double t;
  timer() { t = clock(); }
  ~timer() { printf("\nruntime %.3f s\n", get_time()); }
  double get_time() { return (clock() - t) / CLOCKS_PER_SEC; }
};
string B, N;
long long C;
long long pw[12];
int main() {
  cin >> B >> N >> C;
  int N_len = N.size();
  for (int i = N_len - 1; i >= 0; --i) {
    N[i]--;
    if (N[i] < '0')
      N[i] = '9';
    else
      break;
  }
  long long b = 0;
  int B_len = B.size();
  for (int i = 0; i < B_len; i++) {
    b = (b * 10 + B[i] - '0') % C;
  }
  pw[0] = 1;
  for (int i = 1; i <= 10; i++) {
    pw[i] = pw[i - 1] * b % C;
  }
  long long ans = 1;
  for (int i = 0; i < N_len; i++) {
    ans = ans * ans % C;
    long long tmp = ans;
    ans = ans * ans % C;
    ans = ans * ans % C;
    ans = ans * tmp % C;
    ans = ans * pw[N[i] - '0'] % C;
  }
  ans = ans * (b - 1 + C) % C;
  if (ans == 0)
    cout << C << endl;
  else
    cout << ans << endl;
  return 0;
}
