#include <bits/stdc++.h>
using namespace std;
void exgcd(long long a, long long b, long long &d, long long &x, long long &y) {
  if (b == 0) {
    d = a, x = 1, y = 0;
  } else {
    exgcd(b, a % b, d, x, y);
    long long tmp = x;
    x = y;
    y = tmp - a / b * y;
  }
}
long long km(long long m, long long n, long long p) {
  long long ans = 1;
  while (n > 0) {
    if (n & 1) ans = (m * ans) % p;
    m = (m * m) % p;
    n = n >> 1;
  }
  return ans;
}
long long inverse(int a, long long p) {
  long long x, y;
  long long g;
  exgcd(a, p, g, x, y);
  return (x % p + p) % p;
}
long long c(long long m, long long n, long long p) {
  int ans = 1, nump = 0;
  for (int i = 1; i <= m; i++) {
    int temp = n - m + i;
    while (temp % p == 0) {
      nump++;
      temp /= p;
    }
    ans = ans * temp % p;
    temp = i;
    while (temp % p == 0) {
      nump--;
      temp /= p;
    }
    ans = ans * inverse(temp, p) % p;
  }
  if (nump > 0)
    return 0;
  else
    return ans;
}
long long lucas(long n, long m, long long p) {
  if (m == 0)
    return 1;
  else
    return c(n % p, m % p, p) * lucas(n / p, m / p, p) % p;
}
long long eular(long long n) {
  long long num = n;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      num = (num / i) * (i - 1);
      while (n % i == 0) {
        n /= i;
      }
    }
  }
  if (n > 1) num = num / n * (n - 1);
  return num;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    gcd(b, a % b);
}
char b[1000100], n[1000100];
int main() {
  long long c;
  while (scanf("%s%s%lld", b, n, &c) != EOF) {
    long long ans = 0, mod = eular(c);
    long long l = strlen(n);
    bool f = 0;
    for (int i = 0; i < l; i++) {
      ans = (ans * 10 + n[i] - '0');
      if (ans > mod) {
        f = 1;
        ans = ans % mod;
      }
    }
    if (f) ans = ans + mod;
    long long a = 0;
    l = strlen(b);
    for (int i = 0; i < l; i++) a = (a * 10 + b[i] - '0') % c;
    if (a == 0) a = c;
    ans = km(a, ans - 1, c);
    ans = (ans * ((a - 1 + c) % c)) % c;
    if (ans != 0)
      cout << ans << endl;
    else
      cout << c << endl;
  }
}
