from collections import deque
import heapq

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

for i in range(M):
    
    L,R,C = map(int,input().split())
    L -= 1
    R -= 1
    LRC2.append([L,R,C])

LRC2.sort()

LRC = deque([])

for i in LRC2:
    LRC.append(i)

q = []

for i in range(N):

    while len(LRC) > 0 and LRC[0][0] == i:
        now = LRC.popleft()
        L = now[0]
        C = now[2]
        R = now[1]

        if L == 0:
            heapq.heappush(q,[C,R])
            continue

        while len(q) > 0 and q[0][1] < i:
            heapq.heappop(q)

        if len(q) > 0:
            heapq.heappush(q,[C + q[0][0] , R])

    #print (LRC,q)


while len(q) > 0 and q[0][1] < N-1:
    heapq.heappop(q)

#print (q)

if len(q) > 0:
    print (q[0][0])

else:
    print (-1)
