import sys
import heapq
input = sys.stdin.readline


def dijkstra(s, n, edge):
    d = [float("inf")] * n
    d[s] = 0
    q = []
    for e in edge[s]:
        heapq.heappush(q, e)
    while q:
        minedge = heapq.heappop(q)
        v = minedge[1]
        if d[v] < minedge[0]:
            continue
        d[v] = minedge[0]
        for e in edge[v]:
            heapq.heappush(q, [e[0]+d[v], e[1]])
    return d


N, M = map(int, input().split())
E = [[] for _ in range(N)]
for _ in range(M):
    s, t, c = map(int, input().split())
    E[s-1].append([c, t-1])
for i in range(N-1):
    E[i+1].append([0, i])

A = dijkstra(0, N, E)

ans = A[N-1]
if ans == float("inf"):
    print(-1)
else:
    print(ans)
