#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = (n-1); i >= 0; i--)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)

template <class S>S sum(vector<S>& a) {return accumulate(all(a), S());}
template <class S>S max(vector<S>& a) {return *max_element(all(a));}
template <class S>S min(vector<S>& a) {return *min_element(all(a));}
ll max(int a, ll b) { return max((ll)a, b); } ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); } ll min(ll a, int b) { return min(a, (ll)b); }

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

using P = pair<int,int>;
int n,m;
vector<vector<P>> graph;
vector<int> label;

void dfs(int node){
  for(auto [a,c]: graph[node]){
    if(label[a] < 0){
      if(c == label[node]){
        label[a] = (c+1) % n;
      }else{
        label[a] = c;
      }
      dfs(a);
    }
  }
}


void Main(){
  cin >> n >> m;
  graph = vector<vector<P>>(n);
  label = vector<int>(n, -1);
  Rep(m){
    int u,v,c; cin >> u >> v >> c;
    u--; v--; c--;
    graph[u].push_back(P(v,c));
    graph[v].push_back(P(u,c));
  }
  label[0] = 0;
  dfs(0);
  for(auto i: label){
    cout << i+1 << endl;
  }
}

#include "bits/stdc++.h"
using namespace std;
using P = pair<int, int>;
bool seen[100010], seen2[100010];
vector<vector<P>>graph;
int ans[100010];
void dfs(vector<vector<P>> &G, int v) {
	seen[v] = true;
	for (auto nv : G[v]) {
		if (seen[nv.first])continue;
		graph[v].push_back({ nv.first,nv.second });
		graph[nv.first].push_back({ v,nv.second });
		dfs(G, nv.first);
	}
}
void dfs2(int v) {
	if (v == 0) {
		ans[v] = 1;
	}
	seen2[v] = true;
	for (auto nv : graph[v]) {
		if (seen2[nv.first])continue;
		if (ans[v] == nv.second) {
			int tmp = 1;
			if (tmp == nv.second)tmp++;
			ans[nv.first] = tmp;
		}
		else {
			ans[nv.first] = nv.second;
		}
		dfs2(nv.first);
	}
}
signed main() {
	int N, M; cin >> N >> M;
	graph.resize(N);
	vector<vector<P>>G(N);
	for (int i = 0; i < M; i++) {
		int u, v, c; cin >> u >> v >> c; u--; v--;
		G[u].push_back({ v,c });
		G[v].push_back({ u,c });
	}
	dfs(G, 0);
	dfs2(0);
	for (int i = 0; i < N; i++) {
		cout << ans[i] << endl;
	}
}