#include<bits/stdc++.h>
using namespace std;
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;

#define int ll
#define LOCAL 0
#define dbg(x) cout << #x << " is " << x << "\n"
#define gll(x) scanf("%d",&x)
#define gll2(x,y) scanf("%d%d",&x,&y)
#define gll3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define gllarr(arr,n) f(i,n) gll(arr[i]);
#define sz(x) ((int)x.size())
#define s(x) sort(x.begin(),x.end())
#define all(v) v.begin(),v.end()
#define rs(v) { s(v) ; r(v) ; }
#define r(v) {reverse(all(v));}
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define fr(i,n) for(int i=n-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)

const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e16;
const ld eps = 1e-12;
const ll N = (int)1e5 + 5;
const ll LOGN = 19;
const ld PI = 3.14159265358979323846;
inline ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
inline ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
inline ll power(ll a, ll b, ll m = mod) { if(b == 0) return 1; if(b == 1) return (a % m); ll x = power(a, b / 2, m); x = mul(x, x, m); if(b % 2) x = mul(x, a, m); return x;}

vector< pair<int, pair<int,int> > > edges;
map<pair<int, int>, int> mp;
int par[N], sz[N];
int n, m;
vector<pair<int, int> > adj[N];
int ans[N];

struct DSU{
    void init()
    {
        f(i, N) {
            par[i] = i;
            sz[i] = 1;
        }
    }

    int find(int x){
        if(x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    void merge(int x, int y) {
        int u = find(x);
        int v = find(y);
        if (sz[u] <= sz[v])
        {
            par[u] = par[v];
            sz[v] += sz[u];
        }
        else
        {
            par[v] = par[u];
            sz[u] += sz[v];
        }
    }
};

ll kruskal()
{
    ll ret = 0ll;
    DSU dsu;
    dsu.init();
    for(auto it : edges)
    {
        int w = it.first, u = it.second.first, v = it.second.second;
        int par_u = dsu.find(u), par_v = dsu.find(v);
        if(par_u == par_v) continue;
        adj[u].pb({v, mp[{u, v}]});
        adj[v].pb({u, mp[{u, v}]});
        dsu.merge(u,v);
    }
    return ret;
}

void dfs(int src = 0, int par = -1) {
	for(auto it: adj[src]) {
		int v = it.first, c = it.second;
		if (v == par) continue;
		if (c == ans[src]) ans[v] = (c + 1) % n;
		else ans[v] = c;
		dfs(v, src);
	}
}

void solve() {
	cin>>n>>m;
	f(i, m) {
		int u, v, c;
		cin>>u>>v>>c;
		u--, v--, c--;
		if(mp.find({u, v}) != mp.end()) continue;
		edges.push_back({i, {u, v}});
		mp[{u, v}] = c;
		mp[{v, u}] = c;
	}
	kruskal();
	ans[0] = 0;
	dfs();
	f(i, n) cout<<ans[i] + 1<<"\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (LOCAL) {
        freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\input.txt", "r", stdin);
        freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\output.txt", "w", stdout);
    }
    int t = 1;
    //cin>>t;
    for(int test = 1; test <= t; test++) {
        //cout<<"Case #"<<test<<": ";
        solve();
    }
    return 0;
}
#include<cstdio>
#include<algorithm>
#include<vector>
#define For(i,A,B) for(i=(A);i<=(B);++i)
#define pb push_back
using namespace std;
const int N=100050;
int G[N],to[N*2],w[N*2],nxt[N*2],sz,f[N],a[N];
inline void adde(int u,int v,int c){
	to[++sz]=v;w[sz]=c;nxt[sz]=G[u];G[u]=sz;
	to[++sz]=u;w[sz]=c;nxt[sz]=G[v];G[v]=sz;
}
int gf(int x){return x==f[x]?x:f[x]=gf(f[x]);}
void dfs(int u,int fa,int c){
	int i,v;
	vector<int> t;
	if(c>0)a[u]=c;
	else if(c<0)t.pb(-c);
	for(i=G[u];i;i=nxt[i])if((v=to[i])!=fa){
		dfs(v,u,w[i]==c?-c:w[i]);
		t.pb(w[i]);
	}
	if(c<=0){
		sort(t.begin(),t.end());
		t.erase(unique(t.begin(),t.end()),t.end());
		For(i,0,t.size()-1)if(t[i]!=i+1){
			a[u]=i+1;
			break;
		}
		if(!a[u])a[u]=t.size()+1;
	}
}
int main(){
	int n,m,i,u,v,c,x,y;
	scanf("%d%d",&n,&m);
	For(i,1,n)f[i]=i;
	while(m--){
		scanf("%d%d%d",&u,&v,&c);
		if((x=gf(u))!=(y=gf(v))){
			adde(u,v,c);
			f[x]=y;
		}
	}
	dfs(1,0,0);
	For(i,1,n)printf("%d\n",a[i]);
	return 0;
}