import numpy as np
import scipy
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
N,M = [int(x) for x in input().split()]
path = np.array([[float("INF")]*(N) for _ in range(N)])
#path[:,0]=0
#path[0,:]=0
for i in range(M):
  a,b,c = [int(x) for x in input().split()]
  a = a-1
  b=b-1
  path1 = np.copy(path)
  path1[a:b+1,a:b+1]=c
  path = np.minimum(path,path1)
np.fill_diagonal(path,0)
d = dijkstra(path,indices = 0)
print(int(d[N-1]))
#print(path.sum())