import numpy as np

N,M=map(int,input().split())
A=np.array([float('inf')]*N)

T=[]
for i in range(M):
	a,b,c=map(int,input().split())
	a-=1
	b-=1
	T.append([b,a,c])
T.sort()

A[0]=0
for t in T:
	k = A[t[1]:t[0]].min()
	if k!=float('inf'):
		A[t[0]] = min(A[t[0]], k + t[2])

if A[-1]!=float('inf'):
	print(int(A[-1]))
else:
	print(-1)
