#include<bits/stdc++.h>
#define int long long
#define uc getchar()
#define ph putchar
#define R register
#define CN const
#define rint register int
#define cint const int
#define pe ph('\n')
#define ps ph(' ')
using namespace std;
cint inf=998244353;
cint mix=1e6+5;
inline int in();
inline void out(rint x);
int n=in(),m=in(),ct=1;
int hea[mix],nxt[mix],tor[mix];
int ans[mix],val[mix];
inline void adedge(rint x,rint y){
	nxt[++ct]=hea[x],tor[hea[x]=ct]=y;
	nxt[ct^=1]=hea[y],tor[hea[y]=ct]=x;
	val[ct]=val[ct^1]=in();
}
inline void dfs(rint x){
	for(rint u,i=hea[x];i;i=nxt[i])
	if(!ans[u=tor[i]]){
		if(val[i]!=ans[x])ans[u]=val[i];
		else ans[u]=(val[i]%n)+1;
		dfs(u);
	}
	
}
#undef int
int main() {
#define int long long
	for(rint i=1;i<=m;++i)
	adedge(in(),in());
	dfs(ans[1]=1);
	for(rint i=1;i<=n;++i)
	out(ans[i]),pe;
}
inline int in() {
	rint x=0;
	R char s=uc;
	R bool f=0;
	while((s<'0'||s>'9')&&s!='-')s=uc;
	if(s=='-')f=1,s=uc;
	while(s>='0'&&s<='9')(x*=10)+=(s^48),s=uc;
	return f?-x:x;
}
inline void out(rint x) {
	if(x<0)ph('-'),x=-x;
	rint y=x/10;
	if(y)out(y);
	ph((x-y*10)^48);
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define rep2(i, a)for(auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll mod;
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x *= x; p /= 2; } else { a *= x; p--; } }return a; }
ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x = x * x % mod; p /= 2; } else { a = a * x % mod; p--; } }return a; }
ll co(ll n, ll k) { ll a = 1; rep(i, 1ll, k) { a *= n - i + 1; a /= i; }return a; }
ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; }
ll modinv(ll a) { ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); }u %= mod; if (u < 0) u += mod; return u; }

vector<vector<pair<int, int>>> G;
vector<int> la;
int n;
void sim(int v) {
	for (auto e : G[v]) {
		if (la[e.first] == 0) {
			if (e.second != la[v])la[e.first] = e.second;
			else la[e.first] = la[v] % n + 1;
			sim(e.first);
		}
	}
}
int main() {
	int m, a, b, c;
	cin >> n >> m;
	G.resize(n);
	la.resize(n);
	rep(i, 0, m) {
		cin >> a >> b >> c;
		a--, b--;
		G[a].push_back({ b, c });
		G[b].push_back({ a, c });
	}
	la[0] = 1;
	sim(0);
	for (auto v : la)printf("%d\n", v);
	return 0;
}