# import pypyjit # pypyjit.set_param("max_unroll_recursion=-1") from collections import * from functools import * from itertools import * from heapq import * import sys, math # sys.setrecursionlimit(3*10**5) input = sys.stdin.readline INF = (1<<60) def dijkstra(s,e): N = len(e) dist = [INF]*N dist[s]=0 h = [] heappush(h,s) while h: nw,v = divmod(heappop(h),N) if dist[v]!=nw: continue for iv,ic in e[v]: nc = ic + nw if nc < dist[iv]: dist[iv] = nc heappush(h,nc*N + iv) return dist N,M = map(int,input().split()) e = [[] for _ in range(N)] re = [[] for _ in range(4*N)] for _ in range(M): u,v,t = map(int,input().split()) u -= 1 v -= 1 e[u].append((v,t)) re[v].append((u,t)) re[v+N].append((u+N,t)) re[v+2*N].append((u+2*N,t)) re[v+3*N].append((u+3*N,t)) if v==N-1: re[v+N].append((u,t)) re[v+3*N].append((u+2*N,t)) if v==N-2: re[v+2*N].append((u,t)) re[v+3*N].append((u+N,t)) A = dijkstra(N-1+3*N,re) B = dijkstra(N-2+3*N,re) C = dijkstra(N-1,e) D = dijkstra(N-2,e) # print(A) # print(B) # print(C) # print(D) for i in range(N-2): val = min(A[i]+C[i],B[i]+D[i]) if val>=INF: print(-1) else: print(val)