N,M=map(int,input().split()) Q=[list(map(int,input().split())) for i in range(M)] for i in range(M): Q[i][2]^=1 Q[i][0]-=1 Q[i][1]-=1 D=dict() for i in range(M): D[(Q[i][0],Q[i][1])]=i if N>=8: if max([Q[i][2] for i in range(M)]): print(0) else: print(1) exit() def f(a): for i in range(N): for j in range(N): if D.get((i,j),-1)>=0: if Q[D[(i,j)]][2]!=a[i][j]: return 0 for k in range(2,N+1): if i+k<=N and j+k<=N: s=0 for x in range(i,i+k): for y in range(j,j+k): s^=a[x][y] if s: return 0 return 1 ANS=0 S=set() for i in range(1<>k)&1: for l in range(N): A[k][l]^=1 if (j>>k)&1: for l in range(N): A[l][k]^=1 if f(A): S.add(tuple([A[x//N][x%N] for x in range(N*N)])) print(len(S))