import sys
input = sys.stdin.readline

class RangeUpdateQuery:
    def __init__(self, n):
        self.n0 = 2**(n-1).bit_length()
        self.INF = 10 ** 18
        self.data = [self.INF]*(2*self.n0-1)

    def update(self, l,r,v):
        l += self.n0
        r += self.n0
        while l < r:
            if r&1:
                r -= 1
                if self.data[r-1] > v:
                    self.data[r-1] = v
            if l&1:
                if self.data[l-1] > v:
                    self.data[l-1] = v
                l += 1
            l >>=1
            r >>=1

    def query(self, i):
        i += self.n0-1
        res = self.INF
        while i+1:
            if self.data[i] < res:
                res = self.data[i]
            i = ~-i//2
        return res

n,m = map(int, input().split())
light = [list(map(int, input().split())) for i in range(m)]
light.sort()
RUQ = RangeUpdateQuery(n-1)
RUQ.update(0,1,0)
for a,b,c in light:
    cur = RUQ.query(a-1)
    RUQ.update(a, b, cur+c)

ans = RUQ.query(n-1)
if ans >= 10 ** 18:
  print(-1)
else:
  print(ans)
