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") ####################################################################### def find_cycle(N, M, G): vis = [0] * N used = [0] * M par_v = [None] * N par_e = [None] * N for s in range(N): if vis[s]: continue stack = [(s, -1, -1)] while stack: v, p, e = stack.pop() if e != -1 and used[e]: continue if vis[v]: par_v[v] = p if e != -1: par_e[v] = e return v, p, e, par_v, par_e vis[v] = 1 if e != -1: par_e[v] = e used[e] = 1 par_v[v] = p for a, e in G[v]: if used[e]: continue stack.append((a, v, e)) return -1, -1, -1, par_v, par_e def cycle_detection(N, M, G): v, p, e, par_v, par_e = find_cycle(N, M, G) if p == -1: return [], [] else: cycle_v = [p] cycle_e = [e] while v != p: e = par_e[p] p = par_v[p] cycle_v.append(p) cycle_e.append(e) return cycle_v[::-1], cycle_e[::-1] def find_cycle2(N, M, G): vis = [0] * N used = [0] * M par_v = [None] * N par_e = [None] * N potential = [0] * N for s in range(N): if vis[s]: continue stack = [(s, -1, -1, 0)] while stack: v, p, e, pot = stack.pop() if e != -1 and used[e]: continue if vis[v] and pot != potential[v]: par_v[v] = p if e != -1: par_e[v] = e return v, p, e, par_v, par_e vis[v] = 1 potential[v] = pot if e != -1: par_e[v] = e used[e] = 1 par_v[v] = p for a, e, w in G[v]: if used[e]: continue stack.append((a, v, e, pot + w)) return -1, -1, -1, par_v, par_e def cycle_detection2(N, M, G): v, p, e, par_v, par_e = find_cycle2(N, M, G) #print(v, p, e, par_v, par_e) if p == -1: return [], [] else: cycle_v = [p] cycle_e = [e] while v != p: e = par_e[p] p = par_v[p] cycle_v.append(p) cycle_e.append(e) return cycle_v[::-1], cycle_e[::-1] n, m = na() g = [[] for i in range(n)] E = [] for i in range(m): a, b, w = na() g[a-1].append((b-1, i, w)) g[b-1].append((a-1, i, -w)) E.append((a-1, b-1, w)) cycle_v, cycle_e = cycle_detection2(n, m, g) #print(cycle_v, cycle_e) if not cycle_v: print(-1) else: print(len(cycle_v)) ans = 0 c = 0 for i in cycle_e: if E[i][0] == c: ans += E[i][2] c = E[i][1] else: ans -= E[i][2] c = E[i][0] #print(ans) if ans > 0: print(cycle_v[0]+1) print(*[i+1 for i in cycle_e]) else: print(cycle_v[0]+1) print(*[i+1 for i in cycle_e[::-1]])