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

#define rep(i, a, b) for(int i=a; i<=b; i++)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int) x.size()
#define pb push_back
#define f first
#define s second
#define nl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int MOD = 1e9+7;
// const int MOD = 998244353;
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;

int n, m;
vector<pii> adj[100001];
int val[100001];
bool vis[100001];

void dfs(int x, int v){
  val[x]=v;
  vis[x]=true;
  trav(k, adj[x]){
    if(vis[k.f]) continue;
    dfs(k.f, (k.s==v?(v==n?1:v+1): k.s));
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.txt", "r", stdin); 
  // freopen("output.txt", "w", stdout);

  cin >> n >> m;
  rep(i, 1, m){
    int u, v, c; cin >> u >> v >> c;
    adj[u].pb({v, c});
    adj[v].pb({u, c});
  }

  rep(i, 1, n){
    if(vis[i]) continue;
    dfs(i, 1);
  }

  rep(i, 1, n) cout << val[i] <<nl;
}
#include<bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 110000

vector<vector<int>>g(MAXN);
map<pi, int> costs;
vector<int>r(MAXN);

void dfs(int now=0, int p=-1){
//cout<<now<<' '<<p<<endl;
  if(now==0)r[now]=0;
  for(auto to: g[now]){
    if(to==p || r[to]>=0)continue;

    int c = costs[{now, to}];
    if(r[now]==c)c++;
    r[to] = c;

    dfs(to, now);
  }
}

int main(){
  int n, m;cin>>n>>m;
  g = vector<vector<int>>(n);
  rep(i, m){
    int u,v,c;cin>>u>>v>>c;u--,v--,c--;
    g[u].push_back(v);
    g[v].push_back(u);
    costs[{u, v}] = c;
    costs[{v, u}] = c;
  }

  rep(i, n)r[i]=-1;

  dfs();

  rep(i, n)cout<<r[i]%n+1<<endl;

  return 0;
}