import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
/**
* @author: pashka
*/
public class Johnson {
class Edge {
int src, dst, len;
public Edge(int src, int dst, int len) {
this.src = src;
this.dst = dst;
this.len = len;
}
}
private void run() throws FileNotFoundException {
Scanner in = new Scanner(new File("input.txt"));
int n = in.nextInt();
int m = in.nextInt();
List<Edge>[] outgoing = new List[n];
for (int i = 0; i < n; i++) {
outgoing[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int src = in.nextInt() - 1;
int dst = in.nextInt() - 1;
int len = in.nextInt();
outgoing[src].add(new Edge(
src, dst, len
));
}
int[] d = new int[n];
Arrays.fill(d, 0);
Queue<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
q.add(i);
}
boolean[] inQueue = new boolean[n];
Arrays.fill(inQueue, true);
int c = 0;
while (q.size() > 0) {
c++;
int i = q.poll();
inQueue[i] = false;
for (Edge edge : outgoing[i]) {
if (d[edge.dst] > d[i] + edge.len) {
d[edge.dst] = d[i] + edge.len;
if (!inQueue[edge.dst]) {
q.add(edge.dst);
inQueue[edge.dst] = true;
}
}
}
}
for (int i = 0; i < n; i++) {
for (Edge edge : outgoing[i]) {
int newLen =
edge.len + d[edge.src] -
d[edge.dst];
if (newLen < 0) {
throw new RuntimeException();
}
}
}
System.out.println(c);
System.out.println(Arrays.toString(d));
}
public static void main(String[] args) throws FileNotFoundException {
new Johnson().run();
}
}