class Union_Find: def __init__(self, n=0): self.vertices = n self.mother = [-1 for i in range(self.vertices)] self.size_temp = [1 for i in range(self.vertices)] def root(self, x): normalize_v = [] while x != -1: normalize_v.append(x) y = x x = self.mother[x] for i in normalize_v[:-1]: self.mother[i] = y return y def union(self, x, y): root_x = self.root(x) root_y = self.root(y) if root_x != root_y: self.mother[root_x] = root_y self.size_temp[root_y] += self.size_temp[root_x] def find(self, x, y): if self.root(x) == self.root(y): return True else: return False def size(self, x): return self.size_temp[self.root(x)] n,m = map(int, input().split()) e = [[] for i in range(n)] uf = Union_Find(n) for i in range(m): a,b = map(lambda x:int(x)-1, input().split()) y = int(input()) e[a].append([b,y]) e[b].append([a,y]) uf.union(a, b) s = [] for i in range(n): if i == uf.root(i): s.append(i) tb = [-1] * n f = True for i in s: tb[i] = 0 while s: ns = [] for i in s: for j,k in e[i]: if tb[j] != -1: if tb[i] ^ tb[j] ^ k != 0: f = False break else: tb[j] = tb[i] ^ k ns.append(j) if not f: break s = ns if not f: break if f: print(*tb, sep="\n") else: print(-1)