N,M = map(int,input().split()) e = [[] for _ in range(N)] from collections import defaultdict d = defaultdict(lambda:-1) for _ in range(M): a,b = map(int,input().split()) a -= 1 b -= 1 y = int(input()) if d[(a,b)] != -1: if d[(a,b)] != y: print(-1) exit() d[(a,b)] = y d[(b,a)] = y e[a].append(b) e[b].append(a) X = [-1]*N X[0] = 0 from collections import deque v = deque() v.append(0) while v: x = v.popleft() for ix in e[x]: if X[ix] == -1: X[ix] = X[x]^d[(x,ix)] v.append(ix) if X[ix] != -1: if X[ix] != X[x]^d[(x,ix)]: print(-1) exit() print(*X,sep='\n')