import sys
input=sys.stdin.readline
import heapq
n,m=map(int,input().split())

D=[0]
P=[]
ans=float("inf")

for i in range(m):
  l,r,c=map(int,input().split())
  P.append((l,r,c))
P.sort(key=lambda x:x[0])
heapq.heappush(D,1*10**15+0)
for l,r,c in P:
  while D:
    ld=heapq.heappop(D)
    ll=ld//(10**15)
    dd=ld%(10**15)
    if ll<l:
      continue
    else:
      break
  if ll>=l:
    heapq.heappush(D,r*10**15+dd+c)
  heapq.heappush(D,ll*10**15+dd)
print(D)
while D:
  ld=heapq.heappop(D)
  ll=ld//(10**15)
  dd=ld%(10**15)
  if ll==n:
    ans=min(ans,dd)
print(-1 if ans==float("inf") else ans)


  