#include <bits/stdc++.h>
using namespace std;
using  ll = long long;
using P =pair<int,int>;
#define rep(i,n) for (int i = 0; i < (n); ++i)

int n,m;
std::vector<std::vector<int> > g;
std::map<P, int> v;
std::vector<int> ans;

void bfs(int nowP ,int ncost){
  // std::cout << nowP<<":"<<ncost << '\n';

  if(ans[nowP]!=-1)return ;

  ans[nowP]=ncost;


  for(int np :g[nowP]){

    if(ans[np]==-1){
      int cost =v[P(np,nowP)];
      if(cost == ncost){
        if(cost+1<n)cost++;
        else cost--;
      }
      bfs(np,cost);
    }
  }

}


int main(){

  std::cin >> n >>m;

  ans.resize(n,-1);
  g.resize(n,std::vector<int> ());

  rep(i,m){
    int v1,v2,c1;
    std::cin >> v1 >> v2 >>c1;
    v1--;v2--;c1--;
    g[v1].push_back(v2);
    g[v2].push_back(v1);
    v[P(v1,v2)]=c1;
    v[P(v2,v1)]=c1;
  }

  bfs(0,0);

  rep(i,n){
    if(ans[i]==-1){
      std::cout << "No" << '\n';
      return 0;
    }
  }

  rep(i,n){
    std::cout << ans[i]+1 << '\n';
  }







  return 0;
}

#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m;
    std::cin >> n >> m;
    std::vector<std::vector<std::pair<int, int>>> e(n);
    for (int i = 0; i < m; ++i) {
        int u, v, c;
        std::cin >> u >> v >> c;
        --u, --v, --c;
        e[u].emplace_back(v, c);
        e[v].emplace_back(u, c);
    }
    
    std::vector<int> a(n, -1);
    std::function<void(int, int, int)> dfs = [&](int u, int p, int c) {
        if (p == -1) {
            a[u] = 0;
        } else if (a[p] == c) {
            a[u] = (c + 1) % n;
        } else {
            a[u] = c;
        }
        for (auto [v, c] : e[u])
            if (a[v] == -1) dfs(v, u, c);
    };
    dfs(0, -1, -1);
    
    for (int i = 0; i < n; ++i) std::cout << a[i] + 1 << "\n";
    
    return 0;
}