#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int b,p,k,a;
int intf(int p)
{
if(p==0) return 1;
int t=intf(p/2)%k;
t=(t*t)%k;
if(p%2==1)
{
t=(t*b)%k;
}
return t;
}
int main()
{
cin>>b>>p>>k>>a;
int t=b;
b=b%k;
printf("%d",intf(p));
return 0;
}