import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) N, M = map(int, input().split()) G = [[] for _ in range(N)] for _ in range(M): a, b = map(int, input().split()) a -= 1 b -= 1 y = int(input()) G[a].append((b, y)) G[b].append((a, y)) X = [-1] * N for i in range(N): if X[i] != -1: continue X[i] = 0 q = [i] while q: s = q.pop() for t, y in G[s]: if X[t] == -1: X[t] = y ^ X[s] q.append(t) continue if X[s] ^ X[t] != y: print(-1) exit() print(*X, sep="\n")