import heapq

N,M = list(map(int,input().split()))

LRC = [tuple(map(int,input().split())) for i in range(M)]



es = [[] for i in range(N)]

for l,r,c in LRC:

    l,r = l-1,r-1

    es[l].append((r,c))

for i in range(N-1):

    es[i+1].append((i,0))



INF = 10 ** 18

dist = [INF]*N

dist[0] = 0

hq = [(0,0)]

heapq.heapify(hq)



while(hq):

    d,v = heapq.heappop(hq)

    if v==N-1:

        print(d)

        exit()

    for to,c in es[v]:

        if d+c >= dist[to]: continue

        dist[to] = d+c

        heapq.heappush(hq, (d+c, to))

print((-1))

import sys

input = sys.stdin.readline



import heapq

N,M = list(map(int,input().split()))

LRC = [tuple(map(int,input().split())) for i in range(M)]



es = [[] for i in range(N)]

for l,r,c in LRC:

    l,r = l-1,r-1

    es[l].append((r,c))

for i in range(N-1):

    es[i+1].append((i,0))



INF = float("inf")

dist = [INF]*N

dist[0] = 0

hq = [(0,0)]

heapq.heapify(hq)



while(hq):

    d,v = heapq.heappop(hq)

    if v==N-1:

        print(d)

        exit()

    for to,c in es[v]:

        if d+c >= dist[to]: continue

        dist[to] = d+c

        heapq.heappush(hq, (d+c, to))

print((-1))
import heapq

N, M = list(map(int, input().split()))
LRC = [tuple(map(int, input().split())) for i in range(M)]
es = [[] for i in range(N)]
for l, r, c in LRC:
    l, r = l - 1, r - 1
    es[l].append((r, c))
for i in range(N - 1):
    es[i + 1].append((i, 0))
INF = 10**18
dist = [INF] * N
dist[0] = 0
hq = [(0, 0)]
heapq.heapify(hq)
while hq:
    d, v = heapq.heappop(hq)
    if v == N - 1:
        print(d)
        exit()
    for to, c in es[v]:
        if d + c >= dist[to]:
            continue
        dist[to] = d + c
        heapq.heappush(hq, (d + c, to))
print((-1))

import sys

input = sys.stdin.readline
import heapq

N, M = list(map(int, input().split()))
LRC = [tuple(map(int, input().split())) for i in range(M)]
es = [[] for i in range(N)]
for l, r, c in LRC:
    l, r = l - 1, r - 1
    es[l].append((r, c))
for i in range(N - 1):
    es[i + 1].append((i, 0))
INF = float("inf")
dist = [INF] * N
dist[0] = 0
hq = [(0, 0)]
heapq.heapify(hq)
while hq:
    d, v = heapq.heappop(hq)
    if v == N - 1:
        print(d)
        exit()
    for to, c in es[v]:
        if d + c >= dist[to]:
            continue
        dist[to] = d + c
        heapq.heappush(hq, (d + c, to))
print((-1))

+import sys + +input = sys.stdin.readline -INF = 10**18 +INF = float("inf")