#include <bits/stdc++.h>
using namespace std;
int main() {
  string b, n;
  long long int c, s = 0, e = 1, t;
  cin >> b >> n >> c;
  for (int i = 0; i < b.size(); i++) s = (s * 10LL + b[i] - '0') % c;
  for (int i = n.size() - 1; n[i]-- == '0'; n[i] = '9', i--)
    ;
  for (int i = 0; i < n.size(); i++) {
    t = e;
    for (int j = 1; j < 10; j++) e = e * t % c;
    for (int j = 0; j < n[i] - '0'; j++) e = e * s % c;
  }
  e = e * (s + c - 1) % c;
  if (!e)
    cout << c << endl;
  else
    cout << e << endl;
  return 0;
}
