import sys input = sys.stdin.readline from heapq import heappop,heappush N,M=map(int,input().split()) E=[[] for i in range(N+1)] E_INV=[[] for i in range(N+1)] for i in range(M): u,v,t=map(int,input().split()) E[u].append((v,t)) E_INV[v].append((u,t)) def distance(x,EDGE): DIS=[1<<60]*(N+1) Q=[(0,x)] DIS[x]=0 while Q: time,now=heappop(Q) if DIS[now]!=time: continue for to,cost in EDGE[now]: if DIS[to]>DIS[now]+cost: DIS[to]=DIS[now]+cost heappush(Q,(DIS[to],to)) return DIS D1=distance(N-1,E) D2=distance(N-1,E_INV) D3=distance(N,E) D4=distance(N,E_INV) for i in range(1,N-1): a=D2[i]+D1[N]+D3[i] b=D4[i]+D3[N-1]+D1[i] ANS=min(a,b) if ANS>1<<60: print(-1) else: print(ANS)