#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#ifndef LOCAL
#define cerr if(0)cerr
#endif
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;

const int N = 2e5+1;
int n, m, num[N];
vector<pii> g[N];
bool vis[N];
void dfs(int x, int p, int val) {
	vis[x] = 1;
	if(!p || num[p] == val) {
		num[x] = val%n+1;	
	}
	else {
		num[x] = val;
	}
	for(auto i: g[x]) {
		if(!vis[i.st]) {
			dfs(i.st, x, i.nd);	
		}
	}
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=0; i<m; ++i) {
		int a, b, c; cin>>a>>b>>c;
		g[a].push_back(mp(b, c));
		g[b].push_back(mp(a, c));
	}
	dfs(1, 0, 0);
	for(int i=1; i<=n; ++i) {
		cout<<num[i]<<'\n';
	}
}


#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

struct Edge {
    int t, c;
    Edge() = default;
    Edge(int t, int c) : t(t), c(c) {}
};

int main() {
    int N, M; cin >> N >> M;
    vector<vector<Edge>> E(N);
    REP(i, 0, M) {
        int u, v, c; cin >> u >> v >> c; u--; v--; c--;
        E[u].push_back({v, c});
        E[v].push_back({u, c});
    }

    vector<int> ans(N, -1);
    queue<pair<int, int>> Q;
    Q.push({0, -1});
    while (!Q.empty()) {
        auto q = Q.front(); Q.pop();
        int v = q.first;
        int p = q.second;
        if (ans[v] < 0) ans[v] = -1;
        set<int> labels;
        for (auto e : E[v]) {
            if (ans[e.t] != -1) continue;
            if (ans[v] == -1 || ans[v] != e.c) {
                ans[e.t] = e.c;
                labels.insert(e.c);
            } else {
                ans[e.t]--;
            }
            Q.push({e.t, e.c});
        }

        if (ans[v] < 0) {
            int c = 0;
            labels.insert(p);
            while (labels.count(c)) c++;
            ans[v] = c;
        }
    }
    for (auto a : ans) cout << a + 1 << endl;
    return 0;
}