#include<bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define rrep(i,a,b) for (int i = a - 1; i >= b; i--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ll long long
#define dbwt(x) std::cout << fixed << setprecision(15) << x << '\n' 
#define Wt(x) cout << x << '\n'
#define re0 return 0
#define Int(...) int __VA_ARGS__; in(__VA_ARGS__)
#define Ll(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; in(__VA_ARGS__)
#define Vec(t,a,s) vector<t>a(s); in(a)
#define Vec2(t,a,b,s) vector<t>a(s),b(s);rep(i,0,s)in(a[i],b[i])
#define Vec3(t,a,b,c,s) vector<t>a(s),b(s),c(s);rep(i,0,s)in(a[i],b[i],c[i])
#define Vec4(t,a,b,c,d,s) vector<t>a(s),b(s),c(s),d(s);rep(i,0,s) in(a[i],b[i],c[i],d[i])
#define mvec(t,a,i) vector<t>(a,i)
#define mvec2(t,a,b,i) vector<vector<t>>(a,vector<t>(b,i))
void scan(int& x) {std::cin >> x;}
void scan(ll& x) {std::cin >> x;}
void scan(double& x) {std::cin >> x;}
void scan(std::string& x) {std::cin >> x;}
void scan(std::vector<int>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void scan(std::vector<ll>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void in(){}
template<class Head, class... Tail>
void in(Head& head, Tail&... tail) {scan(head); in(tail...);}
template<class T, class U>
bool chmax(T& a, U b) {if(a < b){a=b; return true;} return false;}
template<class T, class U>
bool chmin(T& a, U b) {if(a > b){a=b; return true;} return false;}
using namespace std;
template<class T>
using vvec = vector<vector<T>>;
template<class T>
void emitvec(vector<T> a) {rep (i, 0, a.size()) cout << a[i] << " "; cout << endl;}
struct IoSetup {
   	IoSetup() {
     	cin.tie(nullptr);
     	ios::sync_with_stdio(false);
     	cout << fixed << setprecision(15);
   	}
} iosetup;

//----------------------------------------------------------------//

int N, M;
map<pair<int, int>, int> mp;
vector<vector<int>> g(100005);

vector<int> col(100005, -1);
vector<bool> visited(100005, false);

void dfs(int v) {
    visited[v] = true;
    for (int to: g[v]) {
        if (visited[to]) continue;
        if (col[v] == mp[{v, to}]) {
            col[to] = (col[v] == 1 ? N : 1);
        }
        else {
            col[to] = mp[{v, to}];
        }
        dfs(to);
    }
}

int main() {
    cin >> N >> M;
    Vec3(int, u, v, c, M);

    rep (i, 0, M) {
        mp[{u[i], v[i]}] = mp[{v[i], u[i]}] = c[i];
        g[u[i]].pb(v[i]);
        g[v[i]].pb(u[i]);
    }

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

    rep (i, 1, N + 1) cout << col[i] << endl; 

}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;

vector<vector<pair<int,int>>> ex(500000);
vector<bool> seen(500000,false);
vector<int> ans(500000,0);

void dfs(int now, int par, int c){
    if(seen[now]) return;
    seen[now] = true;
    if(ans[par] != c) ans[now] = c;
    else if(c == 0) ans[now] = 1;
    for(auto p:ex[now]) dfs(p.first, now, p.second);
}

int main(){
    int n,m;
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int u,v,c;
        cin >> u >> v >> c;
        u--;v--;c--;
        ex[u].push_back({v,c});
        ex[v].push_back({u,c});
    }

    dfs(0,0,0);

    for(int i = 0; i < n; i++) cout << ans[i]+1 << endl;

    return 0;
}