#include <bits/stdc++.h>
using namespace std;
long long b, c;
string temp, n;
void parseB() {
  b = 0;
  for (size_t i = 0; i < temp.size(); i++) b *= 10, b += temp[i] - '0', b %= c;
}
long long binpow(long long x, long long n, long long m) {
  if (n == 0) return 1;
  long long a = binpow(x, n / 2, m);
  a *= a, a %= m;
  if (n & 1) a *= x, a %= m;
  return a;
}
void decN() {
  int i = n.size() - 1;
  while (n[i] == '0') i--;
  n[i]--;
  for (int j = i + 1; j < (int)n.size(); j++) n[j] = '9';
}
int main() {
  cin >> temp >> n >> c;
  parseB();
  decN();
  long long answer = 1, q = b;
  for (int i = n.size() - 1; i >= 0; i--) {
    answer = (answer * binpow(q, n[i] - '0', c)) % c;
    q = binpow(q, 10, c);
  }
  if (b)
    answer = (answer * (b - 1)) % c;
  else
    answer = (answer * (c - 1)) % c;
  answer %= c;
  if (answer == 0)
    cout << c;
  else
    cout << answer;
  return 0;
}
