#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
mt19937 rnd(time(nullptr));
long long mod;
int ppow(int first, int second) {
  int res = 1;
  while (second) {
    if (second & 1) res = res * 1LL * first % mod;
    first = first * 1LL * first % mod;
    second >>= 1LL;
  }
  return res % mod;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string b, n;
  cin >> b >> n >> mod;
  long long d = 1, val = 0, ans = 1;
  for (int i = b.size() - 1; ~i; --i) {
    val += int(b[i] - '0') * d, val %= mod;
    d *= 10, d %= mod;
  }
  for (int i = n.size() - 1; ~i; --i) {
    if (n[i] == '0') continue;
    n[i]--;
    for (int j = i + 1; j < n.size(); ++j) n[j] = '9';
    break;
  }
  long long cur;
  d = val;
  for (int i = n.size() - 1; ~i; --i) {
    cur = 1;
    for (int j = 0; j < n[i] - '0'; ++j) cur *= d, cur %= mod;
    ans *= cur, ans %= mod;
    cur = d;
    for (int j = 1; j < 10; ++j) d *= cur, d %= mod;
  }
  ans *= (val - 1 + mod), ans %= mod;
  cout << (ans ? ans : mod);
}
