#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
template <class T>
inline T sqr(T x) {
  return x * x;
}
char bb[1 << 20], nn[1 << 20];
int c;
int mod(char *s) {
  long long res = 0;
  int n = (int)strlen(s);
  for (int i = 0; i < n; ++i) res = (res * 10 + s[i] - '0') % c;
  return (int)res;
}
void dec(char *s) {
  int n = (int)strlen(s);
  for (int i = n - 1; i >= 0; --i)
    if (s[i] == '0')
      s[i] = '9';
    else {
      --s[i];
      break;
    }
}
long long binpow(long long x, int n) {
  long long res = 1;
  while (n)
    if (n & 1)
      res = res * x % c, --n;
    else
      x = x * x % c, n >>= 1;
  return res;
}
long long pow(long long x, char *s) {
  long long res = 1;
  for (int i = (int)strlen(s) - 1; i >= 0; --i) {
    res = res * binpow(x, s[i] - '0') % c;
    x = binpow(x, 10);
  }
  return res;
}
int main() {
  scanf("%s%s%d", bb, nn, &c);
  long long x = mod(bb);
  long long res = (x - 1 + c) % c;
  dec(nn);
  res = res * pow(x, nn) % c;
  if (!res) res = c;
  printf("%lld\n", res);
  return 0;
}
