#include <bits/stdc++.h>
using namespace std;
char bs[2000010], ns[2000010];
long long ans = 1, b, n, c, phi, lb, ln;
int main() {
  long long x;
  scanf("%s", bs);
  lb = strlen(bs);
  scanf("%s", ns);
  ln = strlen(ns);
  cin >> c;
  phi = x = c;
  for (int i = 2; i * i <= c; i++)
    if (x % i == 0) {
      phi -= phi / i;
      while (x % i == 0) x /= i;
    }
  if (x > 1) phi -= phi / x;
  for (int i = 0; i < lb; i++) b = (b * 10 + bs[i] - 48) % c;
  for (int i = ln - 1; i >= 0; i--)
    if (ns[i] == 48)
      ns[i] = 57;
    else {
      ns[i]--;
      break;
    }
  x = 0;
  for (int i = 0; i < ln; i++) {
    n = n * 10 + ns[i] - 48;
    if (n >= phi) x = 1, n %= phi;
  }
  if (x == 1) n += phi;
  for (x = b; n; n >>= 1, x = x * x % c)
    if (n & 1) ans = ans * x % c;
  ans = (ans * (b - 1) + c) % c;
  if (ans == 0)
    cout << c;
  else
    cout << ans;
}
