from scipy.sparse.csgraph import csgraph_from_dense,dijkstra
import sys

input = sys.stdin.readline

n, m = map(int, input().split())
edges = [[float('INF')]*n for _ in range(n)]

for _ in range(m):
    _from, to, dist = map(int, input().split())
    edges[_from-1][to-1] = dist

for i in range(1, n):
    edges[i][i-1] = 0

G = csgraph_from_dense(edges, null_value=float('INF'))
D = dijkstra(G, directed=True, indices=0)
ans = D[-1]
if ans == float('INF'):
    ans = -1
print(int(ans))