#include <bits/stdc++.h>
using namespace std;
template <typename T = long long>
inline T read() {
  T s = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = (s << 3) + (s << 1) + ch - 48;
    ch = getchar();
  }
  return s * f;
}
const int N = 1e6 + 5, MAXN = 1e5 + 5, MOD = 1e9 + 7, CM = 998244353,
          INF = 0x3f3f3f3f;
int eular(int n) {
  int ans = n;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans -= ans / i;
      while (n % i == 0) n /= i;
    }
  }
  if (n > 1) ans -= ans / n;
  return ans;
}
char b[N], n[N];
int c;
void subone(char *s) {
  int len = strlen(s);
  for (int i = len - 1; i >= 0; i--) {
    if (s[i] > '0') {
      s[i]--;
      break;
    } else
      s[i] = '9';
  }
}
long long pow(long long a, long long b, long long p) {
  long long ans = 1;
  a %= p;
  while (b) {
    if (b & 1) ans = ans * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return ans;
}
void solve(int kase) {
  cin >> b >> n >> c;
  subone(n);
  int phi = eular(c);
  bool f = 0;
  long long nn = 0;
  int lenn = strlen(n);
  int lenb = strlen(b);
  for (int i = 0; i < lenn; i++) {
    nn = nn * 10 + n[i] - '0';
    if (nn >= phi) f = 1;
    nn %= phi;
  }
  nn += f * phi;
  long long bb = 0;
  for (int i = 0; i < lenb; i++) {
    bb = bb * 10 + (b[i] - '0');
    bb %= c;
  }
  long long ans = pow(bb, nn, c);
  subone(b);
  bb = 0;
  for (int i = 0; i < lenb; i++) {
    bb = bb * 10 + b[i] - '0';
    bb %= c;
  }
  ans = bb * ans % c;
  ans = (ans + c) % c;
  if (ans == 0) ans = c;
  printf("%lld\n", ans);
}
const bool DUO = 0;
int main() {
  clock_t start, finish;
  double totaltime;
  start = clock();
  if (DUO) {
    int Kase = 0;
    cin >> Kase;
    for (int kase = 1; kase <= Kase; kase++) solve(kase);
  } else
    solve(1);
  finish = clock();
  return 0;
  printf("\nTime: %lfms\n", (double)(finish - start) / CLOCKS_PER_SEC * 1000);
  return 0;
}
