from collections import deque n,m = map(int,input().split()) e = [[] for i in range(n+1)] for i in range(m): a,b = map(int,input().split()) y = int(input()) e[a].append((b,y)) e[b].append((a,y)) ans = [-1]*(n+1) for i in range(1,n+1): if ans[i] >= 0: continue q = deque([i]) ans[i] = 0 while q: now = q.popleft() for nex,y in e[now]: if ans[nex] >= 0: if ans[now]^ans[nex] != y: print(-1) exit() continue ans[nex] = y^ans[now] q.append(nex) for i in ans[1:]: print(i)