#include main(){ int64_t N,M,T,a=0,i=1;std::cin>>N>>M; std::vector F(5e6,1),R(N); for(F[0]=0;F[i]||F[i+1]>1;T=++i)F[i+2]=(F[i+1]+F[i])%N; for(R[0]++;i;i--)R[(F[i+2]-1+N)%N]+=(M-2)/T+(i<=(M-2)%T?1:0); for(auto r:R)a+=r*(r-1)/2; std::cout<<(M<3?(N>1?0:2):a+(F[(M-1)%T]%N?0:1)+(F[M%T]%N?0:2)); }