import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
LRC = [lr() for _ in range(M)]
LRC.sort()
INF = 10 ** 15
dp = [INF] * (N+1) # 1-indexed
dp[1] = 0
A = set([1]) # 最小値の候補
for l, r, c in LRC:
    rmv = []
    for ri in A:
        if ri < l:
            rmv.append(ri)
        else:
            dp[r] = min(dp[r], dp[ri] + c)
    for x in rmv:
        A.remove(x)
    A.add(r)

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