#include <bits/stdc++.h>
using namespace std;
string b, n;
long long c = 0, bb = 0, nn = 0;
long long qp(int a, int b) {
  long long ans = 1, base = a;
  while (b) {
    if (b & 1) ans = (ans * base) % c;
    base = (base * base) % c;
    b >>= 1;
  }
  return ans;
}
int phi(int n) {
  int ans = n;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans = ans / i * (i - 1);
      while (n % i == 0) n /= i;
    }
  }
  if (n > 1) ans = ans / n * (n - 1);
  return ans;
}
int main() {
  cin >> b >> n >> c;
  long long p = phi(c), nnn = 0;
  int t = n.size();
  while (1) {
    if (n[t] > '0') {
      n[t]--;
      break;
    } else
      n[t] = '9', t--;
  }
  for (int i = 0; i < n.size(); i++) {
    nn = (nn * 10 + (n[i] - '0')) % p;
    if (nnn < p) nnn = nnn * 10 + (n[i] - '0');
  }
  if (nnn + 1 >= p) nn = nn + p;
  for (int i = 0; i < b.size(); i++) bb = (bb * 10 + (b[i] - '0')) % c;
  long long ans = (long long)(qp(bb, nn) * (bb == 0 ? c - 1 : bb - 1)) % c;
  cout << (ans == 0 ? c : ans);
  return 0;
}
