#include #include using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int N,M; int X[3][3] = {}; int popcount(int x){ int res = 0; rep(i,9) res += (x>>i) & 1; return res; } int main(){ cin >> N >> M; rep(i,3) rep(j,3) X[i][j] = 0; rep(i,M){ int a,b,c; cin >> a >> b >> c; a--; b--; X[a%3][b%3] |= (1<<(1-c)); } for(int i=2; i> (x+y*2)) & 1; bool ok = true; rep(x,2) rep(y,2) if(X[x][y] & (1 << D[x][y])) ok = false; if(popcount(i) % 2 == 1) ok = false; if(ok) ans++; } } else{ rep(i,1<<9){ int D[3][3]; rep(x,3) rep(y,3) D[x][y] = (i >> (x+y*3)) & 1; bool ok = true; rep(x,3) rep(y,3) if(X[x][y] & (1 << D[x][y])) ok = false; if(popcount(i) % 2 != 1) ok = false; if(popcount(i & 0b000'011'011) % 2 != 0) ok = false; if(popcount(i & 0b000'110'110) % 2 != 0) ok = false; if(popcount(i & 0b000'111'111) % 2 != 0) ok = false; if(popcount(i & 0b011'011'000) % 2 != 0) ok = false; if(popcount(i & 0b110'110'000) % 2 != 0) ok = false; if(popcount(i & 0b111'111'000) % 2 != 0) ok = false; if(popcount(i & 0b011'000'011) % 2 != 0) ok = false; if(popcount(i & 0b110'000'110) % 2 != 0) ok = false; if(popcount(i & 0b111'000'111) % 2 != 0) ok = false; if(ok) ans++; } } cout << ans << "\n"; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_inst;