坑点:开long long
#include <bits/stdc++.h>
#define int long long
using namespace std;
int m;
int quie(int a, int k) {
int res = 1ll;
while (k) {
if (k & 1) res = res * a % m;
a = a * a % m;
k >>= 1;
}
return res;
}
int a, b, p = 1, flag;
signed main() {
scanf("%lld%lld", &a, &m);
a %= m;
int M = m;
for (int i = 2; i * i <= M; ++i) {
if (M % i) continue;
p *= i - 1;
M /= i;
while (M % i == 0)
p *= i,
M /= i;
}
if (M > 1) p *= M - 1;
char ch;
while ((ch = getchar()) < '0' || ch > '9') ;
while (b = b * 10ll + (ch ^ '0'), (ch = getchar()) >= '0' && ch <= '9')
if (b >= p) flag = 1, b %= p;
if (b >= p) flag = 1, b %= p;
if (flag) b += p;
printf("%lld", quie(a, b));
}