#include <bits/stdc++.h>
#define mp make_pair
#define sc second
#define fr first
#define pb push_back
#define pii pair<int, int>
#define Nmax 100005
using namespace std;
vector <pii> v[Nmax];
int t[Nmax], s[Nmax], viz[Nmax], n, m;

int tata(int x)
{
    if(x!=t[x]) t[x]=tata(t[x]);
    return t[x];
}
void uneste(int x, int y)
{
    if(s[x]>s[y])
    {
        t[y]=x;
        s[x]+=s[y];
    }
    else
    {
        t[x]=y;
        s[y]+=s[x];
    }
}
void dfs(int x)
{
    for(auto it:v[x])
        if(viz[it.fr]==0)
        {
            if(viz[x]==it.sc)
                viz[it.fr]=(viz[x]+1)%n+1;
            else viz[it.fr]=it.sc;
            dfs(it.fr);
        }
}
int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;

    for(int i=1;i<=n;i++)
    {
        s[i]=1; t[i]=i;
    }

    for(int i=1;i<=m;i++)
    {
        int x, y, k;
        cin >> x >> y >> k;
        if(tata(x)!=tata(y))
        {
            uneste(t[x], t[y]);
            v[x].push_back(mp(y, k));
            v[y].push_back(mp(x, k));
        }
    }

    viz[1]=1;
    dfs(1);

    for(int i=1;i<=n;i++)
        cout << viz[i] << '\n';
    return 0;
}

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

using ll = long long;
#define REP(i, n) for(int (i)=0; (i)< (n); ++i)
#define REPR(i, n) for(int (i)=(n); (i)>=0; --i)
#define FOR(i, n, m) for(int (i)=(n); (i)<(m); ++i)
constexpr int INF = 1e9;
//constexpr ll INF = 1LL << 61;
constexpr int mod = 1e9+7;

struct Edge{
  int x, y, d;
  Edge(){}
  Edge(int x, int y, int d): x(x), y(y), d(d){}
  bool operator< (const Edge l)const{
    return d < l.d;
  }
};

struct UnionFind{
  vector<int> node;
  UnionFind(int n){
    node = vector<int>(n, -1);
  }
  int find(int v){
    if(node[v] < 0){
      return v;
    }
    return node[v] = find(node[v]);
  }
  bool merge(int v, int u){
    int rootv = find(v);
    int rootu = find(u);
    if(rootv == rootu) return false;
    if(node[rootv] < node[rootu]){
      swap(rootv, rootu);
    }
    node[rootv] += node[rootu];
    node[rootu] = rootv;
    return true;
  }
  int size(int v){
    return -node[v];
  }
};
vector<int> ans;
void dfs(int v, vector<vector<pair<int, int>>> &g, int root){
  for(auto edge : g[v]){
    int nv = edge.first;
    int cost = edge.second;
    if(nv == root) continue;
    assert(nv != v);
    if(ans[v] == cost){
      if(cost ==0){
        ans[nv]= 1;
      }
      else{
        ans[nv] = 0;
      }
    }
    else{
      ans[nv] = cost;
    }
    dfs(nv, g, v);
  }
}
int main(){
  int N, M;
  cin >> N >> M;
  vector<Edge> edges(M);
  REP(i, M){
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--, c--;
    edges[i] = Edge(u, v, c);
  }
  sort(edges.begin(), edges.end());
  vector<vector<pair<int, int>>> g(N);
  UnionFind uf(N);
  REP(i, M){
    int u, v;
    u = edges[i].x, v = edges[i].y;
    if(uf.merge(u, v)){
      g[v].push_back({u, edges[i].d});
      g[u].push_back({v, edges[i].d});
    }
  }
  ans.resize(N);
  ans[0] = 0;
  dfs(0, g, -1);
  REP(i, N){
    cout << ans[i]+1 << endl;
  }
  return 0;
}
