import sys input = sys.stdin.readline n, m = map(int, input().split()) edges = [[] for _ in range(n)] for _ in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 y = int(input()) edges[a].append((b, y)) edges[b].append((a, y)) X = [0] * n for k in range(30): bi = [-1] * n for i in range(n): if bi[i] != -1: continue bi[i] = 0 stack = [i] while stack: pos = stack.pop() for npos, y in edges[pos]: if y >> k & 1: x = 1 else: x = 0 if bi[npos] == -1: bi[npos] = bi[pos] ^ x X[npos] ^= bi[npos] << k stack.append(npos) elif bi[npos] != bi[pos] ^ x: print(-1) exit() print(*X)