n,m=map(int,input().split())
a=[[[i-1,0]]for i in range(n+1)]
a[0]=[]
for i in range(m):
    l,r,c=map(int,input().split())
    a[l].append([r,c])
q=[float("INF")]*(n+1)
q[1]=0
c=[[1,0]]
while c:
    s,t=c.pop(0)
    for i,j in a[s]:
        if q[i]>t+j:
            q[i]=t+j
            c.append([i,t+j])
print(-1 if q[-1]==float("INF") else q[-1])