#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<numeric>
//#include<atcoder/scc>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
//#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;

int n,m;
vector<vector<Pi>> G;
vector<int> ans;
vector<bool> visited;

void dfs(int u){
    visited[u] = true;
    for(auto v : G[u]){
        if(visited[v.first]) continue;
        if(ans[u] != v.second) ans[v.first] = v.second;
        else ans[v.first] = n+1-v.second;
        dfs(v.first);
    }
}

int main(){
    cin >> n >> m;
    G.resize(n);
    ans.resize(n);
    visited.resize(n,false);
    rep(i,m){
        int u,v,c; cin >> u >> v >> c;
        u--; v--;
        G[u].emplace_back(v,c);
        G[v].emplace_back(u,c);
    }
    ans[0] = G[0][0].second;
    dfs(0);
    rep(i,n){
        cout << ans[i] << endl;
    }
}



#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> II;

void solve(long long N, long long M, std::vector<int> &u, std::vector<int> &v, std::vector<int> &c) {
	vector<vector<II>> edges(N);
	vector<int> color(N, -1), used(N + 1);
	for (int i = 0; i < M; ++i) {
		edges[u[i]].emplace_back(II(v[i], c[i]));
		edges[v[i]].emplace_back(II(u[i], c[i]));
	}
	vector<int> q;
	q.emplace_back(0);
	while (!q.empty()) {
		vector<int> nq;
		for (auto from : q) {
			for (auto kv : edges[from]) {
				int to = kv.first, col = kv.second;
				if (color[to] >= 0) continue;
				if (color[from] == col) {
					for (col = 1; col <= N; ++col) {
						if (color[from] != col) {
							break;
						}
					}
				}
				color[to] = col;
				used[col] = 1;
				nq.emplace_back(to);
			}
		}
		q = nq;
	}
	for (int i = 1; i <= N; ++i) {
		if (!used[i]) {
			color[0] = i;
			break;
		}
	}
	for (int i = 0; i < N; ++i) {
		cout << color[i] << endl;
	}
}

int main() {
    long long N;
	std::cin >> N;
	long long M;
	std::cin >> M;
	std::vector<int> u(M), v(M), c(M);
	for (int i = 0; i < M; i++) {
		std::cin >> u[i]; u[i]--;
		std::cin >> v[i]; v[i]--;
		std::cin >> c[i]; // c[i]--;
	}
	solve(N, M, u, v, c);
	return 0;
}
