#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;

void dfs(int v, int other, vector<vector<pair<int, int>>>& Graph, vector<bool> &seen, vector<int> &color, int N) {
	if (v == 0) {
		if (Graph[v].size() > 0) color[0] = Graph[v][0].second;
	}
	else if (color[v] == -1) {
		color[v] = (other + 1) % N;
	}
	for (int i = 0; i < Graph[v].size(); i++) {
		if (seen[Graph[v][i].first]) continue;
		if (Graph[v][i].second == color[v]) {
			seen[Graph[v][i].first] = true;
			dfs(Graph[v][i].first, Graph[v][i].second, Graph, seen, color, N);
		}
		else {
			seen[Graph[v][i].first] = true;
			color[Graph[v][i].first] = Graph[v][i].second;
			dfs(Graph[v][i].first, N, Graph, seen, color, N);
		}
	}
}

int main() {
	int N, M;
	cin >> N >> M;
	vector<vector<pair<int, int>>> Graph(N);
	for (int i = 0; i < M; i++) {
		int u, v, c;
		cin >> u >> v >> c;
		u--; v--; c--;
		Graph[u].push_back(make_pair(v, c));
		Graph[v].push_back(make_pair(u, c));
	}
	
	vector<bool> seen(N, false);
	seen[0] = true;
	vector<int> color(N, -1);
	dfs(0, N, Graph, seen, color, N);

	for (int i = 0; i < N; i++) {
		cout << color[i] + 1 << endl;
	}
}
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define RI register int
typedef long long LL;

#define FILEIO(name) freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);

using namespace std;

namespace IO {
  char buf[1000000], *p1 = buf, *p2 = buf;
  inline char gc() {
    if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
    return p1 == p2 ? EOF : *(p1++);
  }
  template <class T> inline void read(T &n) {
    n = 0; RI ch = gc(), f;
    while ((ch < '0' || ch > '9') && ch != '-') ch = gc();
    f = (ch == '-' ? ch = gc(), -1 : 1);
    while (ch >= '0' && ch <= '9') n = n * 10 + (ch ^ 48), ch = gc();
    n *= f;
  }
  char Of[105], *O1 = Of, *O2 = Of;
  template <class T> inline void print(T n, char ch = '\n') {
    if (n < 0) putchar('-'), n = -n;
    if (n == 0) putchar('0');
    while (n) *(O1++) = (n % 10) ^ 48, n /= 10;
    while (O1 != O2) putchar(*(--O1));
    putchar(ch);
  }
}

using IO :: read;
using IO :: print;

int const MAXN = 2e5 + 5;
struct Edges { int to, next, val; } e[MAXN << 1];
int vis[MAXN], a[MAXN];
int head[MAXN], tot;
int n, m;

inline void addedge(int from, int to, int val) {
  e[++tot] = (Edges){to, head[from], val};
  head[from] = tot;
  e[++tot] = (Edges){from, head[to], val};
  head[to] = tot;
}

void Dfs(int now) {
  vis[now] = 1;
  for (RI i = head[now]; i; i = e[i].next)
    if (!vis[e[i].to]) {
      if (a[now] == e[i].val)
        a[e[i].to] = n + 1 - e[i].val;
      else
        a[e[i].to] = e[i].val;
      Dfs(e[i].to);
    }
}

int main() {
  
#ifdef LOCAL
  FILEIO("a");
#endif

  read(n), read(m);
  for (RI i = 1, x, y, z; i <= m; ++i)
    read(x), read(y), read(z), addedge(x, y, z);
  a[1] = 1;
  Dfs(1);
  for (RI i = 1; i <= n; ++i)
    if (!vis[i])
      return puts("No"), 0;
  for (RI i = 1; i <= n; ++i)
    printf("%d\n", a[i]);

  return 0;
}

// created by Daniel yuan
/*
     ________
    /        \
   / /      \ \
  / /        \ \
  \            /
   \  ______  /
    \________/
*/
