#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
#define P pair<int,int>
const ll MOD=1e9+7;
const int INF=2e9;
const double PI=acos(-1);


int main() {
    ios_base::sync_with_stdio(false);

    int n,m;
    cin >> n >> m;
    vector<vector<P>> r(n);
    rep(i,m) {
        int u,v,c;
        cin >> u >> v >> c;
        r[u-1].push_back({v-1,c});
        r[v-1].push_back({u-1,c});
    }
    queue<int> q;
    q.push(0);
    vector<int> ans(n),visited(n);
    ans[0]=1;
    visited[0]=1;
    while(!q.empty()) {
        int p=q.front();
        q.pop();
        int rs=r[p].size();
        rep(i,rs) {
            int np=r[p][i].first,c=r[p][i].second;
            if (visited[np]==0) {
                visited[np]=1;
                if (ans[p]==c) ans[np]=c%n+1;
                else ans[np]=c;
                q.push(np);
            }
        }
    }
    bool flag=1;
    rep(i,n) {
        if (visited[i]!=1) flag=0;
    }
    if (flag) {
        rep(i,n) cout << ans[i] << endl;
    } else cout << "No" << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7,INF=1e18;
#define ll long long
#define line cout<<"\n";
#define flush cout<<endl;
#define pll pair<ll,ll>
#define x first
#define y second
#define p_all(arr) for(auto i:arr){cout<<i<<" ";}cout<<"\n";
vector<ll> par,sze,val;
vector<vector<pll> > tree;
ll root(ll u)
{
    if(par[u]==u)
    return u;
    return par[u]=root(par[u]);
}
void union1(ll u,ll v)
{
    u=root(u),v=root(v);
    if(u==v)
    return ;
    if(sze[u]<sze[v])
    swap(u,v);
    sze[u]+=sze[v];
    par[v]=u;
}
void dfs(ll u,ll pa,ll value)
{
    for(auto v:tree[u])
    {
        if(v.x!=pa)
        {
            if(value!=v.y)
            {
                val[v.x]=v.y;
            }
            dfs(v.x,u,val[v.x]);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll t=1;
    // cin>>t;
    for(ll test=1;test<=t;test++)
    {
        ll n,m;
        cin>>n>>m;
        par.resize(n);
        sze.resize(n,1);
        tree.resize(n);
        val.resize(n,-1);
        for(int i=0;i<n;i++)
        par[i]=i;
        vector<pair<pll,ll> > edges(m);
        for(int i=0;i<m;i++)
        {
            ll u,v,w;
            cin>>u>>v>>w;
            u--,v--;
            edges[i]={{u,v},w};
        }
        vector<vector<ll> > val1(n);
        for(int i=0;i<m;i++)
        {
            if(root(edges[i].x.x)!=root(edges[i].x.y))
            {
                union1(edges[i].x.x,edges[i].x.y);
                val1[edges[i].x.x].push_back(edges[i].y);
                val1[edges[i].x.y].push_back(edges[i].y);
                tree[edges[i].x.x].push_back({edges[i].x.y,edges[i].y});
                tree[edges[i].x.y].push_back({edges[i].x.x,edges[i].y});
            }
        }
        dfs(0,-1,-1);
        for(int i=0;i<n;i++)
        {
            if(val[i]==-1)
            {
                sort(val1[i].begin(),val1[i].end());
                for(int j=1;j<=n;j++)
                {
                    if(j>val1[i].size()||val1[i][j-1]!=j)
                    {
                        val[i]=j;
                        break;
                    }
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            cout<<val[i]<<"\n";
        }
    }
    return 0;
}