N,M=map(int,input().split())
A=[float('inf')]*N
idx = [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:
	s,g,c=t[1],t[0],t[2]
	
	mv = float('inf')
	mi = -1
	for i in range(s,g)[::-1]:
		if mi!=-1 and idx[mi]<=i:
			continue
		if A[i]!=float('inf'):
			mv = min(A[i],mv)
			mi = idx[i]

	if mv!=float('inf'):
		A[g] = min(A[g], mv + c)
		idx[g] = s+1

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