import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* @author: pashka
*/
public class NegativeCycles {
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, 0);
int[] p = new int[n];
Arrays.fill(p, -1);
int[] lastEdge = new int[n];
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];
p[dst[i]] = src[i];
lastEdge[dst[i]] = len[i];
ok = true;
}
}
}
if (!ok) break;
// Find cycle
int[] state = new int[n];
Arrays.fill(state, -1);
for (int i = 0; i < n; i++) {
if (state[i] == -1) {
int j = i;
while (state[j] == -1) {
state[j] = i;
j = p[j];
if (j == -1) break;
}
if (j != -1 && state[j] == i) {
System.out.println("Negative Cycle!!!");
System.out.println("Iterations: " + c);
boolean[] mark = new boolean[n];
List<Integer> cycle = new ArrayList<>();
int s = 0;
while (!mark[j]) {
cycle.add(j);
s += lastEdge[j];
mark[j] = true;
j = p[j];
}
System.out.println("Cycle: " + cycle);
System.out.println("Cycle cost: " + s);
return;
}
}
}
}
System.out.println(c);
System.out.println(Arrays.toString(d));
}
}