R=range I=lambda:map(int,input().split()) T,m=I() M=[m] P=set() def G(a,b): while a:a,b=b%a,a return b while M: n=M.pop() if n in P:continue if n<99: for i in R(2,n+1): if n%i<1: P.add(i) while n%i<1:n//=i else: for t in R(99): x,d=t,1 y=(x*x+1)%n while d<2:x,y,d=(x*x+1)%n,(y**4+2*y*y+2)%n,G(abs(x-y),n) if d>i&1<1:c[d]=c[d]*c[d|(1<>i&1<1])%2] print((v-(m<2))%Q)