INFTY = 10**14+5
N,M = map(int,input().split())
A = sorted([list(map(int,input().split())) for _ in range(M)],key=lambda x:x[1])
A = sorted(A,key=lambda x:x[0])
d = [INFTY for _ in range(N+1)]
d[1] = 0
for i in range(M):
    L,R,C = A[i]
    d[R] = min(d[R],d[L]+C)
    j = R-1
    while d[j]>d[R]:
        d[j] = d[R]
        j -= 1
if d[N]>=INFTY:
    print(-1)
else:
    print(d[N])