#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 100;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
long long b, c, x, p[10], ans;
string s, t;
vector<long long> g;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> s >> t >> c;
  for (long long i = 0; i < s.size(); i++) {
    b = (b * 10 + (s[i] - '0')) % c;
  }
  p[0] = 1;
  for (long long i = 1; i < 10; i++) {
    p[i] = (p[i - 1] * b) % c;
  }
  for (long long i = 0; i < t.size(); i++) {
    g.push_back(t[i] - '0');
  }
  for (long long i = 1; i <= g.size(); i++) {
    g[g.size() - i]--;
    if (g[g.size() - i] == -1)
      g[g.size() - i] = 9;
    else
      break;
  }
  ans = 1;
  for (long long i = 0; i < g.size(); i++) {
    x = 1;
    for (long long j = 0; j < 10; j++) {
      x = (x * ans) % c;
    }
    ans = (x * p[g[i]]) % c;
  }
  ans = (ans * (b - 1)) % c;
  ans = (ans + c) % c;
  if (ans == 0) ans = c;
  cout << ans;
  return 0;
}
