#include <bits/stdc++.h>
using namespace std;
string b, n;
long long c, a, res;
int main() {
  int i, j, k;
  cin >> b >> n;
  scanf("%d", &c);
  a = 0;
  for (i = 0; i < b.length(); i++) {
    a = a * 10 + (b[i] - '0');
    a %= c;
  }
  for (i = n.length() - 1; i >= 0; i--) {
    if (n[i] == '0') {
      n[i] = '9';
    } else {
      n[i] -= 1;
      break;
    }
  }
  res = 1;
  for (i = 0; i < n.length(); i++) {
    long long tmp = res;
    for (j = 1; j < 10; j++) {
      res *= tmp;
      res %= c;
    }
    tmp = 1;
    for (j = 1; j <= n[i] - '0'; j++) {
      tmp *= a;
      tmp %= c;
    }
    res = res * tmp;
    res %= c;
  }
  res = ((res * (a - 1)) % c + c) % c;
  if (res)
    cout << res << endl;
  else
    cout << c << endl;
  return 0;
}
