import sys
input=sys.stdin.readline
n,m=map(int,input().split())
lrc=[list(map(int,input().split())) for _ in range(m)]
lrc.sort(key=lambda x:x[0])
lrc.sort(key=lambda x:x[1])

l=[1]
r=[1]
c=[0]
for i in range(m):
    ll,rr,cc=lrc[i]
    l.append(ll)
    r.append(rr)
    c.append(cc)

if l[0]!=1:
    print(-1)
    exit()



import bisect

for i in range(1,m+1):
    x=bisect.bisect_left(r,l[i])
    if x>=i:
        continue
    c[i]+=min(c[x:i])


x=bisect.bisect_left(r,n)
if x==m:
    print(-1)
    exit()
ans=min(c[x:])


lrc.sort(key=lambda x:x[0])
ll=lrc[0][0]
rr=lrc[0][1]
for i in range(1,m):
    l,r,c=lrc[i]
    if rr<l:
        print(-1)
        exit()
    rr=r

print(ans)
