#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

constexpr ll mod=1e9+7;

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n,m; cin >> n >> m;
	vector<vector<pair<int,int>>> v(n);
	for(int i=0;i<m;i++){
		int a,b,y; cin >> a >> b >> y;
		a--; b--;
		v[a].push_back({b,y});
		v[b].push_back({a,y});
	}
	vector<int> res(n,-1);
	queue<int> q;
	for(int i=0;i<n;i++){
		if(res[i]==-1){
			res[i]=0;
			q.push(i);
			while(q.size()){
				int p=q.front();
				q.pop();
				for(auto t:v[p]){
					if(res[t.first]==-1){
						res[t.first]=res[p]^t.second;
						q.push(t.first);
					}
					else{
						if(res[t.first]!=(res[p]^t.second)){
							printf("-1\n"); return 0;
						}
					}
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		printf("%d\n",res[i]);
	}
}