#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 };  // i<4:4way i<8:8way
int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

int n,m;
vector<vector<pair<int,int>>> g;

void rec(int v,int c,vector<int> &res){
    res[v] = c;
    for(auto e: g[v]){
        if(res[e.first] != -1)continue;
        if(e.second == c)rec(e.first,(e.second+1)%n,res);
        else rec(e.first,e.second,res);
    }
}
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cin >> n >> m;
    g.resize(n);
    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        a--;b--;c--;
        g[a].push_back({b,c});
        g[b].push_back({a,c});
    }
    vector<int> res(n,-1);
    rec(0,0,res);
    for(auto v:res)cout << v + 1 << endl;
}
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=400005;
int fa[N],rk[N];
int h[N],nxt[N],to[N],ww[N],cnt=0;
int n,m;
int ans[N];
int getfa(int x) {
	if (fa[x]==x)	return x;
	return fa[x]=getfa(fa[x]);
}
void SetUnion(int fx,int fy) {
	if	(rk[fx]>rk[fy]) swap(fx,fy);
	fa[fx]=fy;
	rk[fy]+=rk[fx];
}
void add_e(int u,int v,int w) {
	to[++cnt]=v;
	ww[cnt]=w;
	nxt[cnt]=h[u];
	h[u]=cnt;
}

void dfs(int cur,int f,int path) {
	if (cur!=1) {
		if (ans[f]==path) {
			ans[cur]=1;
			if (ans[cur]==path)  ans[cur]++;
		} else ans[cur]=path;
	}
	for (int i=h[cur]; i; i=nxt[i]) {
		int v=to[i];
		if (v==f)  continue;

		dfs(v,cur,ww[i]);
	}
}

int main() {
	scanf("%d%d",&n,&m);
	for (int i=1; i<=n; i++) {
		rk[i]=1;
		fa[i]=i;
	}
	for (int i=1; i<=m; i++) {
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		int fx=getfa(u),fy=getfa(v);
		if (fx!=fy) {
			add_e(u,v,w);
			add_e(v,u,w);
			SetUnion(fx,fy);
		}
	}
	ans[1]=1;
	dfs(1,0,0);
	int flag=1;
	for (int i=1; i<=n; i++)
		if (ans[i]==0)	flag=0;
	if (flag==1)
		for (int i=1; i<=n; i++)
			printf("%d\n",ans[i]);
	else
		printf("No");
	return 0;
}