#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long phi(long long x) {
  long long ret = x, a = x;
  for (long long i = 2; i * i <= a; i++) {
    if (a % i == 0) ret = ret / i * (i - 1);
    while (a % i == 0) a /= i;
  }
  if (a > 1) ret = ret / a * (a - 1);
  return ret;
}
long long poww(long long a, long long b, long long mod) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}
long long getNum(string s, long long mod) {
  long long ret = 0, len = s.size();
  for (int i = 0; i < len; i++) ret = (ret * 10 + s[i] - '0') % mod;
  return ret;
}
int main() {
  string b, n;
  long long c;
  cin >> b >> n >> c;
  long long bb = getNum(b, c), ph = phi(c), d = gcd(ph, bb), ans;
  if (n.size() < 10) {
    long long nn = getNum(n, 1ll * 1e15) - 1;
    if (d == 1)
      ans = ((bb - 1 + c) % c) * poww(bb, nn % ph, c) % c;
    else {
      if (nn < ph)
        ans = ((bb - 1 + c) % c) * poww(bb, nn, c) % c;
      else
        ans = ((bb - 1 + c) % c) * poww(bb, nn % ph + ph, c) % c;
    }
  } else {
    long long nn = (getNum(n, ph) - 1 + ph) % ph;
    if (d == 1)
      ans = ((bb - 1 + c) % c) * poww(bb, nn, c) % c;
    else
      ans = ((bb - 1 + c) % c) * poww(bb, nn + ph, c) % c;
  }
  if (ans)
    cout << ans << endl;
  else
    cout << c << endl;
  return 0;
}
