import sys input = sys.stdin.readline from heapq import heappush, heappop inf = float('inf') def dijkstra(s, g, N, G): # ゴールがない場合はg=-1とする。 def cost(v, m): return v * N + m dist = [inf] * N mindist = [inf] * N seen = [False] * N Q = [cost(0, s)] while Q: c, m = divmod(heappop(Q), N) if seen[m]: continue seen[m] = True dist[m] = c if m == g: return dist #------heapをアップデートする。-------- for u, C in G[m]: if seen[u]: continue newdist = dist[m] + C #------------------------------------ if newdist >= mindist[u]: continue mindist[u] = newdist heappush(Q, cost(newdist, u)) return dist N, M = map(int, input().split()) G = [[] for i in range(N)] Gr = [[] for i in range(N)] for i in range(M): u, v, t = map(int, input().split()) u, v = u - 1, v - 1 G[u].append((v,t)) Gr[v].append((u,t)) D1 = dijkstra(N - 2, -1, N, G) D2 = dijkstra(N - 1, -1, N, G) D1r = dijkstra(N - 2, -1, N, Gr) D2r = dijkstra(N - 1, -1, N, Gr) for i in range(N - 2): ans = min(D1[i] + D2r[i] + D2[N-2], D1r[i] + D2[i] + D1[N-1]) print(ans) if ans != inf else print(-1)