import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,m = map(int,ipt().split())
    way = [[] for i in range(n+1)]
    for _ in range(m):
        l,r,c = map(int,ipt().split())
        way[l].append((r,c))
    for i in range(1,n):
        way[i+1].append((i,0))
    hq = [(0,1)]
    dst = [10**18]*(n+1)
    dst[1] = 0
    while hq:
        di,qi = hpq.heappop(hq)
        for j,cj in way[qi]:
            if dst[j] > di+cj:
                dst[j] = di+cj
                hpq.heappush(hq,(di+cj,j))
    print((dst[n]+1)%(10**18+1)-1)
    
    return None

if __name__ == '__main__':
    main()
