from heapq import heappush, heappop N, M = map(int, input().split()) G = [[] for _ in range(N)] GR = [[] for _ in range(N)] for _ in range(M): u, v, t = map(int, input().split()) G[u-1].append((v-1, t)) GR[v-1].append((u-1, t)) INF = 10**18 def dijkstra(start, G): dist = [INF]*N dist[start] = 0 visited = [False]*N que = [(0, start)] while que: d, now = heappop(que) if visited[now]: continue visited[now] = True for next, weight in G[now]: if dist[now]+weight < dist[next]: dist[next] = dist[now]+weight heappush(que, (dist[next], next)) return dist distN1 = dijkstra(N-2, G) distN = dijkstra(N-1, G) distN1R = dijkstra(N-2, GR) distNR = dijkstra(N-1, GR) for i in range(N-2): ans = INF if distN1R[i] != INF and distN1[N-1] != INF and distN[i] != INF: ans = min(ans, distN1R[i]+distN1[N-1]+distN[i]) if distNR[i] != INF and distN[N-2] != INF and distN1[i] != INF: ans = min(ans, distNR[i]+distN[N-2]+distN1[i]) print(ans if ans != INF else -1)