#include <bits/stdc++.h>
using namespace std;
template <class T>
int modpow(T a, int n, int mod) {
  if (n == 0) return 1;
  if (n == 1) return a % mod;
  T x = modpow(a, n / 2, mod);
  if (n & 1)
    return (((a * x) % mod) * x) % mod;
  else
    return (x * x) % mod;
}
long long mod_power(long long a, char* b, long long mod) {
  vector<int> bs;
  for (int i = 0; b[i] != '\0'; i++) bs.push_back(b[i] - '0');
  reverse(bs.begin(), bs.end());
  long long ret = 1, u = a;
  for (int i = 0; i < (int)(bs.size()); i++) {
    ret = (ret * modpow(u, bs[i], mod)) % mod;
    u = modpow(u, 10, mod);
  }
  return ret;
}
char s[1000005];
char t[1000005];
void subtract1(char s[]) {
  int len = strlen(s);
  for (int i = len - 1; i >= 0; i--) {
    if (s[i] == '0')
      s[i] = '9';
    else {
      s[i]--;
      return;
    }
  }
  assert(false);
}
long long modulo(char s[], long long mod) {
  long long x = 0;
  for (int i = 0; s[i] != '\0'; i++) {
    x = 10 * x + (s[i] - '0');
    if (x >= mod) x %= mod;
  }
  return x;
}
int main() {
  int c;
  scanf("%s %s %d", s, t, &c);
  long long mod = c;
  long long b = modulo(s, mod);
  subtract1(s);
  long long b1 = modulo(s, mod);
  assert(b1 >= 1);
  subtract1(t);
  long long ret = mod_power(b, t, mod);
  ret = (b1 * ret) % mod;
  cout << (ret == 0 ? mod : ret) << '\n';
}
