#include <bits/stdc++.h>
const int inf_max = 0x3f3f3f3f;
const int inf_min = 0xc0c0c0c0;
using namespace std;
string y_n[2] = {"No\n", "Yes\n"};
long long c, d;
char b[1000000 + 10], n[1000000 + 10];
int number = 1;
long long Euler(long long x) {
  long long res = x;
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      res = res / i * (i - 1);
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) res = res / x * (x - 1);
  return res;
}
long long Fast_power(long long base, long long power) {
  if (power == 0) return 1;
  long long res = 1;
  while (power) {
    if (power & 1) res = res * base % c;
    base = base * base % c;
    power >>= 1;
  }
  return res % c;
}
void My_reserve(char *s1, char *s2) {
  long long i, j, len = strlen(s1);
  for (i = len - 1, j = 0; i >= 0; i--, j++) s2[j] = s1[i];
  s2[j] = '\0';
}
long long handle(long long m) {
  long long res = (n[0] - '0') % m;
  for (long long i = 1; n[i]; i++) {
    res = (res % m * 10 % m + (n[i] - '0')) % m;
  }
  return (res % m + (-1) % m) % m;
}
long long Initial() {
  long long res = (b[0] - '0') % c;
  for (long long i = 1; b[i]; i++) {
    res = (res % c * 10 % c + (b[i] - '0')) % c;
  }
  return res % c;
}
void Input() {
  while (~scanf("%s%s%I64d", b, n, &c)) {
    long long fai_c = Euler(c);
    d = Initial();
    int flag = 0;
    long long p = n[0] - '0';
    for (long long i = 1; n[i]; i++) {
      p = p * 10 + n[i] - '0';
      if (p >= fai_c + 1) {
        flag = 1;
        p %= fai_c;
      }
    }
    if (flag) p += fai_c;
    long long fir_res = Fast_power(d, p - 1);
    long long sec_res = (d % c + (-1) % c) % c;
    long long ans = fir_res * sec_res % c;
    if (ans < 0) ans += c;
    if (ans)
      printf("%I64d\n", ans);
    else
      printf("%I64d\n", c);
  }
}
int main() {
  Input();
  return 0;
}
