import numpy as np
from scipy.sparse.csgraph import shortest_path
N, M = map(int, input().split())
g = np.full((N, N), float('INF'))
for i in range(M):
    l, r, c = map(int, input().split())
    g[l-1: r, l-1:r] = np.where(g[l-1: r, l-1:r] > c, c, g[l-1: r, l-1:r])
    # print(g)
g = np.where(g == float('INF'), 0, g)
# print(g, shortest_path(g))
ans = shortest_path(g)[0][-1]
if ans == float('INF'):
    print(-1)
else:
    print(int(ans))
