from operator import itemgetter
N,M = [int(x) for x in input().split()]
LRC = []
event = []
dists = [float('inf')]*N
dists[0] = 0
cnt_key = 0
for _ in range(M):
    l, r, c = [int(x) for x in input().split()]
    event.append((l-1, c, 1, cnt_key))
    event.append((r-1, c, -1, cnt_key))
    cnt_key += 1

event.sort(key=itemgetter(0))
paths = {}
now_dist = 0
for ev in event:
    x = ev[0]
    for s in paths:
        if(dists[x]<paths[s][1]):
            paths[s][1] = dists[x]
        dists[x] = min(dists[x], paths[s][0]+paths[s][1])

    if(ev[2]==1):
        paths[ev[3]] = [ev[1], dists[ev[0]]]
    else:
        del paths[ev[3]]

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