from scipy.sparse.csgraph import dijkstra, shortest_path, csgraph_from_dense
import numpy as np
 
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
 
 
n, m = map(int,readline().split())
 
inf = float("inf")
 
g = np.full((n, n), inf)
 
for i in range(m):
    l, r, c = map(int,readline().split())
    g[l-1, r-1] = np.minimum(g[l-1, r-1], c)
    #g[r-1, l-1:r-1] = 0
    for i in range(r-l):
        g[r-1-i, r-2-i] = 0
g = csgraph_from_dense(g, null_value=inf)
 
ans = dijkstra(g, indices = 0)[n-1]
if ans > 10**14:
	print(-1)
else:
	print(int(ans))