#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 7;
const int mod = 998244353;
int head[maxn],num,fa[maxn],n,m,ans[maxn];
struct node{
    int  v,w,next;
}e[maxn];
void add(int u,int v,int w){
    e[num].next = head[u];
    e[num].v = v;
    e[num].w = w;
    head[u] = num ++;
}
int Find(int x){
    if(fa[x] != x) fa[x] = Find(fa[x]);
    return fa[x];
}
void dfs(int u,int pre){
    for(int i = head[u];i != -1;i = e[i].next){
        int to = e[i].v,w = e[i].w;
        if(to == pre) continue;
        if(w == ans[u]){
           if(w == n) ans[to] = 1;
           else ans[to] = w + 1;
        }
        else ans[to] = w;
        dfs(to,u);
    }
}
int main (){
    scanf("%d%d",&n,&m);
    memset(head,-1,sizeof(head));
    for(int i = 1; i <= n; i ++){
        fa[i] = i;
    }
    for(int i = 1,u,v,w; i <= m; i ++){
        scanf("%d%d%d",&u,&v,&w);
        int x = Find(u);
        int y = Find(v);
       // printf ("%d %d..\n",x,y);
        if(x == y) continue;
        fa[x] = y;
       // printf ("%d %d %d\n",u,v,w);
        add(u,v,w);
        add(v,u,w);
    }
    ans[1] = 1;
    dfs(1,-1);
    for(int i = 1; i <= n; i ++){
        printf ("%d\n",ans[i]);
    }












}

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define forin(in ,n) for(ll i=0; i<n; i++) cin>>in[i]
#define forout(out) for(ll i=0; i<(ll)out.size(); i++) cout<<out[i]<<endl
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, n) for (ll i = a; i < n; ++i)
#define rep_down(i, a, n) for (ll i = a; i >= n; --i)
#define P pair<ll, ll>

#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>

constexpr ll INF = (1ll << 60);
constexpr ll mod = 1000000007;
//constexpr ll mod = 998244353;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
void pt(T val) {
    cout << val << "\n";
}
template <typename T>
void pt_vll(vector<T> &v) {
    ll vs = v.size();
    rep(i, vs) {
        cout << v[i];

        if (i == vs - 1)
            cout << "\n";
        else
            cout << " ";
    }
}
ll mypow(ll a, ll n) {
    ll ret = 1;
    if(n==0) return 1;
    if(a==0) return 0;
    rep(i, n) {
        if (ret > (ll)(1e18 + 10) / a) return -1;
        ret *= a;
    }
    return ret;
}
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

struct Edge{
    ll to;
    ll co;
};

int main() {
    ll n,m,x=0,y=0,z=0,k,cnt=1,ans=0;
    cin>>n>>m;
    vector<vector<Edge>> G(n);
    vll color(n);
    rep(i,m){
        cin>>x>>y>>z;
        x--;y--;
        G[x].push_back({y,z});
        G[y].push_back({x,z});
    }
    queue<ll> que;
    que.emplace(0ll);
    color[0]=1;
    while(!que.empty()){
        ll state=que.front();
        que.pop();
        for(auto e:G[state]){
            if(color[e.to]>0) continue;
            if(color[state]==e.co){
                color[e.to]=(e.co)%n+1;
            }else{
                color[e.to]=e.co;
            }
            que.emplace(e.to);
        }
    }
    rep(i,n){
        cout<<color[i]<<endl;
    }
}