//#define local
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define rep(i,n); for(long long i = 0;i < (n);i++)
using ll = long long;
using P = pair<long long,long long>;
template <class T> using vec = vector<T>;
#ifdef local
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    //cout << fixed << setprecision(10);
    ll n,m; cin >> n >> m;
    vec<vec<P>> tree(n);
    vec<set<ll>> s(n);
    rep(i,m){
        ll u,v,c; cin >> u >> v >> c; --u; --v;
        tree[u].emplace_back(make_pair(v,c));  s[u].insert(v);
        tree[v].emplace_back(make_pair(u,c));  s[v].insert(u);
    }
    vec<ll> c(n,-1);
    queue<ll> que; que.push(0);
    ll st = 1;
    while(c[0] == -1){
        if(!s[0].count(st))c[0] = st;
        st++;
    } 
   while(!que.empty()){
        ll now = que.front();
        que.pop();
        for(auto p : tree[now]){
            if(c[p.first] != -1)continue;
            if(c[now] != p.second)c[p.first]=p.second;
            else c[p.first] = (p.second+1)%n;
            que.push(p.first);
        }
    } 
    rep(i,n)cout << c[i] << endl;
}
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define INF 0x3f3f3f3f
#define f first
#define s second
#define pii pair<int, int>
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define mem(x, a) memset(x, a, sizeof(x))
#define loli ios_base::sync_with_stdio(0), cin.tie(0)
using namespace std;
const int maxn = 1e6 + 50;
int n, m, color[maxn];
vector<pii> vc[maxn];
bitset<maxn> vis, ok;

void dfs(int x, int val){
	color[x] = val;
	for(auto i : vc[x]){
		if(color[i.f]) continue;
		if(i.s == color[x]){
			if(i.s == 1) dfs(i.f, 2);
			else dfs(i.f, 1);
		}
		else dfs(i.f, i.s);
	}
}

signed main(){
	loli;
	//freopen("/home/iceylemon157/Desktop/test.in", "r", stdin);
	//freopen("/home/iceylemon157/Desktop/test.out", "w", stdout);
	cin >> n >> m;
	for(int i = 1; i <= m; i ++){
		int a, b, v;
		cin >> a >> b >> v;
		vc[a].pb({b, v});
		vc[b].pb({a, v});
	}
	dfs(1, 1);
	for(int i = 1; i <= n; i ++){
		cout << color[i] << "\n";
	}
}