import numpy as np

n, m = list(map(int, input().split()))

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

dp = np.full(n + 1, np.inf)
dp[1] = 0

for e in edges:
    dp[e[1]] = min(min(dp[e[0]:(e[1] + 1)])+ e[2], dp[e[1]])

if np.isinf(dp[-1]):
    print(-1)
else:
    print(int(dp[-1]))