import numpy as np
from scipy.sparse.csgraph import dijkstra
import itertools
from scipy.sparse import csr_matrix
inf = (10.0) ** 2
n, m = list(map(int, input().split()))

graph = np.full((n+1,n+1), inf)
for i in range(1,n+1):
    graph[i][i - 1] = 0.0001
for i in range(m):
    l, r, c = list(map(int, input().split()))
    graph[l][r] = min(graph[l][r],  c)
    graph[r][l] = min(graph[r][l],  c)
graph = dijkstra(graph)

if graph[1][n] == inf:
    print(-1)
else:
    print(int(graph[1][n]))