#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define read(v) for(auto &it:v) cin>>it;
#define deb(x) cout << ">" << #x << '=' << x << endl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define inf LLONG_MAX
const ll N = 2e5 + 5;
ll MOD = (ll)(1e9 + 7);

vector<pair<int,int> > adj[N];
vector<int> vis(N);
vector<int> value(N);
int n,k;
void dfs(int node,int val){
	value[node]=((val>n)?1:val);
	vis[node]=true;
	for(auto it:adj[node]){
         if(!vis[it.F]){
         	   if(it.S==value[node])
         	   	dfs(it.F,value[node]+1);
         	   else 
         	   	dfs(it.F,it.S);
         }
	}
}
void solve() {
   cin>>n>>k;
   for(int i=0;i<=n;i++){
   	  adj[i].clear();
   	  vis[i]=false;
   }
   while(k--){
   	  int u,v,z;
   	  cin>>u>>v>>z;
   	  adj[u].pb({v,z});
   	  adj[v].pb({u,z});
   }

   dfs(1,1);
   for(int i=1;i<=n;i++){
   	  cout<<value[i]<<endl;
   }



} 

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  //cin >> t;
  while (t--) solve();
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
  return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

class UnionFind {
public:
    vector<int> par;
    int N;
    UnionFind(int n) {
        par.resize(n, -1);
        N = n;
    }
    int Find(int n) {
        return par[n] < 0 ? n : par[n] = Find(par[n]);
    }
    bool Union(int x, int y) {
        x = Find(x);
        y = Find(y);
        if (x == y) return false;
        if (-par[x] > -par[y]) swap(x, y);
        par[y] += par[x];
        par[x] = y;
        N--;
        return true;
    }
    bool Same(int x, int y) {
        return Find(x) == Find(y);
    }
    int size(int x) {
        return -par[Find(x)];
    }
    int size() {
        return N;
    }
};

using edge = tuple<int, int>;

#include <set>

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m;
    cin, n, m;
    UnionFind uf(n);
    vector<vector<edge>> tree(n);
    rep(i, 0, m) {
        int a, b, c;
        cin, a, b, c;
        a--;
        b--;
        if (!uf.Same(a, b)) {
            uf.Union(a, b);
            tree[a].emplace_back(b, c);
            tree[b].emplace_back(a, c);
        }
    }
    auto get_color = [&](int cur) {
        if (cur == 1) {
            return 2;
        } else {
            return 1;
        }
    };
    vector<int> vertex(n, -1);
    auto rec = [&](auto &f, int cur, int prev) -> void {
        for (auto [nxt, color] : tree[cur]) {
            if (nxt == prev) continue;
            if (vertex[cur] == color) {
                vertex[nxt] = get_color(color);
            } else {
                vertex[nxt] = color;
            }
            f(f, nxt, cur);
        }
    };
    rec(rec, 0, -1);
    set<int> s;
    rep(i, 0, n) {
        s.insert(i + 1);
    }
    for (auto [nxt, color] : tree[0]) {
        s.erase(color);
    }
    vertex[0] = *s.begin();
    rep(i, 0, n) {
        print(vertex[i]);
    }
    return 0;
}
