import heapq
import sys
N, M = map(int, input().split())
edges = []
for _ in range(M):
    l, r, c = map(int, input().split())
    edges.append((l-1, r-1, c))
edges.sort(reverse = True)
dp = [0]*N
que = [(0, 0)]
for i in range(1, N):
    while len(edges)>0 and edges[-1][0] == i-1:
        e = edges.pop()
        heapq.heappush(que, (dp[i-1]+e[2], e[1]))
    while True:
        if len(que) == 0:
            print(-1)
            sys.exit()
        e = heapq.heappop(que)
        if e[1]>=i:
            dp[i] = e[0]
            heapq.heappush(que, e)
            break
print(dp[-1])