#include <bits/stdc++.h>
using namespace std;
namespace HMFS {
const long long maxn = 1e6 + 10;
long long c, mod, bj, res, ans;
char n[maxn], b[maxn];
long long get_phi(long long x) {
  long long ans = x;
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      while (x % i == 0) x /= i;
      ans = ans / i * (i - 1);
    }
  }
  return x > 1 ? ans / x * (x - 1) : ans;
}
long long ksm(long long base, long long power) {
  long long res = 1;
  while (power != 0) {
    if (power & 1) {
      res = (res * base) % c;
    }
    power /= 2;
    base = (base * base) % c;
  }
  return res;
}
void work() {
  cin >> b >> n >> c;
  mod = get_phi(c);
  long long lenn = strlen(n), lenb = strlen(b);
  for (long long i = 0; i < lenb; i++) {
    ans = (ans * 10 + b[i] - '0') % c;
  }
  for (long long i = lenn - 1; i >= 0; i--) {
    if (n[i] == '0')
      n[i] = '9';
    else {
      n[i]--;
      break;
    }
  }
  long long tmp = 0;
  for (long long i = 0; i < lenn; i++) {
    tmp = tmp * 10 + n[i] - '0';
    if (tmp >= mod) bj = 1;
    tmp %= mod;
  }
  if (bj) tmp += mod;
  res = (ans - 1 + c) % c * ksm(ans, tmp) % c;
  cout << (res ? res : c);
}
}  // namespace HMFS
int main() { return HMFS::work(), 0; }
