from collections import deque
N, M = map(int,input().split())
side = [[[i-1,0]]for i in range(N)]
side[0] = []
for i in range(M):
    l,r,c = map(int,input().split())
    side[l-1].append([r-1,c])
node = [float('inf')]*N
node[0] = 0
queue = deque([0])
while queue:
    q = queue.popleft()
    for n, c in side[q]:
        if node[n] > node[q] + c:
            node[n] = node[q]+c
            queue.append(n)
if node[-1] == float('inf'):
    print(-1)
else:
    print(node[-1])
