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

#define ll long long
const int N = 2.1e5;

int n, m, tkn[N], ans[N];
vector<pair<int, int>> e[N];
queue<int> q;

void fn(int v) {
    tkn[v] = 1;
    for (auto [u, c] : e[v]) {
        if (!tkn[u]) q.push(u);
    }
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        u--;
        v--;
        c--;
        e[u].push_back({v, c});
        e[v].push_back({u, c});
    }
    q.push(0);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        if (tkn[v]) continue;
        if (v == 0) ans[v] = 0; else {
            for (auto [u, c] : e[v]) {
                if (tkn[u]) {
                    ans[v] = c == ans[u] ? (c + 1) % n : c;
                    break;
                }
            }
        }
        fn(v);
    }
    for (int i = 0; i < n; i++) cout << ans[i] + 1 << '\n';
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
const ll mod = 1000000007;
const ll inf = 1000000000000;//10の12乗
#define yn {puts("Yes");}else{puts("No");}
#define dame { puts("-1"); return 0;}
int n,m;
vector<vector<P>> v(200005);
vector<bool> seen(200005);
vector<long long> ans(200005,0);
void dfs(int k){
    if(!seen[k]){
        seen[k]=true;
        for(auto next:v[k]){
            if(seen[next.F]) continue;
            if(ans[k]!=next.S&&ans[next.F]==0){
                ans[next.F]=next.S;
                
            }
            dfs(next.F);
        }
    }
    
}


int main() {
    cin >> n >> m;
    VVl ng(n);
    map<P,int> mp;
    rep(i,m){
        int x,y,z;
        cin >> x >> y >> z;
        x--;y--;
        if(mp[MP(x,y)]) continue;
        mp[MP(x,y)]++;
        v[x].push_back(MP(y,z));
        v[y].push_back(MP(x,z));
        ng[x].PB(z);
        ng[y].PB(z);
    }
    int now=0;
            rrep(j,1,n+1){
                if(ng[0][now]==j)now++;
                else{
                    ans[0]=j;
                    break;
                }
                if(j==n) ans[0]=n;
            }
    dfs(0);
    rep(i,n){
        sort(all(ng[i]));
        ng[i].PB(-1);
    }
    rep(i,n){
        if(ans[i]==0){
            now=0;
            rrep(j,1,n+1){
                if(ng[i][now]==j)now++;
                else{
                    ans[i]=j;
                    break;
                }
                if(j==n) ans[i]=n;
            }
        }
    }

    rep(i,n) cout << ans[i] << endl;
    
}