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;}g(a,n,m){return n?n%2?g(a*a%m,n/2,m)*a%m:g(a*a%m,n/2,m):1;}f(a,n,m){return!n?:m-1?n-1?n-3|a-2?g(a%m,n-2?p(m)+f(a,n-1,s):a,m):16%m:a%m:0;}n,m;main(a){scanf("%d%d%d",&a,&n,&m);n=!printf("%d",f(a,n,m));}