# instead of AVLTree



INF = 10**14



class BIT():

    def __init__(self, max):

        self.max = max

        self.data = [INF]*(self.max+1)

    

    def update(self, i, x):

        while i > 0:

            self.data[i] = min(self.data[i], x)

            i -= i & -i



    def read(self, i):

        m = INF

        while i <= self.max:

            m = min(self.data[i], m)

            i += i & -i

        return m



import sys

input = sys.stdin.readline

from operator import itemgetter



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

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

LRC.sort(key=itemgetter(0))



bit = BIT(N)

bit.update(1, 0)



for l, r, c in LRC:

    m = bit.read(l)

    bit.update(r, m+c)



ans = bit.data[N]

if ans == INF:

    print((-1))

else:

    print(ans)
import sys

input = sys.stdin.readline

import heapq as hp



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

graph = [[] for _ in range(N)]

for _ in range(M):

    l, r, c = list(map(int, input().split()))

    graph[l-1].append((c, r-1))



for i in range(N-1):

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



INF = 10**16

D = [INF for _ in range(N)] # 頂点iへの最短距離がD[i]

D[0] = 0 

q = [] # しまっていく優先度付きキュー

hp.heappush(q, (0, 0))



while q:

    nd, np = hp.heappop(q) # 一番距離が近いものを取ってくる

    if D[np] < nd: # 追加した後の残骸は見ない

        continue

    for d, p in graph[np]:

        if D[p] > D[np] + d: # 隣接するやつの中で今よりも近くなれるなら更新

            D[p] = D[np] + d

            hp.heappush(q, (D[p], p))



d = D[-1]

if d == INF:

    print((-1))

else:

    print(d)
# instead of AVLTree
INF = 10**14


class BIT:
    def __init__(self, max):
        self.max = max
        self.data = [INF] * (self.max + 1)

    def update(self, i, x):
        while i > 0:
            self.data[i] = min(self.data[i], x)
            i -= i & -i

    def read(self, i):
        m = INF
        while i <= self.max:
            m = min(self.data[i], m)
            i += i & -i
        return m


import sys

input = sys.stdin.readline
from operator import itemgetter

N, M = list(map(int, input().split()))
LRC = [list(map(int, input().split())) for _ in range(M)]
LRC.sort(key=itemgetter(0))
bit = BIT(N)
bit.update(1, 0)
for l, r, c in LRC:
    m = bit.read(l)
    bit.update(r, m + c)
ans = bit.data[N]
if ans == INF:
    print((-1))
else:
    print(ans)

import sys

input = sys.stdin.readline
import heapq as hp

N, M = list(map(int, input().split()))
graph = [[] for _ in range(N)]
for _ in range(M):
    l, r, c = list(map(int, input().split()))
    graph[l - 1].append((c, r - 1))
for i in range(N - 1):
    graph[i + 1].append((0, i))
INF = 10**16
D = [INF for _ in range(N)]  # 頂点iへの最短距離がD[i]
D[0] = 0
q = []  # しまっていく優先度付きキュー
hp.heappush(q, (0, 0))
while q:
    nd, np = hp.heappop(q)  # 一番距離が近いものを取ってくる
    if D[np] < nd:  # 追加した後の残骸は見ない
        continue
    for d, p in graph[np]:
        if D[p] > D[np] + d:  # 隣接するやつの中で今よりも近くなれるなら更新
            D[p] = D[np] + d
            hp.heappush(q, (D[p], p))
d = D[-1]
if d == INF:
    print((-1))
else:
    print(d)

-# instead of AVLTree -INF = 10**14 - - -class BIT: -    def __init__(self, max): -        self.max = max -        self.data = [INF] * (self.max + 1) - -    def update(self, i, x): -        while i > 0: -            self.data[i] = min(self.data[i], x) -            i -= i & -i - -    def read(self, i): -        m = INF -        while i <= self.max: -            m = min(self.data[i], m) -            i += i & -i -        return m - - -from operator import itemgetter +import heapq as hp -LRC = [list(map(int, input().split())) for _ in range(M)] -LRC.sort(key=itemgetter(0)) -bit = BIT(N) -bit.update(1, 0) -for l, r, c in LRC: -    m = bit.read(l) -    bit.update(r, m + c) -ans = bit.data[N] -if ans == INF: +graph = [[] for _ in range(N)] +for _ in range(M): +    l, r, c = list(map(int, input().split())) +    graph[l - 1].append((c, r - 1)) +for i in range(N - 1): +    graph[i + 1].append((0, i)) +INF = 10**16 +D = [INF for _ in range(N)]  # 頂点iへの最短距離がD[i] +D[0] = 0 +q = []  # しまっていく優先度付きキュー +hp.heappush(q, (0, 0)) +while q: +    nd, np = hp.heappop(q)  # 一番距離が近いものを取ってくる +    if D[np] < nd:  # 追加した後の残骸は見ない +        continue +    for d, p in graph[np]: +        if D[p] > D[np] + d:  # 隣接するやつの中で今よりも近くなれるなら更新 +            D[p] = D[np] + d +            hp.heappush(q, (D[p], p)) +d = D[-1] +if d == INF: -    print(ans) +    print(d)