#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const unsigned long long base = 2333;
const int maxn = 1e2 + 55;
const int maxm = 1e4 + 50;
const int maxv = 1e6 + 5;
const int mod = 51123987;
long long Cal(string s, long long p) {
  long long len = s.size();
  long long sum = 0;
  for (long long i = 0; i < len; i++) {
    sum = (sum * 10 + s[i] - '0') % p;
  }
  return sum;
}
long long phi(long long n) {
  long long res = n, m = n;
  for (long long i = 2; i <= sqrt(m); i++) {
    if (m % i == 0) res = res / i * (i - 1);
    while (m % i == 0) m /= i;
  }
  if (m > 1) res = res / m * (m - 1);
  return res;
}
long long Quick_pow(long long x, long long y, long long p) {
  long long ans = 1, res = x;
  while (y) {
    if (y & 1) ans = ans * res % p;
    res = res * res % p;
    y >>= 1;
  }
  return ans;
}
int main() {
  string b, n;
  long long c;
  cin >> b >> n >> c;
  long long ph = phi(c);
  long long bb = Cal(b, c);
  long long nn = Cal(n, ph);
  if (n.size() < 10) {
    long long z = 0;
    for (long long i = 0; i < n.size(); i++) z = (z * 10 + n[i] - '0');
    long long ans = ((bb - 1 + c) % c) * Quick_pow(bb, z - 1, c) % c;
    if (ans == 0) ans = c;
    printf("%lld\n", ans);
    return 0;
  }
  nn = (nn - 1 + ph) % ph + ph;
  long long ans = ((bb - 1 + c) % c) * Quick_pow(bb, nn, c) % c;
  if (ans == 0) ans = c;
  printf("%lld\n", ans);
  return 0;
}
