n, m, *t = map(int, open(0).read().split())
inf = float('inf')
dp = [inf] * -~n
dp[1] = 0

import heapq

mn = []
path = eval('[],' * -~n)
for l, r, c in zip(*[iter(t)] * 3):
    path[l].append((r, c))


def ins(v, r):
    heapq.heappush(mn, (v, r))


for i in range(1, n + 1):
    while len(mn) and mn[0][1] < i:
        heapq.heappop(mn)
    if len(mn):
        dp[i] = min(dp[i], mn[0][0])
    for r, c in path[i]:
        ins(dp[i] + c, r)

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