#include <bits/stdc++.h>
using namespace std;
inline int read(int& x) {
char ch = getchar();
int f = 1; x = 0;
while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
return x * f;
}
static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();
const int maxn = 5e3 + 7,INF = 1e9 + 7;
int n, m,dis[maxn],vis[maxn],cnt[maxn];
vector<pair<int,int>> e[maxn];
bool spfa(int cur) {
for (int i = 0; i <= n; i++) { dis[i] = INF; vis[i] = 0; }
dis[cur] = 0;
queue<int> q;
q.push(cur);
while (!q.empty()) {
int top = q.front(); q.pop();
vis[top] = 0;
for (auto& point : e[top]) {
int nx = point.first, v = point.second;
if (dis[nx] > dis[top] + v) {
dis[nx] = dis[top] + v;
if (cnt[nx] >= n) return false;
if (!vis[nx]) {
vis[nx] = 1;
cnt[nx]++;
q.push(nx);
}
}
}
}
return true;
}
void slove() {
cin >> n >> m;
int a, b, c;
for (int i = 1; i <= m; i++) {
cin >> a >> b >> c;
e[b].push_back({ a,c });
}
for (int i = 1; i <= n; i++) {
e[0].push_back({i,0});
}
if (!spfa(0)) {
cout << "NO" << endl;
}
else {
for (int i = 1; i <= n; i++) {
cout << dis[i] << " ";
}
cout << endl;
}
}
int main() {
slove();
return 0;
}