from heapq import *


def Dijkstra(G: list, source=0):
	INF = float("inf")
	V = len(G)
	D = [INF] * V
	vis = [False] * V
	H = [(0, source)]
	while H:
		dist, cur = heappop(H)
		if vis[cur]:
			continue
		vis[cur] = True
		for nxt, cost in G[cur]:
			t = dist + cost
			if D[nxt] > t:
				D[nxt] = t
				heappush(H, (t, nxt))
	return D[-1] if D[-1]!=INF else -1


N, M, *L = map(int, open(0).read().split())
G = [[(i - 1, 0)] for i in range(N)]
G[0] = []
for l, r, c in zip(*[iter(L)] * 3):
	G[l - 1].append((r - 1, c))
print(Dijkstra(G))
