from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
import numpy as np
N,M=map(int, input().split())
graph=np.full((N,N),999999999,int)
for _ in range(M):
    A,B,C=map(int, input().split())
    for i in range(A-1,B-1):
        for j in range(i+1,B):
            graph[i,j]=min(C,graph[i,j])
            graph[j,i]=min(C,graph[j,i])
for i in range(N):
    graph[i,i]=0
    
graph = csr_matrix(graph)
print(int(floyd_warshall(csgraph=graph)[0,N-1]))
# print(int(dist_matrix))