[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[0], x[1]))

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

for i in range(M):
    Lt = LRC[i][0]
    Rt = LRC[i][1]
    Ct = LRC[i][2]
    cnt[Rt-1]=min(min(cnt[(Lt-1):(Rt-1)])+Ct,cnt[Rt-1])

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