############################################################# import sys sys.setrecursionlimit(10**7) from heapq import heappop,heappush from collections import deque,defaultdict,Counter from bisect import bisect_left, bisect_right from itertools import product,combinations,permutations from math import sin,cos #from math import isqrt #DO NOT USE PyPy ipt = sys.stdin.readline iin = lambda :int(ipt()) lmin = lambda :list(map(int,ipt().split())) INF = 1<<60 def dijkstra_r(g,s,n): cost = [INF] * n hq = [] heappush(hq,(0,s)) while hq: c1,v1 = heappop(hq) if cost[v1] < c1: continue cost[v1] = c1 for v2,c2 in g[v1]: if c1+c2 < cost[v2]: cost[v2] = c1+c2 heappush(hq,(cost[v2],v2)) return cost ############################################################# N,M = lmin() G = [[] for _ in range(N)] RG = [[] for _ in range(N)] for _ in range(M): a,b,t = lmin() G[a-1].append((b-1,t)) RG[b-1].append((a-1,t)) cost2 = dijkstra_r(G,N-2,N) cost2r = dijkstra_r(RG,N-2,N) cost3 = dijkstra_r(G,N-1,N) cost3r = dijkstra_r(RG,N-1,N) for i in range(N-2): ans = min(cost2r[i]+cost2[N-1]+cost3[i],cost3r[i]+cost3[N-2]+cost2[i]) if ans >= INF: print(-1) else: print(ans)