n,m=map(int,input().split())
lrc=[list(map(int,input().split())) for _ in range(m)]
lrc.sort(key=lambda x:x[0])
# 
inf=float('inf')
seen=[inf]*n
seen[0]=0
now=0
from heapq import heappop,heappush
from collections import deque
lrc=deque(lrc)
todo=[[0,now]]
while todo:
  d,v=heappop(todo)
  lst=[]
  while lrc and lrc[0][0]<=v+1:
    lst.append(lrc.popleft())
  for l,r,c in lst:
    if seen[r-1]>seen[v]+c:
      heappush(todo,[seen[v]+c,r-1])
      seen[r-1]=seen[v]+c
if seen[-1]!=inf:
  print(seen[-1])
else:
  print(-1)
#print(seen)
