from operator import itemgetter
inf = float('inf')

n,m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(m)]

a.sort(key=itemgetter(2))
a.sort(key=itemgetter(1))
a.sort(key=itemgetter(0))

d = [inf]*(n+1)
d[1]=0
for i in range(m):
    l,r,c = a[i]
    if d[l]==inf:
        print(-1)
        exit()
    for j in range(r-l):
        if d[r-j]>d[l]+c:
            d[r-j]=d[l]+c
        else:
            break
print(d[n]if d[n]!=inf else -1)