import sys
#input = sys.stdin.readline
sys.setrecursionlimit(10**7)
N, M = map(int, input().split())
LRC = [list(map(int, input().split())) for _ in range(M)]

adjm = [[float('inf')] * N for _ in range(N)]

for lrc in LRC:
    l = lrc[0]-1
    r = lrc[1]-1
    c = lrc[2]

    for i in range(l, r):
        for j in range(i+1, r+1):
            if adjm[i][j] > c:
                adjm[i][j] = c
                adjm[j][i] = 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])

warshall_floyd(adjm)

res = adjm[0][N-1]
if res == float('inf'):
    print(-1)
else:
    print(res)