N, M = map(int, input().split()) d = {} for _ in range(M): A, B = map(int, input().split()) Y = int(input()) if A not in d and B not in d: d[A] = 0 if A not in d: d[A] = d[B] ^ Y if B not in d: d[B] = d[A] ^ Y if d[A] ^ d[B] != Y: print(-1) exit() for i in range(1, N + 1): if i not in d: print(0) else: print(d[i])