#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline long long ksm(long long x, long long y, long long z) {
  long long ret = 1;
  while (y) {
    if (y & 1) ret = (ret * x) % z;
    x = (x * x) % z;
    y >>= 1;
  }
  return ret;
}
string N, B;
long long c, len1, len2, phi, p;
bool flag = 0;
long long n, b;
signed main() {
  cin >> B >> N >> c;
  len1 = B.length();
  len2 = N.length();
  N = " " + N;
  B = " " + B;
  p = phi = c;
  for (long long i = 2; i * i <= p; i++) {
    if (p % i == 0) {
      phi -= phi / i;
      while (p % i == 0) p /= i;
    }
  }
  if (p > 1) phi -= phi / p;
  for (long long i = 1; i <= len1; i++) b = (b * 10 + B[i] - '0') % c;
  for (long long i = len2; i >= 1; i--)
    if (N[i] == '0')
      N[i] = '9';
    else {
      N[i]--;
      break;
    }
  for (long long i = 1; i <= len2; i++) {
    n = n * 10 + N[i] - '0';
    if (n >= phi) flag = 1;
    n %= phi;
  }
  if (flag) n += phi;
  long long ans = ((b - 1) * ksm(b, n, c) + c) % c;
  if (ans == 0) ans = c;
  cout << ans << endl;
}
