import queue
class Dijkstra:
    def __init__(self,n):
        self.n = n
        self.lengths = [dict() for _ in range(n)]
        self.inf = float('inf')
    def register(self,v1,v2,l,multiple=True):
        self.lengths[v1][v2] = l
        if multiple:
            self.lengths[v2][v1] = l
    def solve(self,s1):
        d = [self.inf for _ in range(self.n)]
        d[s1] = 0
        Q = queue.PriorityQueue()
        Q.put((d[s1],s1))
        while not Q.empty():
            d1,v1 = Q.get()
            for v2,l in self.lengths[v1].items():
                val = d1 + l
                if val < d[v2]:
                    d[v2] = val
                    Q.put((val,v2))
        return d
N,M = map(int,input().split())
LRC = []
for _ in range(M):
    LRC.append(list(map(int,input().split())))
D = Dijkstra(N)
for l,r,c in LRC:
    D.register(l-1,r-1,c,False)
for i in range(N-1):
    D.register(i+1,i,0,False)
ans = D.solve(0)[-1]
if ans == float('inf'):
    print(-1)
else:
    print(ans)