n,m = map(int, input().split())
link = [[] for _ in range(n)]
for i in range(m):
    a,b,cost = list(map(int,input().split()))
    link[a-1].append([b-1,cost])
    link[b-1].append([a-1,cost])

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

from heapq import heappush, heappop,heappushpop,heapify
def dks(g,start,num_v):
    INF=float('inf')
    visited = set()
    hq = []
    heappush(hq, (0,start))
    cost = [INF] * num_v
    while hq:
        shortest, i = heappop(hq)
        if i in visited:
            continue
        visited.add(i)
        cost[i] = shortest
        for j, t in g[i]:
            if j in visited:
                continue
            heappush(hq, (shortest + t, j))
    return cost

ans=dks(link,0,n)
if ans[-1]!=float("inf"):
    print(ans[-1])
else:
    print(-1)