from collections import defaultdict, deque INF = 1 << 60 N, M = map(int, input().split()) adj = defaultdict(list) r_adj = defaultdict(list) for _ in range(M): u, v, t = map(int, input().split()) u -= 1 v -= 1 adj[u].append((v, t)) r_adj[v].append((u, t)) def distance(src: int, adj) -> list[int]: q = deque([src]) dists = [INF] * N dists[src] = 0 while q: v = q.popleft() for to, c in adj[v]: if dists[to] <= dists[v] + c: continue dists[to] = dists[v] + c q.append(to) return dists d1 = distance(N-1, adj) d2 = distance(N-2, adj) r_d1 = distance(N-1, r_adj) r_d2 = distance(N-2, r_adj) for i in range(N-2): # i -> N-2 -> N-1 -> i res1 = r_d2[i] + d2[N-1] + d1[i] # i -> N-1 -> N-2 -> i res2 = r_d1[i] + d1[N-2] + d2[i] r = min(res1, res2) if r >= INF: print(-1) else: print(r)