n,m=map(int,input().split())
p=sorted([list(map(int,input().split()))for i in range(m)])
a=2**(n-1).bit_length()
b=2**31-1
s=[b]*(2*a)
def f(k,x):
    k+=a-1
    s[k]=x
    while k>=0:
        k=(k-1)//2
        s[k]=min(s[2*k+1],s[2*k+2])
def g(l,r):
    L,R=l+a,r+a
    t=b
    while L<R:
        if R&1:
            R-=1
            t=min(t,s[R-1])
        if L&1:
            t=min(t,s[L-1])
            L+=1
        L>>=1
        R>>=1
    return s
f(0,0)
for l,r,c in p:
    q=g(l-1,r-1)
    if s[r+a-2]>q+c
        f(r-1,c+q)
print(-1 if s[n+a-2]==b else s[n+a-2])