import sys sys.setrecursionlimit(10 ** 6) class WeightedUnionFind: def __init__(self, n): self.node = [-1 for _ in range(n)] self.value = [0 for _ in range(n)] def root(self, x): if self.node[x] < 0: return x else: r = self.root(self.node[x]) self.value[x] ^= self.value[self.node[x]] self.node[x] = r return r def size(self, x): x = self.root(x) return (- self.node[x]) def is_same(self, x, y): return self.root(x) == self.root(y) def weight(self, x): self.root(x) return self.value[x] def diff(self, x, y): return self.weight(y) ^ self.weight(x) def unite(self, x, y, w): w ^= self.weight(x) w ^= self.weight(y) rx = self.root(x) ry = self.root(y) if rx == ry: return dx = self.node[x] dy = self.node[y] if dx <= dy: self.node[ry] = rx self.node[rx] += dy self.value[ry] = w else: self.node[rx] = ry self.node[ry] += dx self.value[rx] = w n, m = map(int, input().split()) wuf = WeightedUnionFind(n) for _ in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 y = int(input()) if not wuf.is_same(a, b): wuf.unite(a, b, y) elif wuf.diff(a, b) != y: print("-1") exit() for v in range(n): print(wuf.weight(v))