from heapq import heappush, heappop
def dijkstra(graph:list, node:int, start:int) -> list:
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist

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

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

d = dijkstra(graph,N,0)
ans = d[-1] if d[-1] != float('inf') else -1
print(ans)