#include <bits/stdc++.h>
using namespace std;
class bignum {
 public:
  deque<int> v;
  inline void get() {
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); ++i) v.push_front(s[i] - '0');
  }
  inline void print() {
    for (int i = v.size() - 1; i >= 0; --i) cout << v[i];
    cout << endl;
  }
};
bignum n, b;
long long mod;
long long B;
inline long long operator%(bignum &fi, long long se) {
  long long ret = 0;
  for (int i = fi.v.size() - 1; i >= 0; --i) ret = (ret * 10 + fi.v[i]) % se;
  return ret;
}
inline long long POW(long long a, bignum &fi, long long Mod) {
  long long ret = 1;
  long long now = a;
  for (int i = 0; i < fi.v.size(); ++i) {
    for (int j = 0; j < fi.v[i]; ++j) ret *= now, ret %= Mod;
    long long cp = now;
    for (int j = 1; j < 10; ++j) now *= cp, now %= Mod;
  }
  return ret;
}
inline void minus_(bignum &fi) {
  int ind = 0;
  while (fi.v[ind] == 0) {
    fi.v[ind] = 9;
    ind++;
  }
  fi.v[ind]--;
  if (fi.v.size() > 0 && ind == fi.v.size() - 1 && fi.v[ind] == 0)
    fi.v.pop_back();
}
int main() {
  ios_base::sync_with_stdio(false);
  b.get();
  n.get();
  cin >> mod;
  B = b % mod;
  minus_(n);
  long long ans = POW(B, n, mod);
  ans *= ((B - 1 + mod) % mod);
  ans %= mod;
  if (ans == 0)
    cout << mod << endl;
  else
    cout << ans << endl;
  return 0;
}
