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

const long long MOD=1e9+7;
const int N=1e5+5;

bool vis[N];
int u,v,c,n,m,r[N];
vector<pair<int,int>> adj[N];

void dfs(int x, int y){
	vis[x]=true;
	for(auto p:adj[x]){
		if(vis[p.first]) continue;
		if(p.second!=y) r[p.first]=p.second;
		else r[p.first]=p.second%n+1;
		dfs(p.first,r[p.first]);
	}
}

int main(){
	/*scanf("%d", &t);
	while(t--){
		scanf("%d", &n);
		for(int i=0;i<n;i++){
			scanf("%d", &a[i]);
		}
	}
	*/
	scanf("%d %d", &n, &m);
	for(int i=0;i<m;i++){
		scanf("%d %d %d", &u, &v, &c);
		adj[u].push_back({v,c});
		adj[v].push_back({u,c});
	}
	r[1]=1;
	dfs(1,1);
	for(int i=1;i<=n;i++) printf("%d\n", r[i]);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr lint B1 = 1532834020;
constexpr lint M1 = 2147482409;
constexpr lint B2 = 1388622299;
constexpr lint M2 = 2147478017;
constexpr int INF = 2147483647;
void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}
template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; }
template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; }
struct Node;
vector<Node> V;
int N;
struct Node {
  int label = -1;
  vector<int> nbr;
  vector<int> edge;
  bool vst = false;
  void dfs() {
    vst = true;
    REP(i, nbr.size()) {
      if(!V[nbr[i]].vst) {
        if(label == edge[i]) V[nbr[i]].label = (edge[i] + 1) % N;
        else V[nbr[i]].label = edge[i];
        V[nbr[i]].dfs();
      }
    }
  };
};
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int M;
  cin >> N >> M;
  V = vector<Node>(N);
  REP(i, M) {
    int u, v, c;
    cin >> u >> v >> c;
    u--; v--; c--;
    V[u].nbr.push_back(v);
    V[v].nbr.push_back(u);
    V[u].edge.push_back(c);
    V[v].edge.push_back(c);
  }
  V[0].label = 0;
  V[0].dfs();
  REP(i, N) cout << (V[i].label + 1) << "\n";
}