n,m = map(int, input().split())
edge = [list(map(int, input().split())) for i in range(m)]
edge.sort()
from heapq import heappop,heapify,heappush
heapify(edge)

heap = [(0, 1)]
dist = [float("inf")]*(n+1)
while heap:
    cost, cur = heappop(heap)
    if dist[cur] <= cost:
        continue
    dist[cur] = cost
    while edge:
        if edge[0][0] <= cur:
            l,r,c = heappop(edge)
            if r < cur:
                continue
            heappush(heap,(cost+c, r))
        else:
            break

if dist[-1] == float("inf"):
    print(-1)
else:
    print(dist[-1])