#include <bits/stdc++.h>
using namespace std;
char s1[1000000 + 10], s2[1000000 + 10];
long long c;
long long euler_phi(long long n) {
  long long m = sqrt(n + 0.5);
  long long ans = n;
  for (long long i = 2; i <= m; i++)
    if (n % i == 0) {
      ans = ans / i * (i - 1);
      while (n % i == 0) n /= i;
    }
  if (n > 1) ans = ans / n * (n - 1);
  return ans;
}
long long quickpow(long long m, long long n, long long k) {
  if (n == 0) return 1;
  long long b = 1;
  while (n > 0) {
    if (n & 1) b = (b * m) % k;
    n = n >> 1;
    m = (m * m) % k;
  }
  return b;
}
int main() {
  int flag = 0;
  scanf("%s", s1);
  scanf("%s", s2);
  if (strlen(s2) == 1 && s2[0] == '1') flag = 1;
  scanf("%lld", &c);
  long long MOD = euler_phi(c);
  long long b = 0;
  for (int i = 0; s1[i]; i++) b = (b * 10 + s1[i] - '0') % c;
  long long f = (b - 1 + c) % c;
  long long x = 0;
  for (int i = 0; s2[i]; i++) x = (x * 10 + s2[i] - '0') % MOD;
  x = (x - 1 + MOD) % MOD;
  if (flag == 0) {
    x += MOD;
  }
  long long ans = (f * quickpow(b, x, c)) % c;
  if (ans == 0) ans = c;
  printf("%lld\n", ans);
  return 0;
}
