#非再帰minセグ木

class MinSegmentTree:
    def __init__(self, n): #a1, a2, ..., an
        n_ = 1
        while n_ < n:
            n_ *= 2

        self.n = n_
        self.arr = [float("inf")] * (2*self.n-1)

    def update(self, k, a): #0_indexed
        k += self.n - 1
        self.arr[k] = a

        while k:
            k = (k-1) // 2
            x = self.arr[2*k+1]; y = self.arr[2*k+2]
            self.arr[k] = x if x<y else y

    def query(self, l, r):#[l,r)の値を返す
        L, R = l+self.n, r+self.n
        res = float ("inf")

        while L < R:
            if R & 1:
                R -= 1
                y = self.arr[R-1]
                if res > y:
                    res = y

            if L & 1:
                y = self.arr[L-1]
                if res > y:
                    res = y
                L += 1

            L >>= 1
            R >>= 1

        return res

import sys
input=sys.stdin.readline
n,m=map(int,input().split())

sg=MinSegmentTree(n+1)
sg.update(1,0)
lrc=[tuple(map(int,input().split())) for _ in range(m)]
lrc.sort(key=lambda x:x[0])
for i in range(m):
    l,r,c=lrc[i]
    x=sg.query(l,l+1)
    if x!=float("inf"):
        x+=c
        for j in range(l,r+1):
            sg.update(j,min(x,sg.query(j,j+1)))
ans=sg.query(n,n+1)
print(ans if ans!=float("inf") else -1)
