import sys,heapq
input = sys.stdin.readline

n,m = map(int,input().split())

dg = 10**6


edge = [[] for _ in [0]*n]

for _ in [0]*m:
    l,r,c = map(int,input().split())
    edge[l-1].append(c*dg+r-1)

d = [10**20]*n
d[0] = 0

tank = []
for e in edge[0]:
    heapq.heappush(tank,e)
while len(tank) != 0:
    q = heapq.heappop(tank)
    go = q%dg
    if d[go] != 10**20:
        continue
    cost = q//dg

    for i in range(go,-1,-1):
        if d[i] != 10**20:
            break
        d[i] = cost
        for e in edge[i]:
            heapq.heappush(tank,e+cost*dg)
if d[-1] == 10**20:
    print(-1)
else:
    print(d[-1])
    





