#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; res[0] = 0; qu.push(0); 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(); } for (auto &p : res) if (p < 0) p = 0; return res; }