from collections import deque 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() for x, y in G[v]: if x in nonvisited: X[x] = X[v] ^ y nonvisited.remove(x) d.append(x) else: if X[x] != X[v] ^ 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")