#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; namespace algebra { template < class T > class XOR { public: using set = T; static constexpr T op(const T &l, const T &r) { return l ^ r; } static constexpr T id = T(0); static constexpr T inv(const T &x) { return x; } static constexpr T pow(const T &x, const int n) { return n & 1 ? x : 0; } static constexpr bool comm = true; }; } template < class abel > class pot_uf { public: using T = typename abel::set; vector data; vector< T > pote; pot_uf(int n) : data(n, -1), pote(n, abel::id) {} int root(int x) { if(data[x] < 0) return x; int r = root(data[x]); pote[x] = abel::op(pote[x], pote[data[x]]); return data[x] = r; } T pot(int x) { root(x); return pote[x]; } bool same(int x, int y) { return root(x) == root(y); } T diff(int x, int y) { return abel::op(pot(x), abel::inv(pot(y))); } bool unite(int x, int y, T p) { p = abel::op(p, diff(y, x)); x = root(x), y = root(y); if(x == y) return p == abel::inv(p); if(data[x] < data[y]) swap(x, y), p = abel::inv(p); data[y] += data[x]; data[x] = y; pote[x] = p; return true; } int size(int x) { return -data[root(x)]; } }; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N,M; cin >> N >> M; pot_uf< algebra::XOR > uf(N); rep(_,M) { int a,b,y; cin >> a >> b >> y; a--; b--; if(uf.same(a, b)) { if(uf.diff(a, b) != y) { cout << -1 << endl; return 0; } } else { uf.unite(a, b, y); } } rep(i,N) cout << uf.pot(i) << endl; }