#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T>
inline void read(T &v) {
  char ch;
  while (ch = getchar())
    if (ch <= '9' && ch >= '0') break;
  v = ch - '0';
  while (ch = getchar())
    if (ch <= '9' && ch >= '0')
      v = (v << 1) + (v << 3) + ch - '0';
    else
      break;
}
const int MX = 1111111;
inline long long eular(long long x) {
  long long ret = x, i;
  for (i = 2; i <= sqrt(x); ++i)
    if (0 == x % i) {
      ret -= ret / i;
      do {
        x /= i;
      } while (0 == x % i);
    }
  if (x > 1) ret -= ret / x;
  return ret;
}
inline long long quickpow(long long a, long long b, long long c) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % c;
    b >>= 1;
    a *= a;
    a %= c;
  }
  return ret;
}
long long bb[MX << 2];
int main() {
  string a, b;
  long long c, i;
  cin >> a >> b >> c;
  long long phi = eular(c), ra = 0;
  for (i = 0; a[i]; ++i) {
    ra = (ra << 1) + (ra << 3) + a[i] - '0';
    ra %= c;
  }
  long long rb = 0, top = 0;
  int len = b.size();
  for (i = len - 1; i >= 0; --i) bb[top++] = b[i] - '0';
  int pos = 0;
  while (bb[pos] == 0) {
    bb[pos] = 9;
    ++pos;
  }
  bb[pos]--;
  for (i = top; i >= 0; --i)
    if (bb[i]) break;
  len = i;
  bool flag = false;
  for (; i >= 0; --i) {
    rb = (rb << 1) + (rb << 3) + bb[i];
    while (rb >= phi) {
      rb -= phi;
      flag = true;
    }
  }
  if (flag) rb += phi;
  long long ret = quickpow(ra, rb, c), Asn = ret, Ans = 0;
  len = a.size();
  long long now = 0;
  top = 0;
  memset(bb, 0, sizeof bb);
  for (i = len - 1; i >= 0; --i) {
    bb[top++] = a[i] - '0';
  }
  pos = 0;
  while (0 == bb[pos]) {
    bb[pos] = 9;
    ++pos;
  }
  bb[pos]--;
  for (i = 0; i < top + MX; ++i) {
    bb[i] = bb[i] * ret + now;
    now = bb[i] / 10;
    bb[i] %= 10;
  }
  for (i = top + MX; i >= 0; --i)
    if (bb[i]) break;
  len = i;
  for (; i >= 0; --i) {
    Ans = Ans * 10 + bb[i];
    Ans %= c;
  }
  ret = Ans;
  if (ret)
    cout << ret << endl;
  else
    cout << c << endl;
  return 0;
}
