#include <bits/stdc++.h>
using namespace std;
long long c, a, b, _a;
char A[1000007], B[1000007];
long long euler(long long n) {
  long long ans = n;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans -= ans / i;
      while (n % i == 0) {
        n /= i;
      }
    }
  }
  if (n > 1) ans -= ans / n;
  return ans;
}
long long quick(long long a, long long b, long long m) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % m;
    a *= a;
    a %= m;
    b >>= 1;
  }
  return ans;
}
int main() {
  while (~scanf("%s %s %lld", A, B, &c)) {
    long long la = 0, lb = 0;
    for (long long i = 0; A[i] != '\0'; i++) {
      a = (a * 10 + A[i] - '0') % c;
      la++;
    }
    for (long long i = la - 1; i >= 0; i--) {
      if (A[i] == '0')
        A[i] = '9';
      else {
        A[i]--;
        break;
      }
    }
    for (long long i = 0; A[i] != '\0'; i++) {
      _a = (_a * 10 + A[i] - '0') % c;
    }
    long long p = euler(c);
    int flag = 0;
    for (int i = 0; B[i] != '\0'; ++i) {
      b = (b * 10 + B[i] - '0');
      if (b >= p) flag = 1;
      b %= p;
    }
    if (flag) b += p;
    long long ans = quick(a, b - 1, c) * _a % c;
    printf("%lld\n", ans ? ans : c);
  }
  return 0;
}
