#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1000010;
char c[MAXN], d[MAXN];
long long a, m, b, phi, flag, len;
inline long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
inline long long PHI(long long x) {
  long long cnt = x;
  for (long long i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      while (x % i == 0) x = x / i;
      cnt = cnt / i * (i - 1);
    }
  }
  if (x > 1) cnt = cnt / x * (x - 1);
  return cnt;
}
inline long long fast_pow(long long w, long long p, long long mod) {
  long long ans = 1;
  while (p) {
    if (p & 1) ans = ans * w % mod;
    w = w * w % mod;
    p = p >> 1;
  }
  return ans;
}
signed main() {
  scanf("%s", c + 1);
  scanf("%s", d + 1);
  len = strlen(c + 1);
  scanf("%d", &m);
  for (long long i = 1; i <= len; ++i) a = (a * 10 + c[i] - '0') % m;
  phi = PHI(m);
  len = strlen(d + 1);
  for (long long i = 1; i <= len; ++i) {
    if (b > phi) {
      b = (b * 10 + d[i] - '0') % phi;
      flag = 1;
    } else
      b = b * 10 + d[i] - '0';
  }
  if (!flag)
    if (b > phi + 1) b = b % phi, flag = 1;
  if (flag)
    --b, b = (b + phi) % phi;
  else
    --b;
  long long w;
  if (gcd(a, m) == 1) {
    w = (fast_pow(a, b, m) * ((a - 1 + m) % m) % m);
    w = w == 0 ? m : w;
    printf("%lld\n", w);
  } else {
    if (flag)
      w = (fast_pow(a, b + phi, m) * ((a - 1 + m) % m) % m);
    else
      w = (fast_pow(a, b, m) * ((a - 1 + m) % m) % m);
    w = w == 0 ? m : w;
    printf("%lld\n", w);
  }
  return 0;
}
