#include <bits/stdc++.h>
using namespace std;
int main() {
  string b, n;
  long long int c;
  cin >> b >> n;
  cin >> c;
  long long int B = 0;
  for (int i = 0; i < b.length(); i++) {
    B = (B * 10 + b[i] - '0') % c;
  }
  long long int B10 = 1;
  for (int i = 1; i <= 10; i++) {
    B10 = (B10 * B) % c;
  }
  for (int i = n.length() - 1; i >= 0; i--) {
    if (n[i] - '0' >= 1) {
      n[i]--;
      break;
    } else {
      n[i] = '9';
    }
  }
  long long int ans = 1;
  long long int tmp = B10;
  for (int i = n.length() - 2; i >= 0; i--) {
    int current_number = n[i] - '0';
    long long int B10di = 1;
    for (int j = 1; j <= current_number; j++) {
      B10di = (B10di * tmp) % c;
    }
    ans = (ans * B10di) % c;
    long long int tmp1 = 1;
    for (int j = 1; j <= 10; j++) {
      tmp1 = (tmp * tmp1) % c;
    }
    tmp = tmp1;
  }
  long long int first_digit = 1;
  int current_number = n[n.length() - 1] - '0';
  for (int i = 1; i <= current_number; i++) {
    first_digit = (first_digit * B) % c;
  }
  ans = (ans * first_digit) % c;
  ans = (ans * (B - 1)) % c;
  if (ans < 0) {
    ans = ans + c;
  }
  if (ans == 0) {
    ans = c;
  }
  cout << ans << endl;
  return 0;
}
