from collections import defaultdict n, m = map(int, input().split()) graph = [[] for _ in range(n)] memo = defaultdict(lambda: defaultdict(lambda: -1)) for _ in range(m): a, b = map(int, input().split()) y = int(input()) a, b = a - 1, b - 1 graph[a].append(b) graph[b].append(a) if a > b: b, a = a, b if memo[a][b] >= 0: if memo[a][b] == y: continue else: print(-1) exit() memo[a][b] = y used = [0] * n ans = [-1] * n for i in range(n): if not used[i]: stack = [i] used[i] = 1 ans[i] = 0 while stack: v = stack.pop() now = ans[v] for u in graph[v]: if u < v: temp = memo[u][v] else: temp = memo[v][u] if used[u]: if ans[u] ^ ans[v] == temp: continue else: print(-1) exit() used[u] = 1 ans[u] = temp ^ ans[v] stack.append(u) for a in ans: print(a)