#include <bits/stdc++.h>
using namespace std;
bool is_Smaller_str(string str1, string str2) {
  long long n1 = str1.length(), n2 = str2.length();
  if (n1 < n2) return true;
  if (n2 < n1) return false;
  for (int i = 0; i < n1; i++)
    if (str1[i] < str2[i])
      return true;
    else if (str1[i] > str2[i])
      return false;
  return false;
}
long long mod(long long a, long long b) {
  long long ans = a / b;
  ans *= b;
  ans = a - ans;
  return ans;
}
string find_Diff_str(string str1, string str2) {
  if (is_Smaller_str(str1, str2)) swap(str1, str2);
  string str = "";
  long long n1 = str1.length(), n2 = str2.length();
  reverse(str1.begin(), str1.end());
  reverse(str2.begin(), str2.end());
  long long carry = 0;
  for (long long i = 0; i < n2; i++) {
    long long sub = ((str1[i] - '0') - (str2[i] - '0') - carry);
    if (sub < 0) {
      sub = sub + 10;
      carry = 1;
    } else
      carry = 0;
    str.push_back(sub + '0');
  }
  for (long long i = n2; i < n1; i++) {
    long long sub = ((str1[i] - '0') - carry);
    if (sub < 0) {
      sub = sub + 10;
      carry = 1;
    } else
      carry = 0;
    str.push_back(sub + '0');
  }
  reverse(str.begin(), str.end());
  return str;
}
string find_sum_str(string str1, string str2) {
  if (str1.length() > str2.length()) swap(str1, str2);
  string str = "";
  int n1 = str1.length(), n2 = str2.length();
  reverse(str1.begin(), str1.end());
  reverse(str2.begin(), str2.end());
  int carry = 0;
  for (int i = 0; i < n1; i++) {
    int sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
    str.push_back(sum % 10 + '0');
    carry = sum / 10;
  }
  for (int i = n1; i < n2; i++) {
    int sum = ((str2[i] - '0') + carry);
    str.push_back(sum % 10 + '0');
    carry = sum / 10;
  }
  if (carry) str.push_back(carry + '0');
  reverse(str.begin(), str.end());
  return str;
}
void FAST() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long Rselect(vector<long long> v, long long i, long long l, long long r);
long long partition(vector<long long> v, long long l, long long r);
void scanc(vector<char>& v, long long n) {
  for (long long i = 0; i < n; i++) {
    char num;
    cin >> num;
    v.push_back(num);
  }
}
void scanP(vector<pair<long long, long long> >& v, long long n) {
  for (long long i = 0; i < n; i++) {
    long long num1, num2;
    cin >> num1 >> num2;
    pair<long long, long long> p = {num1, num2};
    v.push_back(p);
  }
}
template <typename T>
ostream& operator<<(ostream& stream, const vector<T>& vec) {
  for (auto& i : vec) {
    stream << i << ' ';
  }
  stream << '\n';
  return stream;
}
template <class T>
istream& operator>>(istream& stream, vector<T>& vec) {
  for (auto& i : vec) {
    stream >> i;
  }
  return stream;
}
void solve();
void scanN(vector<long long>& v, long long n) {
  for (long long i = 0; i < n; i++) {
    int num;
    cin >> num;
    v.push_back(num);
  }
}
void scans(vector<string>& v, long long n) {
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    v.push_back(s);
  }
}
long long modfactorial(long long n, long long p) {
  if (n >= p) return 0;
  long long result = 1;
  for (long long i = 1; i < n + 1; i++) {
    result = result * i;
    result = result % p;
  }
  return result;
}
long long MI(long long a, long long b, long long s0 = 1, long long s1 = 0) {
  long long k = b;
  if (b == 0)
    return s0;
  else {
    return MI(b, a % b, s1, s0 - s1 * (a / b));
  }
}
long long choose(long long a, long long b, long long c = 1000000007) {
  if (a < b) return 0;
  long long x = modfactorial(a, c);
  long long y = modfactorial(b, c);
  long long z = modfactorial(a - b, c);
  long long y_ = MI(y, c, 1, 0);
  if (y_ < 0) y_ = y_ + c;
  long long z_ = MI(z, c, 1, 0);
  if (z_ < 0) z_ = z_ + c;
  long long mul = (x * y_) % c;
  mul = (mul * z_) % c;
  return mul;
}
void SOLT() {
  int test;
  cin >> test;
  while (test--) {
    solve();
  }
}
long long modpow(long long n, long long p, long long k) {
  n = n % k;
  long long res = 1;
  while (p > 0) {
    if (p % 2 == 1) res = res * n % k;
    n = n * n % k;
    p = p / 2;
  }
  return res;
}
long long str_mod_num(string b, long long c) {
  long long k = b.size();
  vector<long long> mod(k + 1);
  mod[0] = 1;
  for (long long i = 1; i < k + 1; i++) {
    mod[i] = 10 * mod[i - 1];
    mod[i] %= c;
  }
  long long rem = 0;
  for (long long i = k - 1; i > -1; i--) {
    long long num = b[i] - '0';
    num *= mod[k - 1 - i];
    num %= c;
    rem += num;
    rem %= c;
  }
  return rem;
}
long long str_pow_str_mod_num(string b, string n, long long c) {
  long long tr = n.size();
  long long rem = str_mod_num(b, c);
  long long time_pass = n[0] - '0';
  long long ans = modpow(rem, time_pass, c);
  for (long long i = 1; i < tr; i++) {
    time_pass = n[i] - '0';
    ans = modpow(ans, 10, c);
    ans *= modpow(rem, time_pass, c);
    ans %= c;
  }
  return ans;
}
bool isPrime(long long a) {
  for (long long i = 2; i * i <= a; ++i)
    if (a % i == 0) return false;
  return true;
}
bool useflushdivisible(long long a) {
  cout << a << endl;
  fflush(stdout);
  char sl[10];
  scanf("%s", sl);
  return sl[0] == 'y' || sl[0] == 'Y';
}
long long phi(long long n) {
  long long result = n;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n = n / i;
      result = result - result / i;
    }
  }
  if (n > 1) result = result - result / n;
  return result;
}
bool cmp(pair<long long, long long> x, pair<long long, long long> y) {
  return x.first < y.first;
}
vector<long long> getfactors(long long n) {
  vector<long long> factors;
  for (long long i = 1; i < sqrt(n) + 1; i++) {
    if (n % i == 0) {
      factors.push_back(i);
      if (i != (n / i)) factors.push_back(n / i);
    }
  }
  return factors;
}
long long Binary_Search(vector<long long>& a, long long low, long long high,
                        long long key) {
  if (high < low) return a[low - 1];
  int mid = (high + low) / 2;
  if (key == a[mid])
    return a[mid];
  else if (key < a[mid])
    return Binary_Search(a, low, mid - 1, key);
  return Binary_Search(a, mid + 1, high, key);
}
class bigint {
 public:
  bigint() { str = ""; }
  bigint(const string& x) { str = x; }
  bigint(const char* x) { str = x; }
  long long Length() const { return str.size(); }
  void Push_back(char s) {
    if ((s - '0' < 0 || s - '0' > 9) && s != '-')
      throw invalid_argument("Invalid argument");
    else
      str.push_back(s);
  }
  void Reverse() { reverse(begin(str), end(str)); }
  void Clear() { str.clear(); }
  char& operator[](long long i) {
    if (i >= str.size()) throw invalid_argument("Invalid argument");
    return str[i];
  }
  void operator=(const char* x) { str = x; }
  void operator=(string x) { str = x; }
  long long bigint_to_int() {
    reverse(begin(str), end(str));
    long long ind = str.size();
    for (long long i = str.size() - 1; i > -1; i--) {
      if (str[i] != '0' && str[i] != '-') {
        ind = i + 1;
        break;
      }
    }
    long long ans = 0;
    for (long long i = 0; i < ind; i++) {
      ans += ((str[i] - '0') * (pow(10, i)));
    }
    if (str[str.size() - 1] == '-') ans = -ans;
    return ans;
  }
  void Clip() {
    long long ind = -1;
    long long t = -1;
    if (str[0] == '-')
      t = 1;
    else
      t = 0;
    for (long long i = t; i < str.size(); i++) {
      if (str[i] != '0') {
        ind = i;
        break;
      }
    }
    if (ind == -1)
      str = "0";
    else {
      string p = str;
      bool nag = false;
      if (str[0] == '-') nag = true;
      str.clear();
      if (nag) str.push_back('-');
      for (long long i = ind; i < p.size(); i++) str.push_back(p[i]);
    }
  }
  void Abs() {
    string p;
    if (str[0] == '-') {
      for (long long i = 1; i < str.size(); i++) {
        p.push_back(str[i]);
      }
      str = p;
    } else {
      for (long long i = 0; i < str.size(); i++) p.push_back(str[i]);
      str = p;
    }
  }
  void Neg() {
    string p;
    if (str[0] == '-') {
      for (long long i = 1; i < str.size(); i++) {
        p.push_back(str[i]);
      }
      str = p;
    } else if (str[0] != '-') {
      p.push_back('-');
      bool ind = false;
      for (long long i = 0; i < str.size(); i++) {
        if (ind) p.push_back(str[i]);
        if (!ind && str[i] != '0') {
          ind = true;
          p.push_back(str[i]);
        }
      }
      if (p.size() != 1)
        str = p;
      else
        str = "0";
    }
  }
  string str;
};
bigint operator-(bigint str1, bigint str2);
bigint operator+(bigint str1, bigint str2);
bigint operator*(bigint num1, bigint num2);
bigint operator/(bigint number, long long divisor);
void swap(bigint& x, bigint& y) {
  bigint TEMP;
  for (long long i = 0; i < x.Length(); i++) {
    TEMP.Push_back(x[i]);
  }
  x.Clear();
  for (long long i = 0; i < y.Length(); i++) {
    x.Push_back(y[i]);
  }
  y.Clear();
  for (long long i = 0; i < TEMP.Length(); i++) {
    y.Push_back(TEMP[i]);
  }
  TEMP.Clear();
}
istream& operator>>(istream& stream, bigint& num) {
  stream >> num.str;
  return stream;
}
ostream& operator<<(ostream& stream, bigint x) {
  long long n = x.Length();
  for (int i = 0; i < n; i++) stream << x[i];
  return stream;
}
bool operator<(bigint& str1, bigint& str2) {
  str1.Clip();
  str2.Clip();
  long long n1 = str1.Length(), n2 = str2.Length();
  if (str1[0] == '-' && str2[0] != '-') return true;
  if (str1[0] != '-' && str2[0] == '-') return false;
  if (str1[0] == '-' && str2[0] == '-') {
    str1.Abs();
    str2.Abs();
    return str2 < str1;
  }
  if (n1 < n2) return true;
  if (n2 < n1) return false;
  for (int i = 0; i < n1; i++)
    if (str1[i] < str2[i])
      return true;
    else if (str1[i] > str2[i])
      return false;
  return false;
}
bool operator==(bigint& str1, bigint& str2) {
  str1.Clip();
  str2.Clip();
  long long n1 = str1.Length();
  long long n2 = str2.Length();
  if (n1 < n2) return false;
  if (n2 < n1) return false;
  for (long long i = 0; i < n1; i++)
    if (str1[i] != str2[i]) return false;
  return true;
}
bool operator!=(bigint& str1, bigint& str2) {
  if (str1 == str2) return false;
  return true;
}
bool operator>(bigint& str1, bigint& str2) {
  if (str1 < str2) return false;
  if (str1 == str2) return false;
  return true;
}
bool operator<=(bigint& str1, bigint& str2) {
  if (str1 < str2 || str1 == str2) {
    return true;
  }
  return false;
}
bool operator>=(bigint& str1, bigint& str2) {
  if (str1 > str2 || str1 == str2) return true;
  return false;
}
bigint operator-(bigint str1, bigint str2) {
  bigint s1 = str1, s2 = str2;
  s1.Abs();
  s2.Abs();
  bool swapped = false;
  if (s1 < s2) {
    swap(str1, str2);
    swapped = true;
  }
  bigint zero = "0";
  if (str1 < zero && str2 < zero) {
    str1.Abs();
    str2.Abs();
    bigint str = str2 - str1;
    str.Clip();
    if (!swapped) {
      str.Neg();
      return str;
    } else {
      return str;
    }
  } else if (str2 < zero) {
    str2.Abs();
    bigint str = str1 + str2;
    str.Clip();
    if (!swapped)
      return str;
    else {
      str.Neg();
      return str;
    }
  } else if (str1 < zero) {
    str1.Abs();
    bigint str = str1 + str2;
    if (!swapped) {
      str.Neg();
      return str;
    } else {
      return str;
    }
  }
  bigint str;
  long long n1 = str1.Length(), n2 = str2.Length();
  str1.Reverse();
  str2.Reverse();
  long long carry = 0;
  for (long long i = 0; i < n2; i++) {
    long long sub = ((str1[i] - '0') - (str2[i] - '0') - carry);
    if (sub < 0) {
      sub = sub + 10;
      carry = 1;
    } else
      carry = 0;
    str.Push_back(sub + '0');
  }
  for (long long i = n2; i < n1; i++) {
    long long sub = ((str1[i] - '0') - carry);
    if (sub < 0) {
      sub = sub + 10;
      carry = 1;
    } else
      carry = 0;
    str.Push_back(sub + '0');
  }
  str.Reverse();
  str.Clip();
  if (swapped) str.Neg();
  return str;
}
bigint operator+(bigint str1, bigint str2) {
  bigint s1 = str1;
  bigint s2 = str2;
  s1.Abs();
  s2.Abs();
  if (s1.Length() > s2.Length()) {
    swap(str1, str2);
  }
  bigint zero = "0";
  bigint str;
  if (str2 < zero && str1 < zero) {
    str1.Abs();
    str2.Abs();
    str = str1 + str2;
    str.Clip();
    str.Neg();
    return str;
  }
  if (str1 < zero) {
    str1.Abs();
    str = str2 - str1;
    str.Clip();
    return str;
  }
  if (str2 < zero) {
    str2.Abs();
    str = str1 - str2;
    str.Clip();
    return str;
  }
  int n1 = str1.Length(), n2 = str2.Length();
  str1.Reverse();
  str2.Reverse();
  int carry = 0;
  for (int i = 0; i < n1; i++) {
    int sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
    str.Push_back(sum % 10 + '0');
    carry = sum / 10;
  }
  for (int i = n1; i < n2; i++) {
    int sum = ((str2[i] - '0') + carry);
    str.Push_back(sum % 10 + '0');
    carry = sum / 10;
  }
  if (carry) str.Push_back(carry + '0');
  str.Reverse();
  str.Clip();
  return str;
}
bigint convert(long long n) {
  bigint res;
  long long m = n;
  n = abs(n);
  while (n > 0) {
    long long rem = n % 10;
    char c = rem + '0';
    res.Push_back(c);
    n /= 10;
  }
  if (m < 0) {
    res.Push_back('-');
  }
  res.Reverse();
  res.Clip();
  return res;
}
long long operator%(bigint b, long long c) {
  bool neg = false;
  bigint zero = "0";
  if (b < zero) {
    neg = true;
    b.Abs();
  }
  long long k = b.Length();
  vector<long long> mod(k + 1);
  mod[0] = 1;
  for (long long i = 1; i < k + 1; i++) {
    mod[i] = 10 * mod[i - 1];
    mod[i] %= c;
  }
  long long rem = 0;
  for (long long i = k - 1; i > -1; i--) {
    long long num = b[i] - '0';
    num *= mod[k - 1 - i];
    num %= c;
    rem += num;
    rem %= c;
  }
  if (!neg)
    return rem;
  else {
    rem = c - rem;
    return rem;
  }
}
bigint operator*(bigint num1, bigint num2) {
  bigint zero;
  zero = "0";
  if (num1 < zero && num2 < zero) {
    num1.Abs();
    num2.Abs();
    bigint ans = num1 * num2;
    ans.Clip();
    return ans;
  } else if (num1 < zero || num2 < zero) {
    num1.Abs();
    num2.Abs();
    bigint ans = num1 * num2;
    ans.Neg();
    ans.Clip();
    return ans;
  }
  int len1 = num1.Length();
  int len2 = num2.Length();
  if (len1 == 0 || len2 == 0) {
    return zero;
  }
  vector<long long> result(len1 + len2, 0);
  int i_n1 = 0;
  int i_n2 = 0;
  for (int i = len1 - 1; i >= 0; i--) {
    int carry = 0;
    int n1 = num1[i] - '0';
    i_n2 = 0;
    for (int j = len2 - 1; j >= 0; j--) {
      int n2 = num2[j] - '0';
      int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
      carry = sum / 10;
      result[i_n1 + i_n2] = sum % 10;
      i_n2++;
    }
    if (carry > 0) result[i_n1 + i_n2] += carry;
    i_n1++;
  }
  int i = result.size() - 1;
  while (i >= 0 && result[i] == 0) i--;
  if (i == -1) return zero;
  string s = "";
  while (i >= 0) s += std::to_string(result[i--]);
  bigint b;
  b = s;
  b.Clip();
  return b;
}
bigint operator/(bigint number, long long divisor) {
  bigint ans;
  bigint zero;
  zero = "0";
  if (number < zero && divisor < 0) {
    number.Abs();
    divisor = -divisor;
    ans = number / divisor;
    return ans;
  } else if (divisor == 0) {
    throw invalid_argument("Invalid argument");
    return zero;
  } else if (number < zero || divisor < 0) {
    number.Abs();
    divisor = abs(divisor);
    ans = number / divisor;
    ans.Neg();
    return ans;
  }
  int idx = 0;
  int temp = number[idx] - '0';
  while (temp < divisor) {
    if (idx != number.Length() - 1)
      temp = temp * 10 + (number[++idx] - '0');
    else
      break;
  }
  while (number.Length() > idx) {
    long long c = temp / divisor;
    ans.Push_back(c + '0');
    if (idx != number.Length() - 1)
      temp = (temp % divisor) * 10 + number[++idx] - '0';
    else
      break;
  }
  if (ans.Length() == 0) return zero;
  return ans;
}
bigint operator^(bigint b, bigint n) {
  bigint res;
  res = "1";
  bigint zero;
  zero = "0";
  while (n > zero) {
    if (n % 2 == 1) {
      bigint temp;
      temp = res * b;
      res = temp;
    }
    bigint temp;
    temp = b * b;
    b = temp;
    temp = n / 2;
    n = temp;
  }
  res.Clip();
  return res;
}
long long powmod(bigint b, bigint n, long long MODULO) {
  long long tr = n.Length();
  long long rem;
  rem = b % MODULO;
  long long time_pass = n[0] - '0';
  long long ans = 0;
  ans = modpow(rem, time_pass, MODULO);
  for (long long i = 1; i < tr; i++) {
    ans = modpow(ans, 10, MODULO);
    time_pass = n[i] - '0';
    ans = ans * modpow(rem, time_pass, MODULO);
    ans %= MODULO;
  }
  return ans;
}
bigint Min(bigint a, bigint b) {
  if (a <= b) return a;
  return b;
}
bigint Max(bigint a, bigint b) {
  if (a >= b) return a;
  return b;
}
bigint Binary_Form(bigint a) {
  bigint zero = "0";
  bigint ans;
  while (a > zero) {
    long long rem = a % 2;
    ans.Push_back(rem + '0');
    bigint temp = a / 2;
    a = temp;
  }
  ans.Reverse();
  return ans;
}
bigint bin_dec(bigint a) {
  long long n = a.Length();
  bigint two = "2";
  bigint ans = "0";
  for (long long i = n - 1; i > -1; i--) {
    bigint I = convert(i);
    bigint temp = two ^ I;
    bigint one = convert(a[n - 1 - i] - '0');
    temp = temp * one;
    ans = ans + temp;
  }
  return ans;
}
bigint operator&(bigint a, bigint b) {
  bigint a1 = Binary_Form(a);
  bigint b1 = Binary_Form(b);
  if (a1 < b1) {
    swap(a1, b1);
  }
  long long n1 = a1.Length();
  long long n2 = b1.Length();
  a1.Reverse();
  b1.Reverse();
  bigint ans;
  for (long long i = 0; i < n1; i++) {
    if (i < n2) {
      long long num1 = a1[i] - '0';
      long long num2 = b1[i] - '0';
      long long num = num1 & num2;
      ans.Push_back(num + '0');
    } else {
      ans.Push_back('0');
    }
  }
  ans.Reverse();
  ans = bin_dec(ans);
  ans.Clip();
  return ans;
}
bigint operator|(bigint a, bigint b) {
  bigint a1 = Binary_Form(a);
  bigint b1 = Binary_Form(b);
  if (a1 < b1) {
    swap(a1, b1);
  }
  long long n1 = a1.Length();
  long long n2 = b1.Length();
  a1.Reverse();
  b1.Reverse();
  bigint ans;
  for (long long i = 0; i < n1; i++) {
    if (i < n2) {
      long long num1 = a1[i] - '0';
      long long num2 = b1[i] - '0';
      long long num = num1 | num2;
      ans.Push_back(num + '0');
    } else {
      ans.Push_back(a1[i]);
    }
  }
  ans.Reverse();
  ans = bin_dec(ans);
  ans.Clip();
  return ans;
}
int main() {
  FAST();
  srand(time(0));
  int test = 1;
  for (int tt = 1; tt <= test; tt++) solve();
  return 0;
}
void solve() {
  bigint b, n;
  long long c;
  cin >> b >> n >> c;
  long long ans1 = powmod(b, n, c);
  bigint n1 = n - "1";
  bigint zero = "0";
  long long ans2 = powmod(b, n1, c);
  long long ans = ans1 - ans2;
  while (ans <= 0) {
    ans += c;
  }
  cout << ans;
}
long long Rselect(vector<long long> v, long long i, long long l, long long r) {
  if (l == r) return v[l];
  long long pivot = partition(v, l, r);
  if (pivot == i)
    return v[pivot - 1];
  else if (pivot < i) {
    return Rselect(v, i, pivot, r);
  } else {
    return Rselect(v, i, l, pivot - 2);
  }
}
long long partition(vector<long long> v, long long l, long long r) {
  long long pivot_index = rand() % (r - l + 1) + l;
  swap(v[pivot_index], v[l]);
  long long i = l + 1, j = l + 1;
  while (j <= r) {
    if (v[j] < v[l]) {
      swap(v[j], v[i]);
      i++;
    }
    j++;
  }
  swap(v[l], v[i - 1]);
  return i;
}
