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 ans = Route(n, m, map); WriteLine(ans); } static string Route(int n, int m, int[][] map) { 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(); var ans = ""; for (var i = 0; i < n; ++i) { if (len[i] == INF) { len[i] = 0; if (DFS(i, tree, len, new List(), new List(), ref ans)) return ans; } } return "-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, ref string ans) { 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, ref ans)) { 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(next.to); var route = elist.Skip(start).ToList(); route.Insert(0, next.id); if (len[next.to] > len[cur] + next.len) route.Reverse(); ans = $"{route.Count}\n{cur + 1}\n{string.Join(" ", route.Select(r => r + 1))}"; return true; } } return false; } }