# -*- coding: utf-8 -*-
N, M = map(int, input().split())
NN = [[0] * N for _ in range(N)]
for i in range(N):
    for j in range(i+1,N):
        NN[i][j] = float("inf")
for i in range(M):
    l, r, c = map(int, input().split())
    NN[l-1][r-1] = c
    
def warshall_floyd(d):
    for k in range(N):
        for i in range(N):
            for j in range(N):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

d = warshall_floyd(NN)

if d[0][N-1] == float("inf"):
    print(-1)
else:
    print(int(d[0][N-1]))