import sys
import bisect

input = sys.stdin.readline

N, M = list(map(int,input().split()))
LRC = [list(map(int,input().split())) for i in range(M)]
LRC.sort()

INF = 100100100100100100
dist = [INF]*N
dist[0] = 0

for li, ri, ci in LRC:
	td = dist[li-1]+ci
	ti = bisect.bisect(dist,dist[li-1]+ci)
	ti = min(ti,ri+1)
	for i in range(ti,ri):
		dist[i] = td

ans = dist[-1]
if ans==INF: ans = -1

print(ans)