from sys import stdin
import heapq

N, M = [int(x) for x in stdin.readline().rstrip().split()]
B = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]

graph = [[10 ** 15] * N for _ in range(N)]
for i in range(1, N):
    graph[i][i - 1] = 0
for m in B:
    graph[m[0] - 1][m[1] - 1] = m[2]

d = [(0, 0)]
distance = [10 ** 15] * N
distance[0] = 0
color = ['white'] * N
heapq.heapify(d)

while len(d) > 0:
    temp, u = heapq.heappop(d)
    color[u] = 'black'
    if distance[u] < temp:
        continue
    for v in range(N):
        if color[v] != 'black':
            if distance[u] + graph[u][v] < distance[v]:
                distance[v] = distance[u] + graph[u][v]
                color[v] = 'gray'
                heapq.heappush(d, (distance[v], v))

if distance[N - 1] == 10 ** 15:
    print(-1)
else:
    print(distance[N - 1])