#include using namespace std; using P = pair; int n, m; vector> g; vector solve(); int main() { cin >> n >> m; g.resize(n); for (int i = 0; i < m; ++i) { int a, b, x; cin >> a >> b >> x; g[--a].emplace_back(--b, x); g[b].emplace_back(a, x); } auto res = solve(); if (res.size()) for (auto p : res) cout << p << endl; else cout << -1 << endl; return 0; } vector solve() { vector res(n, -1); queue qu; for (int i = 0; i < n; ++i) if (res[i] < 0) { res[i] = 0; qu.push(i); while (qu.size()) { int now = qu.front(); qu.pop(); for (auto [to, x] : g[now]) if (res[to] < 0) { res[to] = x ^ res[now]; qu.push(to); } else if (res[to] != (x ^ res[now])) return vector(); } } return res; }