#include <bits/stdc++.h>
using namespace std;
char b[1000005], s[1000005];
long long c;
int main() {
  int i, j;
  while (scanf("%s%s%I64d", b, s, &c) != EOF) {
    long long now = 0;
    int l1 = strlen(b), l2 = strlen(s);
    for (i = 0; i < l1; i++) {
      now *= 10;
      now += b[i] - '0';
      if (now >= c) now %= c;
    }
    long long ans = 1, tt = now;
    for (i = l2 - 1; i >= 0; i--) {
      if (s[i] > '0') {
        s[i]--;
        break;
      }
      if (s[i] <= '0') {
        s[i] += 9;
      }
    }
    for (i = l2 - 1; i >= 0; i--) {
      long long tp = now;
      for (j = 1; j < 10; j++) {
        if (s[i] - '0' == j) ans *= now;
        now *= tp;
        if (now >= c) now %= c;
      }
      if (ans >= c) ans %= c;
    }
    tt -= 1;
    if (tt < 0) tt += c;
    ans *= tt;
    if (ans >= c) ans %= c;
    if (ans == 0) ans = c;
    cout << ans << endl;
  }
  return 0;
}
