import heapq
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N, M = list(map(int, sys.stdin.readline().split()))
LRC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]


def test():
    LRC.sort()
    dp = [INF] * N
    dp[0] = 0
    for l, r, c in LRC:
        l -= 1
        r -= 1
        for i in range(l, r + 1):
            dp[i] = min(dp[i], dp[l] + c)
    if dp[-1] == INF:
        print(-1)
    else:
        print(dp[-1])


# test()

heap = []
LRC.sort()
LRC = [(l - 1, r - 1, c) for l, r, c in LRC]
mi = 0
dp = [INF] * N
dp[0] = 0
for pos in range(N):
    if dp[pos] == INF:
        break
    while mi < M and LRC[mi][0] <= pos:
        l, r, c = LRC[mi]
        heapq.heappush(heap, (dp[pos] + c, r))
        mi += 1
    while heap and heap[0][1] <= pos:
        heapq.heappop(heap)
    if not heap:
        break
    dp[pos + 1] = heap[0][0]


if dp[-1] == INF:
    print(-1)
else:
    print(dp[-1])
