import sys
input = sys.stdin.readline
import heapq

def main():
    N,M = map(int,input().split())
    INF = 10**10+1
    root = [[INF for _ in range(N)] for _ in range(N)]
    for i in range(N-1):
        root[i+1][i] = 0
    for _ in range(M):
        a,b,c = map(int,input().split())
        root[a-1][b-1] = c
        root[b-1][a-1] = c
    
    dist = [INF for _ in range(N)]
    que = [(0,0)]
    dist[0] = 0
    while que:
        dif,now = heapq.heappop(que)
        for f,fol in enumerate(root[now]):
            if (fol != INF and dist[f] > dist[now] + fol):
                dist[f] = dist[now] + fol
                heapq.heappush(que,(dist[f],f))
                
    if dist[-1] == INF:
        print(-1)
    else:
        print(dist[-1])

if __name__ == "__main__":
    main()
