import sys
input = sys.stdin.readline
from heapq import heappush, heappop

N,L = map(int,input().split())

LRC = [[int(x) for x in input().split()] for _ in range(L)]
LRC.sort() # Lの昇順

LRC

# [l,r]を使うなら、その時点での最安値と噛み合わせる

q = [(0,1)] # (コスト、右端)
for left,right,cost in LRC:
    while True:
        if len(q)==0:
            print(-1)
            exit()
        x,y = q[0]
        if y < left:
            heappop(q)
            continue
        break
    heappush(q,(x+cost,right)) 

while q:
    x,y = heappop(q)
    if y != N:
        continue
    answer = x
    break

print(answer)
