from heapq import heappop, heappush, heapify
import sys
sys.setrecursionlimit(10**9)
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
q = []
LRC = sorted([inpl() for _ in range(M)])
points = sorted(list({lrc[i] for i in range(2) for lrc in LRC}))
if points[0] != 1 or points[-1] != N:
    print(-1)
    exit()
positions = dict()
P = 0
prev = -1
for p in points:
    if p > prev:
        prev = p
        positions[p] = P
        P += 1
m = 0
dq = [(0,0)]
for i in range(P):
    while dq and dq[0][1] < i:
        heappop(dq)
    if not dq:
        print(-1)
        break
    d = dq[0][0]
    while m < M and positions[LRC[m][0]] == i:
        heappush(dq, (d+LRC[m][2], positions[LRC[m][1]]))
        m += 1
else:
    print(d)
