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

N, M = map(int, input().split())
a = np.full((N, N), 10**15)
for _ in range(M):
    L, R, C = map(int, input().split())
    a[L - 1:R, L - 1:R] = np.minimum(a[L - 1:R, L - 1:R], C)

indices_diagonal = np.diag_indices(N)
a[indices_diagonal] = 0
d = dijkstra(a)
if d[0, N - 1] > 10**14:
    print(-1)
else:
    print(int(d[0, N - 1]))