import sys input = sys.stdin.readline # https://at274.hatenablog.com/entry/2018/02/03/140504 class WeightedUnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.rank = [0] * (n+1) # 根への距離を管理 self.weight = [0] * (n+1) # 検索 def find(self, x): if self.par[x] == x: return x else: y = self.find(self.par[x]) # 親への重みを追加しながら根まで走査 self.weight[x] += self.weight[self.par[x]] self.par[x] = y return y # 併合 def union(self, x, y, w): rx = self.find(x) ry = self.find(y) # xの木の高さ < yの木の高さ if self.rank[rx] < self.rank[ry]: self.par[rx] = ry self.weight[rx] = w - self.weight[x] + self.weight[y] # xの木の高さ ≧ yの木の高さ else: self.par[ry] = rx self.weight[ry] = -w - self.weight[y] + self.weight[x] # 木の高さが同じだった場合の処理 if self.rank[rx] == self.rank[ry]: self.rank[rx] += 1 # 同じ集合に属するか def same(self, x, y): return self.find(x) == self.find(y) # xからyへのコスト def diff(self, x, y): return self.weight[x] - self.weight[y] from collections import deque def bfs(G, s): Q = deque([s]) N = len(G) dist = [-1] * N par = [-1] * N dist[s] = 0 while Q: u = Q.popleft() for v, i in G[u]: if dist[v] != -1: continue dist[v] = dist[u] + 1 par[v] = (u, i) Q.append(v) return par N, M = map(int, input().split()) U = WeightedUnionFind(N) flag = 1 Edge = [] start, goal = -1, -1 for i in range(M): u, v, w = map(int, input().split()) u, v = u - 1, v - 1 if U.same(u, v) and U.diff(u, v) != w: if U.diff(u, v) < w: flag = 1 else: flag = 0 start = u goal = v break U.union(u, v, w) Edge.append((u, v)) if start == -1: print(-1) exit() G = [[] for i in range(N)] for i in range(len(Edge)): u, v = Edge[i] G[u].append((v, i)) G[v].append((u, i)) par = bfs(G, start) ans = [] while goal != start: nv, ni = par[goal] ans.append(ni + 1) goal = nv ans = [len(Edge) + 1] + ans if flag == 0: ans.reverse() print(len(ans)) print(start + 1) print(*ans)