import sys input = sys.stdin.readline class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] self.diff_weight = [0 for _ in range(n)] self.flag = 1 def find(self, x): if self.par[x] == x: return x else: root = self.find(self.par[x]) self.diff_weight[x] ^= self.diff_weight[self.par[x]] self.par[x] = root return self.par[x] def union(self, x, y, w): rx = self.find(x) ry = self.find(y) w ^= self.diff_weight[y]^self.diff_weight[x] if rx != ry: if self.rank[rx] < self.rank[ry]: rx, ry = ry, rx if self.rank[rx] == self.rank[ry]: self.rank[rx] += 1 self.par[ry] = rx self.size[rx] += self.size[ry] self.diff_weight[ry] = w else: if w: self.flag = 0 def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] def diff(self, x, y): return self.diff_weight[y]^self.diff_weight[x] N, M = map(int, input().split()) U = UnionFind(N) for i in range(M): A, B = map(int, input().split()) A, B = A - 1, B - 1 Y = int(input()) U.union(A, B, Y) if U.flag: for i in range(N): U.find(i) print(U.diff_weight[i]) else: print(-1)