import sys input = sys.stdin.readline from heapq import heappop,heappush N,M=map(int,input().split()) EDGE=[list(map(int,input().split())) for i in range(M)] E=[[] for i in range(N+1)] for i in range(M): x,y,d=EDGE[i] E[x].append((y,d,i+1)) E[y].append((x,-d,i+1)) D=[-(1<<63)]*(N+1) FROM=[-(1<<63)]*(N+1) flag=0 lastroute=-1 for i in range(1,N+1): if D[i]==-1<<63: D[i]=0 Q=[(0,i)] while Q: now,ind=heappop(Q) #print(Q,ind,now,D,FROM) #print(ind) for to,dis,route in E[ind]: if D[to]!=-1<<63 and D[to]!=now+dis: #print("!!",to) lastroute=(ind,to,route) flag=1 ANS=to break elif D[to]0: print(len(ANS)) print(p) print(*ANS) else: print(len(ANS)) print(p) print(*ANS[::-1])