N, M = gets.split.map(&:to_i) E = Hash.new { |h, k| h[k] = [] } C = Hash.new { |h, k| h[k] = Hash.new } M.times do a, b = gets.split.map(&:to_i) y = gets.to_i E[a] << b E[b] << a if C[a][b] && C[a][b] != y puts -1 exit end C[a][b] = C[b][a] = y end ans = Array.new(N + 1, -1) visited = Array.new(N + 1, false) 1.upto(N) do |i| next if visited[i] queue = [] queue << [i, 0] until queue.empty? u, x = queue.shift next if visited[u] visited[u] = true ans[u] = x E[u].each do |v| c = C[u][v] nc = c ^ x if ans[v] != -1 && ans[v] != nc puts -1 exit end queue << [v, nc] end end end puts ans[1..-1]