#include <bits/stdc++.h>
#include <vector>

using namespace std;
typedef long long int64;

struct Edge {
    int to;
    int cost;
};

using Graph = vector<vector<Edge>>;

int ans[200010]={0};

int N=0;

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v,int prev_label) {
    //printf("%d is seen prev_label=%d\n",v,prev_label);
    seen[v] = true; // v を訪問済にする

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) {
        if (seen[next_v.to]) continue; // next_v が探索済だったらスルー

        int label=next_v.cost;
        if(prev_label!=label)ans[next_v.to]=label;
        else
        {
            if(label+1<=N)label++;
            else label--;
            ans[next_v.to]=label;
        }
        dfs(G, next_v.to,label); // 再帰的に探索
        //break;
    }
    if(ans[v]==0)
    {
        //printf("%d is ume\n",v);
        if(prev_label+1<=N)ans[v]=prev_label+1;
        else ans[v]=prev_label-1;
    }
}


int main() {
    // 頂点数と辺数、s と t
    int M;
    cin >> N >> M;

    // グラフ入力受取
    Graph G(N);
    for (int i = 0; i < M; ++i) {
        int a, b,c;
        cin >> a >> b >> c;
        Edge e,f;
        e.to = b-1;
        e.cost = c;
        f.to = a-1;
        f.cost = c;
        G[a-1].push_back(e);
        G[b-1].push_back(f);
    }

    // 頂点 s をスタートとした探索
    seen.assign(N, false); // 全頂点を「未訪問」に初期化
    ans[0]=1;
    dfs(G, 0,1);

    for(int i=0;i<N;i++)
    {
        if(ans[i]==0)printf("%d\n",N);
        else printf("%d\n",ans[i]);
    }
}
// *********************************************************************************
// *                        MURTAZA MUSTAFA KHUMUSI                                *
// *                        NIT-DGP,CSE - 2019-2023                                *
// *********************************************************************************

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loop1(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloop1(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define si set<int>
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);                \
    cout.tie(NULL);
const int maxm = 100001;

// ll m = 1e9+7;
ll m = 998244353;
vector<vector<pi>> adj;
vi h, sz, in,vis,ans;
vector<ll> fact;

//modular arithmentic

ll add(ll a, ll b, ll mod = m)
{
    return ((a % mod) + (b % mod)) % mod;
}
ll mul(ll a, ll b, ll mod = m)
{
    return ((a % mod) * (b % mod)) % mod;
}
ll sub(ll a, ll b, ll mod = m)
{
    return ((a % mod) - (b % mod) + mod) % mod;
}

ll modmul(ll a, ll b, ll mod = m)
{
    ll ans = 0;
    a = a % mod;
    while (b)
    {
        if (b & 1)
            ans = add(ans, a, mod);
        a = mul(a, 2, mod);
        b >>= 1;
    }
    return ans;
}

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

ll fe(ll base, ll exp, ll mod = m)
{
    ll ans = 1;
    while (exp)
    {
        if (exp & 1)
            ans = mul(ans, base, mod);
        base = mul(base, base, mod);
        exp >>= 1;
    }
    return ans;
}

ll modin(ll a, ll mod = m)
{
    return fe(a, mod - 2, mod);
}

ll nCr(ll a, ll b, ll mod = m)
{
    if (a < b)
        return 0;
    return mul(fact[a], mul(modin(fact[b], mod), modin(fact[a - b], mod)));
}

//graph theory
void dfs(int cur)
{
    vis[cur] = 1;
    for (const auto &v : adj[cur])
    {
        if (vis[v.ff])continue;
        if(v.ss==ans[cur]){
            if(v.ss==1)
            ans[v.ff] = 2;
            else
            ans[v.ff] = 1;
        }else{
            ans[v.ff] = v.ss;
        }
        dfs(v.ff);
    }
}

void buildGraph(int n, int m)
{
    adj = vector<vector<pi> >(n + 1);
    ans = vi(n+1);
    vis = vi(n+1);

    loop(i, 0, m)
    {
        int a, b, c;
        cin >> a >> b>>c;
        adj[a].pb(MP(b,c));
        adj[b].pb(MP(a,c));
    }
    ans[1] = 1;
    dfs(1);
}
struct cmp
{
    bool operator()(const pi &a, const pi &b)
    {
        return a.ff < b.ff;
    }
};

void solve()
{
    int n,m;cin>>n>>m;
    buildGraph(n, m);
    loop1(i,1,n){
        cout<<ans[i]<<"\n";
    }
    // cout<<"\n";
}
int main()
{
    io;
#ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
#endif
    int test;
    // cin >> test;?
    test=1;
    while (test--)
    {
        solve();
    }
    return 0;
}
