#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 + ((unsigned int)(x * mm))*m) >> rsh; return t>stepsh] = MR(x); } }while(x!=init); return mu; } ll naive(ll x, ll n){ x = MR(r2 * x); while(n--){ x = MR(x*(x+m4)); } return MR(x); } int main(){ cin>>n>>m; if(m == 2){ cout<<0<>stepsh]; n = n & (step-1); x = naive(x,n); cout<