from heapq import heappush, heappop
import sys
input = sys.stdin.readline

INF = float('inf')

# 単一始点最短経路を求める（Dijkstra法）
def Dijkstra(adjList, vSt):
    numV = len(adjList)
    numUsed = 0
    costs = [INF] * numV
    costs[vSt] = 0
    PQ = []
    heappush(PQ, (0, vSt))
    while PQ:
        cNow, vNow = heappop(PQ)
        if cNow > costs[vNow]: continue
        numUsed += 1
        if numUsed == numV: break
        for v2, wt in adjList[vNow]:
            c2 = cNow + wt
            if c2 < costs[v2]:
                costs[v2] = c2
                heappush(PQ, (c2, v2))
    return costs

N, M = map(int, input().split())
adjL = [[] for _ in range(N)]
for _ in range(M):
    L, R, C = map(int, input().split())
    L, R = L-1, R-1
    adjL[L].append((R, C))
    adjL[R].append((L, C))

for i in range(N-1):
    adjL[i+1].append((i, 0))

costs = Dijkstra(adjL, 0)

if costs[-1] == INF:
    print(-1)
else:
    print(costs[-1])
