[N,M] = list(map(int,input().split()))

LRC = []
for i in range(M):
    tmp = list(map(int,input().split()))
    LRC.append((tmp[0],tmp[1],tmp[2]))

LRC = sorted(LRC, key=lambda x: (x[1], x[2], x[0]))

cnt = [2*10**9]*N
cnt[0] = 0

t=1
p=0
while p<M:
    L=LRC[p][0]
    R=LRC[p][1]
    C=LRC[p][2]
    if t >=R:
        t = R-1
    while t < R:
        cnt[t]=cnt[L-1]+C
        t+=1
    p+=1


if cnt[-1]==2*10**9:
    print(-1)
else:
    print(cnt[-1])