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

n, m = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(m)]
# edge.sort(key=lambda x: (x[1], x[0]))
edge.sort()

q = [(1, 0)]
for l, r, c in edge:
    # print((l, r, c), q)

    while q:
        y, z = heappop(q)
        if y >= l:
            heappush(q, (y, z))
            break

    cand = float("inf")
    for y, z in q:
        if y < r:
            cand = min(cand, z)
        else:
            break

    if cand != float("inf"):
        heappush(q, (r, cand + c))

# print(q)

ans = -1
for y, z in q:
    if y == n:
        if ans == -1:
            ans = z
        else:
            ans = min(ans, z)

print(ans)
