import heapq
import sys
#input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, M = map(int, input().split())
LRC = [list(map(int, input().split())) for _ in range(M)]

def dijkstra_heap(s,edge):
    d = [float("inf")] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d



edge = [[] for i in range(n)]

for lrc in LRC:
    l = lrc[0] - 1
    r = lrc[1] - 1
    z = lrc[2]

    for x in range(l, r):
        for y in range(x+1, r+1):

            edge[x].append([z,y])
            edge[y].append([z,x])

l = dijkstra_heap(0, edge)
res = l[-1]

if res == float('inf'):
    print(-1)
else:
    print(res)