import sys
import numpy as np

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 = np.full(N+1, INF) # 1-indexed
dp[1] = 0
for l, r, c in LRC:
    m = dp[l:r+1].min()
    dp[r] = min(dp[r], m + c)

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