#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int c, len1, len2;
int phi(int x) {
  int ret = x;
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0) {
      ret = ret / i * (i - 1);
      while (x % i == 0) x /= i;
    }
  if (x > 1) ret = ret / x * (x - 1);
  return ret;
}
long long power(long long now, long long k) {
  long long ret = 1;
  while (k) {
    if (k & 1) ret = ret * now % c;
    now = now * now % c;
    k >>= 1;
  }
  return ret;
}
char b[maxn], n[maxn];
int main() {
  scanf("%s%s%d", b + 1, n + 1, &c);
  long long ph = phi(c);
  len1 = strlen(n + 1);
  len2 = strlen(b + 1);
  if (len1 > 10) {
    long long n1 = 0;
    for (int i = 1; i <= len1; i++) n1 = (n1 * 10 % ph + n[i] - '0') % ph;
    n1 = n1 + ph - 1;
    long long C = 0;
    for (int i = 1; i <= len2; i++) C = (C * 10 % c + b[i] - '0') % c;
    long long fucking = (C - 1 + c) % c * power(C, n1) % c;
    printf("%lld\n", fucking == 0 ? c : fucking);
  } else {
    long long n1 = 0, b1 = 0;
    for (int i = 1; i <= len1; i++) n1 = n1 * 10 + n[i] - '0';
    for (int i = 1; i <= len2; i++) b1 = (b1 * 10 % c + b[i] - '0') % c;
    printf("%lld\n", (b1 - 1 + c) % c * power(b1, n1 - 1) % c == 0
                         ? c
                         : (b1 - 1 + c) % c * power(b1, n1 - 1) % c);
  }
}
