import bisect
N, M = map(int, input().split())
LRCs = []
for i in range(M):
  L, R, C = map(int, input().split())
  L, R = L-1, R-1
  LRCs.append((L, R, C))
LRCs.sort()
#print(LRCs)
Ds = [-1]*N
Ds[0] = 0
Rs = sorted([0]+[R for L, R, C in LRCs])
#print(Rs)
for L, R, C in LRCs:
  i = bisect.bisect_left(Rs, L)
  j = bisect.bisect_left(Rs, R)
  #print(i, j)
  if i == len(Rs):
    continue
  L = Rs[i]
  #print(L, Ds)
  if Ds[L] < 0:
    continue
  for k in range(i+1, j+1):
    R = Rs[k]
    if Ds[R] < 0:
      Ds[R] = Ds[L]+C
    else:
      Ds[R] = min(Ds[R], Ds[L]+C)
r = Ds[-1]
print(r)
