import sys from collections import deque input = sys.stdin.buffer.readline def solve(G): X = [None] * N nonvisited = set(range(N)) while nonvisited: v0 = nonvisited.pop() X[v0] = 0 d = deque([v0]) while d: v = d.pop() xv = X[v] for nv, y in G[v]: if nv in nonvisited: X[nv] = xv ^ y nonvisited.remove(nv) d.append(nv) else: if X[nv] != xv ^ y: return None return X N, M = map(int, input().split()) G = [[] for _ in range(N)] for _ in range(M): a, b = (int(x) - 1 for x in input().split()) y = int(input()) G[a].append((b, y)) G[b].append((a, y)) X = solve(G) if X is None: print(-1) else: print(*X, sep="\n")