s,i;p(m){i=1;for(s=m;m/++i;)for(s-=m%i?0:s/i;m%i<1;m/=i);return s;}r;g(a,n,m){for(r=1;n;n/=2)n&1?r=r*a%m:0,a=a*a%m;}f(a,n,m){return~-m?n?g(a%m,--n?n-1?p(m)+f(a,n,s):a:1,m),r:1:0;}n,m;main(a){scanf("%d%d%d",&a,&n,&m);n=!printf("%d",f(a,n,m));}