#include <bits/stdc++.h>
using namespace std;
long long mod(long long a, long long b) { return ((a % b) + b) % b; }
int gcd(int a, int b) {
  int tmp;
  while (b) {
    a %= b;
    tmp = a;
    a = b;
    b = tmp;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long extended_euclid(long long a, long long b, long long &x,
                          long long &y) {
  long long xx = y = 0;
  long long yy = x = 1;
  while (b) {
    long long q = a / b;
    long long t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
pair<long long, long long> chinese_remainder_theorem(long long x, long long a,
                                                     long long y, long long b) {
  long long s, t;
  long long d = extended_euclid(x, y, s, t);
  if (a % d != b % d) return make_pair(0, -1);
  return make_pair(mod(s * b * x + t * a * y, x * y) / d, x * y / d);
}
pair<long long, long long> chinese_remainder_theorem(const vector<int> &x,
                                                     const vector<int> &a) {
  pair<long long, long long> ret = make_pair(a[0], x[0]);
  for (int i = 1; i < x.size(); i++) {
    ret = chinese_remainder_theorem(ret.second, ret.first, x[i], a[i]);
    if (ret.second == -1) break;
  }
  return ret;
}
char b[1000005];
char n[1000005];
int c;
int pw(int a, int p, int mod) {
  if (p == 0) return 1;
  long long r = pw(a, p / 2, mod);
  r *= r;
  r %= mod;
  if (p % 2) r *= a;
  return r % mod;
}
int main() {
  scanf("%s", b);
  scanf("%s", n);
  scanf("%d", &c);
  long long base = 0;
  for (int i = 0; b[i] != '\0'; i++) {
    base *= 10;
    base += b[i] - '0';
    base %= c;
  }
  int len = strlen(n);
  if (len < 10) {
    long long power = 0;
    for (int i = 0; i < len; i++) {
      power *= 10;
      power += n[i] - '0';
    }
    long long res = base - 1;
    while (res < 0) res += c;
    res *= pw(base, power - 1, c);
    res %= c;
    int ans = res;
    if (ans == 0) ans = c;
    printf("%d\n", ans);
    return 0;
  }
  int x = c;
  vector<int> pws;
  vector<int> prs;
  for (int i = 2; i * i <= x; i++) {
    int cnt = 0;
    int pw = 1;
    while (x % i == 0) {
      cnt++;
      x /= i;
      pw *= i;
    }
    if (cnt) {
      pws.push_back(pw);
      prs.push_back(i);
    }
  }
  if (x > 1) {
    pws.push_back(x);
    prs.push_back(x);
  }
  vector<int> a(pws.size());
  for (int j = 0; j < pws.size(); j++) {
    long long power = 0;
    long long r = 0;
    long long r2 = 0;
    for (int i = 0; b[i] != '\0'; i++) {
      r *= 10;
      r += b[i] - '0';
      r %= pws[j];
      r2 *= 10;
      r2 += b[i] - '0';
      r2 %= prs[j];
    }
    int modp = (pws[j] - pws[j] / prs[j]);
    for (int i = 0; n[i] != '\0'; i++) {
      power *= 10;
      power += n[i] - '0';
      power %= modp;
    }
    if (r2 == 0) {
      a[j] = 0;
    } else {
      power = (power + modp - 1) % modp;
      a[j] = pw(r, power, pws[j]);
    }
  }
  pair<long long, long long> res = chinese_remainder_theorem(pws, a);
  int ans = res.first;
  ans %= c;
  base--;
  while (base < 0) base += c;
  ans = (ans * 1ll * base) % c;
  if (ans == 0) ans = c;
  printf("%d\n", ans);
}
