#include <bits/stdc++.h>
using namespace std;
const auto maxn = int(1e5 + 5);
struct edge {
    int to, c, next;
    edge(int v, int tag, int nxt)
        : to(v), c(tag), next(nxt) {}
};
auto ans = vector<int>(maxn, -1);
auto visited = vector<bool>(maxn, false);
auto es = vector<edge>();
auto head = vector<int>(maxn, -1);
auto color = 1;
auto n = 0, m = 0;

void dfs(int p, int e, int u) {
    if (ans[p] != e) {
        ans[u] = e;
    } else {
        while (color == e) {
            color++;
            if (color > n) color = 1;
        }
        ans[u] = color++;
    }
    for (auto v = head[u]; v != -1; v = es[v].next) {
        if (!visited[es[v].to]) {
            visited[es[v].to] = true;
            dfs(u, es[v].c, es[v].to);
        }
    }
}

auto main() -> int {
    cin >> n >> m;
    auto u = 0, v = 0, c = 0, cnt = 0;
    for (auto i = 0; i < m; ++i) {
        cin >> u >> v >> c;
        es.emplace_back(v, c, head[u]);
        head[u] = cnt++;
        es.emplace_back(u, c, head[v]);
        head[v] = cnt++;
    }
    visited[1] = true;
    dfs(0, -1, 1);

    for (auto i = 1; i <= n; ++i)
        cout << ans[i] << endl;

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

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using LD = long double;

template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; }
void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; }
void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; }

struct Edge{
    int to, color, id;    
};

int n, m;
vi ans;
vector<vector<Edge>> G;

void dfs(int v = 0, int p = -1){
    for(auto [u, co, id] : G[v]){
        if(ans[u] != -1) continue;
        if(ans[v] == co){
            ans[u] = co % n + 1;
        }else{
            ans[u] = co;
        }
        dfs(u, v);
    }    
}

void Main(){
    cin >> n >> m;
    G.resize(n);
    rep(i, m){
        int u, v, c; 
        cin >> u >> v >> c;
        u--; v--;
        G[v].pb({u, c, i});
        G[u].pb({v, c, i});
    }
    ans.resize(n, -1);
    ans[0] = 1;
    dfs();
    rep(i, n) cout << ans[i] << endl;
    return;
}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}