import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### 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] def dfs(n, e, U, V): g = [[] for _ in range(n)] for u, v, w, i in e: g[u].append((v, w, i)) g[v].append((u, -w, i)) prev_v = [-1] * n prev_e = [-1] * n seen = [False] * n q = [U] seen[U] = True while q: x = q.pop() for y, w, i in g[x]: if seen[y]: continue seen[y] = True prev_v[y] = x prev_e[y] = i q.append(y) path = [] x = V while x != U: path.append(prev_e[x]) x = prev_v[x] return path[::-1] n, m = na() e = [] for i in range(m): u, v, w = na() e.append((u-1, v-1, w, i)) wuf = WeightedUnionFind(n) for u, v, w, i in e: if wuf.same(u, v): if wuf.diff(u, v) > w: path = dfs(n, e[:i], u, v) print(len(path) + 1) print(u+1) print(*([path[i]+1 for i in range(len(path))] + [i+1])) exit() elif wuf.diff(u, v) < w: path = dfs(n, e[:i], v, u) print(len(path) + 1) print(v+1) print(*([path[i]+1 for i in range(len(path))] + [i+1])) exit() else: wuf.union(u, v, w) print(-1)