
from collections import deque
import heapq
N, M = map(int, input().split())
dp = [[float('inf')] * (N+1) for _ in range(N+1)]
for i in range(1+N):
    dp[i][i] = 0
for _ in range(M):
    l, r, c = map(int, input().split())
    for i in range(l, r+1):
        for j in range(i+1, r+1):
            dp[i][j] = min(dp[i][j], c)
            dp[j][i] = min(dp[j][i], c)
visited = set()
q = [(0, 1)]
while q:
    dis, idx = heapq.heappop(q)
    if idx == N:
        print(dis)
        exit()
    if idx in visited:  continue
    visited.add(idx)
    for u in range(1, N+1):
        if u != idx and dp[idx][u] != float('inf'):
            heapq.heappush(q, (dis+dp[idx][u], u))
print(-1)


