#include using namespace std; typedef unsigned long long ll; typedef vector vi; typedef vector vl; typedef pair pii; typedef pair pll; typedef int _loop_int; #define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i) #define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i) #define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i) #define DEBUG(x) cout<<#x<<": "< 1){ if(!(t%2)){ t += m; res += i; } t /= 2; rr /= 2; i *= 2; } return res; } inline ll MR(ll x){ ll t = (x + ((x * mm)&rmsk)*m) >> rsh; return t= m) x4 -= m; x = MR(x*x4); if((mu&(step-1))==0){ memo[mu>>stepsh] = MR(x); } }while(x!=init); return mu; } ll naive(ll x, ll n){ x = MR(r2 * x); while(n--){ ll x4 = x+m4; if(x4 >= m) x4 -= m; x = MR(x*x4); } return MR(x); } ll solve(){ if(m==2)return 0ll; // montgomery r2 = r%m*r%m; mm = calc_mm(); m4 = MR(r2 * 4); // solve if(n <= LAMBDA){ return naive(2,n); } ll x = naive(2,LAMBDA); n -= LAMBDA; int mu = floyd_cycle_algorithm(x); n %= mu; x = memo[n>>stepsh]; n = n & (step-1); x = naive(x,n); return x; } int main(){ // while(true){ // m = rand()%1024; // if(m<=1)continue; // bool ok = true; // for(int x=2;x*x<=m;x++)if(m%x==0){ // ok = false;break; // } // if(!ok)continue; // ll v = 2; // FOR(i,1,10000){ // n = i; // v = v * (v+4) % m; // ll x = solve(); // if(v != x){ // puts("WRONG"); // printf("n = %lld, m = %lld\n", (ll)i, m); // printf("my solver : %lld\n", x); // printf("naive : %lld\n", v); // exit(0); // } // } // } cin>>n>>m; cout<