#include <bits/stdc++.h>
using namespace std;
const long long N = 5e6 + 7;
char s1[N], s2[N];
long long b, n, m, k, t, mod, phi, sum, ans;
long long a[N];
long long qpow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = 1ll * res * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return res;
}
long long get_phi(long long n) {
  long long ans = n;
  for (long long i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      ans = ans / i * (i - 1);
      while (n % i == 0) n /= i;
    }
  }
  if (n > 1) ans = ans / n * (n - 1);
  return ans;
}
signed main() {
  scanf("%s%s%lld", s1, s2, &mod);
  phi = get_phi(mod);
  long long len1 = strlen(s1), len2 = strlen(s2);
  for (long long i = 0; i < len1; ++i) b = (b * 10 + s1[i] - '0') % mod;
  for (long long i = 0; i < len2; ++i) a[i] = s2[i] - '0';
  a[len2 - 1]--;
  for (long long i = len2 - 1; ~i; --i) {
    if (a[i] < 0)
      a[i] += 10, a[i - 1]--;
    else
      break;
  }
  bool planB = 0;
  for (long long i = 0; i < len2; ++i) {
    n = (n * 10 + a[i]) % phi;
    if (planB == 0) sum = sum * 10 + a[i];
    if (sum >= phi) planB = 1;
  }
  ans = (b - 1 + mod) % mod * qpow(b, planB ? n + phi : n) % mod;
  if (ans == 0)
    cout << mod << endl;
  else
    cout << ans << endl;
  return 0;
}
