var
a,c,b,p,k:longint;
begin
assign(input,'dmod.in');
assign(output,'dmod.out');
reset(input);
rewrite(output);
readln(b,p,k);
a:=b mod k;
for c:=2 to p do
begin
a:=(a*(b mod k))mod k;
if a=b mod k then break;
end;
if c<>p then begin
a:=1;
if p mod(c-1)=0 then for c:=1 to c-1 do
a:=(a*(b mod k))mod k else
for c:=1 to p mod(c-1) do
a:=(a*(b mod k))mod k;
end;
writeln(a);
close(input);
close(output);
end.