import java.io.*;
import java.util.Arrays;
public class 差分约束 {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static StreamTokenizer t = new StreamTokenizer(br);
public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static PrintWriter out = new PrintWriter(bw);
public static int nextInt() throws Exception {
t.nextToken();
return (int) t.nval;
}
public static int n, m;
public static class Edge {
int u;
int v;
int w;
public Edge(int u, int v, int w) {
this.u = u;
this.v = v;
this.w = w;
}
}
public static Edge[] edges;
public static int[] head, dist;
public static boolean Bellman_Ford() {
for (int i = 0; i < n - 1; i += 1) {
for (int j = 0; j < m; j += 1) {
int u = edges[j].u, v = edges[j].v, w = edges[j].w;
if (dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
}
}
}
for (int i = 0; i < m; i += 1) {
int u = edges[i].u, v = edges[i].v, w = edges[i].w;
if (dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v]) {
return false;
}
}
return true;
}
public static void main(String[] args) throws Exception {
n = nextInt();
m = nextInt();
edges = new Edge[m + n];
head = new int[n + 1];
dist = new int[n + 1];
Arrays.fill(head, -1);
Arrays.fill(dist, Integer.MAX_VALUE);
dist[0] = 0;
for (int i = 0; i < m; i += 1) {
int u = nextInt(), v = nextInt(), w = nextInt();
edges[i] = new Edge(v, u, w);
}
for (int i = 1; i <= n; i += 1) {
edges[m] = new Edge(0, i, 0);
m += 1;
}
if (Bellman_Ford()) {
for (int i = 1; i <= n; i += 1) {
out.print(dist[i] + " ");
}
} else {
out.print("NO");
}
out.flush();
}
}