from heapq import *
from collections import *

N, M = map(int, input().split())
L, R, C = [0] * M, [0] * M, [0] * M

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

start = defaultdict(lambda: [])
end = defaultdict(lambda: [])
for i in range(M):
    start[L[i]].append(i)
    end[R[i]].append(i)

state = []
for i in start[0]:
    heappush(state, (C[i], i))

for i in range(1, N):
    while len(state) > 0 and R[state[0][1]] < i:
        heappop(state)

    if len(state) == 0:
        print(-1)
        exit()

    for j in start[i]:
        heappush(state, (state[0][0] + C[j], j))

print(state[0][0])
