#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
char b_[N], n_[N];
long long mod, b, n;
int prime[N];
inline void get_phi() {
  int x = mod;
  int sx = sqrt(mod);
  for (int i = 2; i <= sx; ++i) {
    if (x == 1) break;
    if (x % i) continue;
    while (x % i == 0) x /= i;
    prime[++prime[0]] = i;
  }
  if (!prime[0] || x != 1) prime[++prime[0]] = x;
}
inline long long gcd(long long a, long long b) {
  return (b == 0 ? a : gcd(b, a % b));
}
inline long long power(long long a, long long p) {
  long long ans = 1, base = a;
  while (p) {
    if (p & 1ll) (ans = ans * base) %= mod;
    (base = base * base) %= mod;
    p >>= 1ll;
  }
  return ans;
}
int main() {
  scanf("%s%s", b_ + 1, n_ + 1);
  scanf("%lld", &mod);
  int len = strlen(b_ + 1);
  b = 0ll;
  for (register int i = 1; i <= len; ++i) {
    (b = b * 10ll + b_[i] - '0') %= mod;
  }
  long long ans = (b - 1ll + mod) % mod;
  get_phi();
  long long phi = mod;
  for (register int i = 1; i <= prime[0]; ++i)
    phi = phi / prime[i] * (prime[i] - 1);
  long long gcd_ = gcd(mod, b);
  len = strlen(n_ + 1);
  n = 0ll;
  bool bigger = 0;
  for (register int i = 1; i <= len; ++i) {
    n = n * 10ll + n_[i] - '0';
    if (n > phi) {
      bigger = 1;
      n %= phi;
    }
  }
  if (gcd_ != 1)
    (ans *= power(b, (n - 1 + phi) % phi + (bigger ? phi : 0ll)) % mod) %= mod;
  else
    (ans *= power(b, (n - 1 + phi) % phi) % mod) %= mod;
  printf("%lld\n", ((ans % mod) == 0 ? mod : ans % mod));
  return 0;
}
