#include <bits/stdc++.h>
using namespace std;
long long phi(int n) {
  long long ret = n;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n /= i;
      ret -= ret / i;
    }
  }
  if (n > 1) ret -= ret / n;
  return ret;
}
long long bigmod(long long base, long long power, long long m) {
  if (power == 0) return 1ll;
  if (power == 1) return base;
  long long x = bigmod(base, power / 2, m);
  x = (x * x) % m;
  if (power & 1) x = (x * base) % m;
  return x;
}
long long hugemod(string a, string b, int m) {
  int la = a.length();
  int lb = b.length();
  for (int i = lb - 1; i >= 0; i--) {
    if (b[i] - '0' >= 1) {
      b[i] = b[i] - 1;
      break;
    } else
      b[i] = '9';
  }
  long long A = 0;
  for (int i = 0; i < la; i++) {
    A = (A * 10 + a[i] - '0') % m;
  }
  long long x = 1;
  for (int i = 0; i < lb; i++) {
    x = bigmod(x, 10, m);
    x = (x * bigmod(A, b[i] - '0', m)) % m;
  }
  x = ((A - 1) * x) % m;
  return (x + m) % m;
}
int main() {
  string a, b;
  long long c;
  cin >> a >> b >> c;
  long long ans = hugemod(a, b, c);
  cout << (ans != 0 ? ans : c) << endl;
  return 0;
}
