#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

const int N = 1e5 + 10;

int n, m;
int tot, Head[N], ver[N << 1], Next[N << 1], Leng[N << 1];
int fa[N], ans[N];

void add(int x, int y, int z) {
	tot++;
	ver[tot] = y;
	Leng[tot] = z;
	Next[tot] = Head[x];
	Head[x] = tot;
}

int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); }

void merge(int x, int y) { fa[findfa(x)] = findfa(y); }

void dfs(int x, int f) {
	for (int i = Head[x]; i; i = Next[i]) {
		int y = ver[i];
		if (y == f) continue;
		if (Leng[i] == ans[x]) {
			if (ans[x] == 1) ans[y] = 2;
			else ans[y] = 1;
		}
		else ans[y] = Leng[i];
		dfs(y, x);
	}
}
		

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) fa[i] = i;
	for (int i = 1; i <= m; i++) {
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		if(findfa(x) != findfa(y)) {
			merge(x, y);
			add(x, y, z);
			add(y, x, z);
		}
	}
	ans[1] = 1;
	dfs(1, 0);
	for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono; 
const int mod=1e9+7;
const int mex=300005;
#define ll long long 
#define test int t;cin>>t;while(t--)
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fo(i,a,n) for(int i=a;i<n;i++)
#define rfo(i,a,b) for(int i=a;i>=b;i--)
#define bg begin()
#define en end()
#define fi first
#define se second
#define ub upper_bound
#define lb lower_bound
#define pb push_back
#define veci vector<int>
#define veclli vector<long long int> 
#define all(x) x.begin(),x.end()
#define sci(x) scanf("%d",&x);
#define scc(x) scanf("%c",&x);
#define scs(x) scanf("%s",x);
#define debug(arr,n) for(int i=0;i<n;i++) printf("%d ",arr[i]);
#define sz(x) x.size()
#define loop(x) for(auto it=x.begin();it!= x.end();it++) 
vector<pair<int,int> > v[mex];
int vis[mex],val[mex];
int N;
void dfs(int n)
{
    vis[n]=1;
    if(val[n]==0) val[n]=1;
    fo(i,0,sz(v[n]))
    {
        if(vis[v[n][i].fi]==0&&val[v[n][i].fi]==0)
        {
            if(val[n]!=v[n][i].se) val[v[n][i].fi]=v[n][i].se;
            else val[v[n][i].fi]=v[n][i].se%N+1;
            dfs(v[n][i].fi);
        }
    }
}
int main()
{
    int n,m;
    cin>>n>>m;
    N=n;
    fo(i,0,m)
    {
        int a,b,c;
        cin>>a>>b>>c;
        v[a].pb({b,c});
        v[b].pb({a,c});
    }
    dfs(1);
    fo(i,1,n+1)
    cout<<val[i]<<endl;
    
   
}