#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll inf = 0x3f3f3f3f;
const ll N = 5e5 + 9;
struct edge {
ll v, w;
};
vector<edge>G[N];
ll d[N], instk[N], cnt[N];
ll n, m;
void bel_ford(ll s) {
memset(d, 0x3f, sizeof d);
memset(instk, 0, sizeof instk);
memset(cnt, 0, sizeof cnt);
queue<int>q;
d[s] = 0;
instk[s] = 1;
q.push(s);
while (!q.empty()) {
auto u = q.front();
q.pop();
instk[u] = 0;// /???????????????????????!!!!!!!!!!!!!!!!!!!!
for (auto e:G[u]) {
ll v = e.v;
if (d[v] > d[u] + e.w) {
d[v] = d[u] + e.w;
if (!instk[v]) {
instk[v] = 1;
cnt[v] ++;
q.push(v);
if (cnt[v] == n + 1) {//关键,因为有0所以才n + 1
cout << "NO
";
exit(0);
}
}
}
}
}
}
void solve() {
cin >> n >> m;
for (ll i = 1; i <= m; i ++) {
ll u, v, w;
cin >> u >> v >> w;
G[v].push_back({u, w});//关键,必须要反向,因为是松弛 x_u - x_v <= w
}
for (ll i = 1; i <= n; i++) {
G[0].push_back({i, 0});
}
bel_ford(0);//关键,必须由0,
for (ll i = 1; i <= n; i ++) {
cout << d[i] << " ";
}
}
signed main() {
ll t = 1;//cin >> t;
while (t--) {
solve();
}
}