#include <bits/stdc++.h>
using namespace std;
int n, ta, tb;
char s[1000010];
bool low;
long long a[1000010], b[1000010], p, phi, ans;
long long Pow(long long a, int b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return res;
}
int main() {
  int i, j;
  scanf("%s", s);
  ta = strlen(s);
  reverse(s, s + ta);
  for (i = 0; i < ta; i++) a[i] = s[i] - '0';
  scanf("%s", s);
  tb = strlen(s);
  reverse(s, s + tb);
  for (i = 0; i < tb; i++) b[i] = s[i] - '0';
  scanf("%lld", &p);
  n = p;
  phi = p;
  for (i = 2; i * i <= n; i++) {
    if (!(n % i)) {
      phi = phi / i * (i - 1);
      while (!(n % i)) n /= i;
    }
  }
  if (n > 1) phi = phi / n * (n - 1);
  for (i = ta - 1; i; i--) a[i - 1] += a[i] % p * 10;
  a[0] %= p;
  for (i = tb - 1; i; i--) low |= (b[i] >= phi), b[i - 1] += b[i] % phi * 10;
  low |= (b[0] >= phi);
  b[0] = ((b[0] - 1) % phi + phi) % phi + phi * low;
  ans = Pow(a[0], b[0]) * ((a[0] - 1 + p) % p) % p;
  printf("%lld", (!ans) ? p : ans);
  return 0;
}
