#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) #define sz(x) ((ll)(x).size()) #define ceil(x, y) (((x)+(y)-1) / (y)) #define all(x) (x).begin(),(x).end() #define outl(...) dump_func(__VA_ARGS__) #define inf 1e18 #define mod 1000000007 using namespace std; typedef long long llint; typedef long long ll; typedef pair P; bool exceed(ll x, ll y, ll m){return x >= m / y + 1;} template ostream& operator << (ostream& os, vector& vec) { for(int i = 0; i ostream& operator << (ostream& os, pair& pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template ostream& operator << (ostream& os, map& map_var) { for(typename map::iterator itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if(itr != map_var.end()) os << ","; itr--; } return os; } template ostream& operator << (ostream& os, set& set_var) { for(typename set::iterator itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if(itr != set_var.end()) os << " "; itr--; } return os; } void dump_func() {cout << endl;} template void dump_func(Head &&head, Tail &&... tail) { cout << head; if(sizeof...(Tail) > 0) cout << " "; dump_func(std::move(tail)...); } struct WeightedUnionFind{ int size; vector parent; vector weight; WeightedUnionFind(){} WeightedUnionFind(int size){ this->size = size; parent.resize(size+1); weight.resize(size+1); init(); } void init(){ for(int i = 0; i <= size; i++) parent[i] = i, weight[i] = 0; } int root(int i){ if(parent[i] == i) return i; int r = root(parent[i]); weight[i] = weight[parent[i]] ^ weight[i]; return parent[i] = r; } bool same(int i, int j){ return root(i) == root(j); } llint get(int i, int j){ //i->jの重みを返す(非連結なら0を返す) if(!same(i, j)) return 0; return weight[j] ^ weight[i]; } bool unite(int i, int j, llint x){ //i->jの重みをxに設定する。矛盾があればfalseを返す。 int root_i = root(i), root_j = root(j); if(root_i == root_j){ return get(i, j) == x; } parent[root_i] = root_j; weight[root_i] = weight[j]^weight[i]^x; return true; } }; ll n, m; WeightedUnionFind uf(100005); int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; ll u, v, w; bool flag = true; rep(i, 1, m){ cin >> u >> v >> w; flag &= uf.unite(u, v, w); } if(!flag){ outl(-1); return 0; } rep(i, 1, n) outl(uf.get(uf.root(i), i)); return 0; }