import sys
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
sys.setrecursionlimit(10**7)
n, m = [int(i) for i in sys.stdin.readline().split()]
INF = 10**9
graph = [[1e-10 if j - i == 1 else INF for i in range(n)] for j in range(n)]
for i in range(m):
    l, r, c = [int(i) for i in sys.stdin.readline().split()]
    l -= 1
    r -= 1
    graph[l][r] = c
    graph[r][l] = c
graph = csr_matrix(graph)
res = dijkstra(graph, indices=[0])
print(int(res[0][-1]) if res[0][-1] < INF else -1)