#include <bits/stdc++.h>
using namespace std;
#ifdef __DEBUG__
    void debug_out() { cerr << endl; } 
    template <typename Head, typename... Tail>
    void debug_out(Head H, Tail... T) {cerr << " " << H; debug_out(T...);}
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
    #define debug(...) 
#endif

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[]={1, 0, -1, 0, 1,-1, 1,-1};
int dy[]={0, 1, 0, -1, 1, 1,-1,-1};
const int INT_INF = (int)(2e9);
const ll  LL_INF = (ll)(2e18);

inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const deque<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const set<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

int N,M;
vector<vector<pii>> g;
inline void input(){
    fast_io();
    cin >> N >> M;
    g.resize(N);
    for (int i = 0; i < M; i++)
    {
        int u,v,c;
        cin >> u >> v >> c;
        u--,v--;
        g[u].emplace_back(v,c);
        g[v].emplace_back(u,c);
    }
}

vector<vector<pii>> prim(vector<vector<pii>> g, int s){
    vector<vector<pii>> f(N);
    queue<int> q;
    vector<bool> seen(N,false);
    q.push(s);
    seen[s] = true;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for(auto [v,c]:g[u]){
            if(seen[v]) continue;
            seen[v] = true;
            q.push(v);
            f[v].emplace_back(u,c);
            f[u].emplace_back(v,c);
        }
    }
    return f;
}

vector<int> col;
void dfs(vector<vector<pii>> &f, int u, int p){
    for(auto [v,c]:f[u]){
        if(v==p) continue;
        if(c!=col[u]) {
            col[v] = c;
        }
        dfs(f,v,u);
    }
}

inline int solve(){
    auto f = prim(g,0);
    col.assign(N,-1);
    col[0] = 1;
    for(auto [v,c]:f[0]){
        if(col[0]==c) col[0]++;
    }
    vector<set<int>> col_set(N);
    for (int i = 0; i < N; i++)
    {
        for(auto [v,c]:f[i]){
            col_set[i].insert(c);
        }
    }
    
    dfs(f,0,-1);
    for (int i = 0; i < N; i++)
    {
        if(col[i]==-1){
            col[i] = 1;
            while(col_set[i].find(col[i]) != col_set[i].end()){
                col[i]++;
            }
        }
    }
    for (int i = 0; i < N; i++)
    {
        cout << col[i] << "\n";
    }
    
    
    return 0;
}   

int main()
{
    input();
    solve();
    return 0;
}

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

long long int dp[20][(1<<18)];
int n;
vector<pair<int,int>>v[20];


long long int fun(int pos,int mask)
{
    //if(mask>n)return 0;
    
    long long int &ans=dp[pos][mask];
    if(ans!=-1)
    return ans;

    for(auto p:v[pos])
    {
        int y=p.first;
        int z=p.second;
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(((mask>>i)&1)==1&&i+1<=y)
            cnt++;
        }
        if(cnt>z)
        {
            ans=0;
            return ans;
        }

    }
    if(pos==n)
    return ans=1;
    ans=0;
    for(int i=0;i<n;i++)
    {
        if(((mask>>i)&1)==1)continue;
        ans+=fun(pos+1,mask|(1<<i));
    }
    //cout<<mask<<endl;
    return ans;

    


    

}


void solve()
{
    int q;
    cin>>n>>q;
    memset(dp,-1,sizeof(dp));


    while(q--)
    {
        int x,y,z;
        cin>>x>>y>>z;
        v[x].push_back({y,z});
    }

    cout<<fun(0,0);

    return;
}

int main()
{
    int q=1;
    //cin>>q;
    while(q--)
    {
        solve();
        cout<<endl;

    }


    return 0;  
}