from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra
import numpy as np

N, M = map(int, input().split())
edges = []
for _ in range(M):
    l, r, c = map(int, input().split())
    edges.append((l - 1, r - 1, c))
for i in range(N - 1):
    edges.append((i + 1, i, 0))

E = np.asarray(edges)
fr, to, w = E[:, 0], E[:, 1], E[:, 2]
graph = csr_matrix((w, (fr, to)), shape=(N, N))
dists = dijkstra(graph, indices=0)

if np.isinf(dists[-1]):
    print(-1)
else:
    print(int(dists[-1]))