#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
from time import* 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline
input=sys.stdin.readline
t=time()

def main():
    n,m=map(int,input().split())
    x=[tuple(map(int,input().split()))for i in range(m)]
    y=sorted(x)
    start=set()
    for s,t,r in y:
        if s==1:
            start.add((1,t,r))
    ans=set([-1])
    substart=set()
    while 1:
        while start:
            qp,p,q=start.pop()
            for s,t,r in y:
                if t==n:
                    ans.add(q)
                elif s<=p and p<=t and s!=qp:
                    substart.add((p,t,q+r))
                    
        start=substart
        if time()-t>1.9:
            if len(ans)!=1:
                print(min(i for i in ans if i!=-1))
            else:
                print(-1)
            exit()
if __name__ == '__main__':
    main()