# -*- coding: utf-8 -*-
from scipy.sparse.csgraph import floyd_warshall

N, M = map(int, input().split())
NN = [[0] * N for _ in range(N)]
for i in range(M):
    l, r, c = map(int, input().split())
    for j in range(l,r):
        for k in range(l+1,r+1):
            if NN[j-1][k-1] >= c or NN[j-1][k-1] == 0:
                NN[j-1][k-1] = c
                NN[k-1][j-1] = c
  
d = floyd_warshall(NN, directed = False)

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