using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); static string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m) = (c[0], c[1]); var map = NArr(m); var tree = new List<(int id, int to, long len)>[n]; for (var i = 0; i < n; ++i) tree[i] = new List<(int id, int to, long len)>(); for (var i = 0; i < m; ++i) { var edge = map[i]; tree[edge[0] - 1].Add((i, edge[1] - 1, edge[2])); tree[edge[1] - 1].Add((i, edge[0] - 1, -edge[2])); } var len = Enumerable.Repeat(INF, n).ToArray(); len[0] = 0; if (!DFS(0, tree, len, new List(), new List())) { WriteLine(-1); } } static long INF = long.MinValue; static bool DFS(int cur, List<(int id, int to, long len)>[] tree, long[] len, List vlist, List elist) { foreach (var next in tree[cur]) { if (len[next.to] == INF) { len[next.to] = len[cur] + next.len; vlist.Add(cur); elist.Add(next.id); if (DFS(next.to, tree, len, vlist, elist)) { return true; } vlist.RemoveAt(vlist.Count - 1); elist.RemoveAt(elist.Count - 1); } else { if (len[next.to] == len[cur] + next.len) continue; var start = vlist.LastIndexOf(cur); var route = elist.Skip(start).ToList(); route.Insert(0, next.id); if (len[next.to] > len[cur] + next.len) route.Reverse(); WriteLine(route.Count); WriteLine(cur + 1); WriteLine(string.Join(" ", route.Select(r => r + 1))); return true; } } return false; } }