import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 10**16 md = 998244353 # md = 10**9+7 n,m=LI() xyz=LLI(m) if n>=7: for x,y,z in xyz: if z==0: print(0) exit() print(1) exit() xyz=[(x-1,y-1,z) for x,y,z in xyz] ans=0 for bit in range(1 << 2*n-1): tt = [[0]*n for _ in range(n)] for i in range(n): tt[i][0] = bit >> i & 1 for j in range(1, n): tt[0][j] = bit >> j+n-1 & 1 for i in range(1, n): for j in range(1, n): s = tt[i-1][j-1] ^ tt[i][j-1] ^ tt[i-1][j] tt[i][j] = s cs = [[0]*(n+1) for _ in range(n+1)] for i in range(n): for j in range(n): cs[i+1][j+1] = cs[i+1][j] ^ tt[i][j] for j in range(n): for i in range(n): cs[i+1][j+1] ^= cs[i][j+1] def ok(): for x,y,z in xyz: if tt[x][y]==z:return False for d in range(3, n+1): for i in range(n-d+1): for j in range(n-d+1): x = cs[i][j] ^ cs[i+d][j+d] ^ cs[i+d][j] ^ cs[i][j+d] if x: return False return True # print("OK" if ok() else "NG") if ok(): ans+=1 # p2D(tt) # print() print(ans)