N, M = map(int,input().split())
G = [[10 ** 9 for _ in range(N)] for _ in range(N)]
for i in range(M):
  l,r,c = map(int,input().split())
  G[l-1][r-1] = c
for i in range(1,N):
  G[i][i-1] = 0
#print(G)
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra
G = csgraph_from_dense(G, null_value=10**9)
dist_matrix = dijkstra(G,indices = 0)
print(-1 if dist_matrix[-1] == float("inf") else int(dist_matrix[-1]))