import sys

read=sys.stdin.readline



class segtree:

    def __init__(self,n,op,e):

        self.e=e

        self.op=op

        self.size=1<<(n-1).bit_length()

        self.SEG=[self.e]*(self.size*2)

    

    def update(self,i,x):

        i+=self.size

        self.SEG[i]=x

        

        while i>0:

            i>>=1

            self.SEG[i]=self.op(self.SEG[i*2],self.SEG[i*2+1])

    

    def get(self,i):

        return self.SEG[i+self.size]

    

    def query(self,l,r):

        l+=self.size

        r+=self.size

        

        lres,rres=self.e,self.e

        

        while l<r:

            if l&1:

                lres=self.op(lres,self.SEG[l])

                l+=1

            

            if r&1:

                r-=1

                rres=self.op(rres,self.SEG[r])

            

            l>>=1

            r>>=1

            

        res=self.op(lres,rres)

        return res



INF=float('inf')

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



Q=[]

for i in range(M):

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

    Q.append((l-1,r-1,c))

Q.sort(key=lambda x:x[0])



seg=segtree(N,min,INF)

seg.update(0,0)

for L,R,C in Q:

    res=seg.query(L,R)+C

    if seg.get(R)>res:

        seg.update(R,res)

ans=seg.get(N-1)

print((ans if ans!=INF else -1))
import sys

from heapq import heappush,heappop,heapify

read=sys.stdin.readline

INF=10**30



def dijkstra(G,s,n):

    que=[(0,s)]

    dist=[INF]*n

    dist[s]=0

    while que:

        mincost,u=heappop(que)

        if(mincost>dist[u]):

            continue

        for c,v in G[u]:

            if(dist[u]+c<dist[v]):

                dist[v]=dist[u]+c

                heappush(que,(dist[v],v))

    return dist



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



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

for i in range(N-1):

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

    

for i in range(M):

    L,R,C=list(map(int,read().split()))

    L-=1;R-=1

    G[L].append((C,R))

    

dist=dijkstra(G,0,N)

ans=dist[N-1]

print((ans if ans!=INF else -1))
import sys

read = sys.stdin.readline


class segtree:
    def __init__(self, n, op, e):
        self.e = e
        self.op = op
        self.size = 1 << (n - 1).bit_length()
        self.SEG = [self.e] * (self.size * 2)

    def update(self, i, x):
        i += self.size
        self.SEG[i] = x
        while i > 0:
            i >>= 1
            self.SEG[i] = self.op(self.SEG[i * 2], self.SEG[i * 2 + 1])

    def get(self, i):
        return self.SEG[i + self.size]

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.e, self.e
        while l < r:
            if l & 1:
                lres = self.op(lres, self.SEG[l])
                l += 1
            if r & 1:
                r -= 1
                rres = self.op(rres, self.SEG[r])
            l >>= 1
            r >>= 1
        res = self.op(lres, rres)
        return res


INF = float("inf")
N, M = list(map(int, read().split()))
Q = []
for i in range(M):
    l, r, c = list(map(int, read().split()))
    Q.append((l - 1, r - 1, c))
Q.sort(key=lambda x: x[0])
seg = segtree(N, min, INF)
seg.update(0, 0)
for L, R, C in Q:
    res = seg.query(L, R) + C
    if seg.get(R) > res:
        seg.update(R, res)
ans = seg.get(N - 1)
print((ans if ans != INF else -1))

import sys
from heapq import heappush, heappop, heapify

read = sys.stdin.readline
INF = 10**30


def dijkstra(G, s, n):
    que = [(0, s)]
    dist = [INF] * n
    dist[s] = 0
    while que:
        mincost, u = heappop(que)
        if mincost > dist[u]:
            continue
        for c, v in G[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                heappush(que, (dist[v], v))
    return dist


N, M = list(map(int, read().split()))
G = [[] for _ in range(N)]
for i in range(N - 1):
    G[i + 1].append((0, i))
for i in range(M):
    L, R, C = list(map(int, read().split()))
    L -= 1
    R -= 1
    G[L].append((C, R))
dist = dijkstra(G, 0, N)
ans = dist[N - 1]
print((ans if ans != INF else -1))

+from heapq import heappush, heappop, heapify +INF = 10**30 -class segtree: -    def __init__(self, n, op, e): -        self.e = e -        self.op = op -        self.size = 1 << (n - 1).bit_length() -        self.SEG = [self.e] * (self.size * 2) - -    def update(self, i, x): -        i += self.size -        self.SEG[i] = x -        while i > 0: -            i >>= 1 -            self.SEG[i] = self.op(self.SEG[i * 2], self.SEG[i * 2 + 1]) - -    def get(self, i): -        return self.SEG[i + self.size] - -    def query(self, l, r): -        l += self.size -        r += self.size -        lres, rres = self.e, self.e -        while l < r: -            if l & 1: -                lres = self.op(lres, self.SEG[l]) -                l += 1 -            if r & 1: -                r -= 1 -                rres = self.op(rres, self.SEG[r]) -            l >>= 1 -            r >>= 1 -        res = self.op(lres, rres) -        return res +def dijkstra(G, s, n): +    que = [(0, s)] +    dist = [INF] * n +    dist[s] = 0 +    while que: +        mincost, u = heappop(que) +        if mincost > dist[u]: +            continue +        for c, v in G[u]: +            if dist[u] + c < dist[v]: +                dist[v] = dist[u] + c +                heappush(que, (dist[v], v)) +    return dist -INF = float("inf") -Q = [] +G = [[] for _ in range(N)] +for i in range(N - 1): +    G[i + 1].append((0, i)) -    l, r, c = list(map(int, read().split())) -    Q.append((l - 1, r - 1, c)) -Q.sort(key=lambda x: x[0]) -seg = segtree(N, min, INF) -seg.update(0, 0) -for L, R, C in Q: -    res = seg.query(L, R) + C -    if seg.get(R) > res: -        seg.update(R, res) -ans = seg.get(N - 1) +    L, R, C = list(map(int, read().split())) +    L -= 1 +    R -= 1 +    G[L].append((C, R)) +dist = dijkstra(G, 0, N) +ans = dist[N - 1]