#include<bits/stdc++.h>usingnamespace std;#defineintlonglongconstint N =2e5+10;constint mod =998244353;typedef pair<int,int> PII;int n, m, q, k;int a[N], b[N];
vector<PII> g[N];int vis[N], dist[N], cnt[N];intspfa(){memset(dist,0x3f,sizeof(dist));
queue<int64_t> q;
q.push(0);
vis[0]=0;
dist[0]=0;while(q.size()){int u = q.front();
q.pop();if(cnt[u]> n){return0;}
vis[u]=0;for(auto i : g[u]){if(dist[i.first]> dist[u]+ i.second){
dist[i.first]= dist[u]+ i.second;if(vis[i.first]){continue;}
q.push(i.first);
vis[i.first]=1;
cnt[i.first]++;}}}return1;}voidsolve(){
cin >> n >> m;// xi - xj <= cfor(int i =1; i <= m; i++){int u, v, w;
cin >> u >> v >> w;
g[v].push_back({u, w});}for(int i =1; i <= n; i++){
g[0].push_back({i,0});}if(spfa()){for(int i =1; i <= n; i++){
cout << dist[i]<<" \n"[i == n];}}else{
cout <<"NO\n";}}signedmain(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);int t =1;// cin >> t;while(t--){solve();}return0;}