from bisect import bisect,insort
inf=float("inf")
n,m=map(int,input().split())
lrc=[list(map(int,input().split())) for i in range(m)]
lrc.sort()
data=[[1,0]]
h=1
for u in lrc:
    l,r,c=u
    a=bisect(data,[l])
    if a==h:
        print(-1)
        break
    b=data[a][1]
    insort(data,[r,b+c])
    h+=1
else:
    ans=inf
    for u in data:
        if u[0]==n:
            ans=min(ans,u[1])
    if ans==inf:
        print(-1)
    else:
        print(ans)