import sys
import math
from heapq import heappop, heappush

N, M = [int(x) for x in sys.stdin.readline().split()]
LRC = []
for _ in range(M):
    LRC.append([int(x) for x in sys.stdin.readline().split()])

dist = [math.inf] * N
dist[0] = 0

routes = [None] * N
for i in range(N):
    routes[i] = [math.inf] * N

for l, r, c in LRC:
    for i in range(l - 1, r - 1):
        for j in range(i + 1, r):
            routes[i][j] = min(routes[i][j], c)
            routes[j][i] = min(routes[j][i], c)

q = []
heappush(q, (dist[0], 0))

while q:
    d, u = heappop(q)
    if dist[u] < d:
        continue
    for i, w in enumerate(routes[u]):
        t = d + w
        if t < dist[i]:
            dist[i] = t
            heappush(q, (t, i))

if dist[-1] == math.inf:
    print(-1)
else:
    print(dist[-1])