import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author: pashka
 */
public class FordBellman {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("input.txt"));
        int n = in.nextInt();
        int m = in.nextInt();
        int[] src = new int[m];
        int[] dst = new int[m];
        int[] len = new int[m];
        for (int i = 0; i < m; i++) {
            src[i] = in.nextInt() - 1;
            dst[i] = in.nextInt() - 1;
            len[i] = in.nextInt();
        }

        int[] d = new int[n];
        Arrays.fill(d, Integer.MAX_VALUE);
        d[0] = 0;

        int c = 0;
        while (true) {
            c++;
            boolean ok = false;
            for (int i = 0; i < m; i++) {
                if (d[src[i]] < Integer.MAX_VALUE) {
                    if (d[src[i]] + len[i] < d[dst[i]]) {
                        d[dst[i]] = d[src[i]] + len[i];
                        ok = true;
                    }
                }
            }
            if (!ok) break;
        }

        System.out.println("Iterations: " + c);
        System.out.println("Distances: " + Arrays.toString(d));
    }
}