#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
char b[N];
char s[N];
long long euler_phi(long long n) {
  long long m = (int)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 Pow(long long a, long long b, long long mod) {
  long long ret = 1;
  long long A = a;
  while (b) {
    if (b & 1) ret = ret * A % mod;
    A = A * A % mod;
    b >>= 1;
  }
  return ret;
}
int main(void) {
  long long c;
  scanf("%s%s%I64d", b, s, &c);
  int lenb = strlen(b);
  int lens = strlen(s);
  long long tb = 0, tb1 = 0;
  for (int i = 0; i < lenb; i++) {
    tb = (tb * 10 + b[i] - '0') % c;
  }
  tb1 = (tb - 1 + c) % c;
  long long phic = euler_phi(c);
  for (int i = lens - 1; i >= 0; i--) {
    if (s[i] == 0)
      s[i] = '9';
    else {
      s[i]--;
      break;
    }
  }
  bool flag = false;
  long long tn = 0;
  for (int i = 0; i < lens; i++) {
    if (tn >= phic) flag = true;
    if (flag)
      tn = (tn * 10 + s[i] - '0') % phic;
    else
      tn = tn * 10 + s[i] - '0';
  }
  if (flag) tn += phic;
  long long ans = Pow(tb, tn, c);
  ans = ans * tb1 % c;
  if (ans == 0)
    printf("%I64d\n", c);
  else
    printf("%I64d\n", ans);
  return 0;
}
