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

N, M = map(int, input().split())

# (L,R)の組が重複？ 除外する
edge_d_dict = dict()
for i in range(M):
    L, R, C = map(int, input().split())
    if (L, R) in edge_d_dict:
        edge_d_dict[(L, R)] = min(edge_d_dict[(L, R)], C)
    else:
        edge_d_dict[(L, R)] = C
edge_d = np.array([(L, R, C) for (L, R), C in edge_d_dict.items()], dtype=np.int64)

edge_n = np.vstack([np.arange(2, N + 1, dtype=np.int64),
                    np.arange(1, N, dtype=np.int64),
                    np.zeros(N - 1, dtype=np.int64)]).T
edge = np.concatenate([edge_d, edge_d[:, [1, 0, 2]], edge_n]).T

matr = csr_matrix((edge[2], (edge[:2] - 1)), (N, N))
ans = dijkstra(matr, indices=0)[-1]
if ans == np.inf:
    ans = -1
print(int(ans))
