""" https://yukicoder.me/problems/no/1566 Nが非常に大きいので、算数で行けるはず 全ての2 x 2 で、1は偶数個である必要がる 11 11 101 010 101 111 111 111 000 111 000 """ import sys from sys import stdin N,M = map(int,stdin.readline().split()) lis = [ [None,None] , [None,None] ] ans = 1 for i in range(M): x,y,z = map(int,stdin.readline().split()) if lis[x % 2][y % 2] == None: lis[x % 2][y % 2] = z elif lis[x % 2][y % 2] != z: print (0) sys.exit() lis = lis[0] + lis[1] ans = 0 for i in range(2**4): t1 = max(1,2**3 & i) t2 = max(1,2**2 & i) t3 = max(1,2**1 & i) t4 = max(1,2**0 & i) tlis = [t1,t2,t3,t4] if (t1+t2+t3+t4) % 2 == 1: continue for i in range(4): if lis[i] != None and tlis[i] != lis[i]: break else: ans += 1 print (ans)