#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;

vector<vector<P>> edge(110000,vector<P>(0));
vector<bool> used(110000,0);
VI ans(110000,-1);
bool no=0;

void dfs(int v, int x){
    for(auto to:edge[v]){
        if(ans[to.first]!=-1&&ans[to.first]!=(x^to.second))
            no=1;
        if(!used[to.first]){
            ans[to.first]=x^to.second;
            used[to.first]=1;
            dfs(to.first,x^to.second);
        }
    }
}

int main(){
    int n, m; cin >> n >> m;
    int a, b, y;
    REP(i,m){
        cin >> a >> b >> y;
        a--, b--;
        edge[a].push_back({b,y});
        edge[b].push_back({a,y});
    }
    REP(i,n){
        if(!used[i]){
            ans[i]=0;
            dfs(i,0);
        }
    }
    if(no)
        cout << -1 << endl;
    else{
        REP(i,n)
            cout << ans[i] << endl;
    }
    return 0;
}