import sys
def input():
	return sys.stdin.readline()[:-1]

INF = 10**30

class Rmin():
	def __init__(self, size):
		#the number of nodes is 2n-1
		self.n = 1
		while self.n < size:
			self.n *= 2
		self.node = [INF] * (2*self.n-1)

	def Access(self, x):
		return self.node[x+self.n-1]

	def Update(self, x, val):
		x += self.n-1
		self.node[x] = min(val, self.node[x])
		while x > 0:
			x = (x-1)//2
			self.node[x] = min(self.node[2*x+1], self.node[2*x+2])
		return

	#[l, r)
	def Get(self, l, r):
		L, R = l+self.n, r+self.n
		s = INF
		while L<R:
			if R & 1:
				R -= 1
				s = min(s, self.node[R-1])
			if L & 1:
				s = min(s, self.node[L-1])
				L += 1
			L >>= 1
			R >>= 1
		return s

n, m = map(int, input().split())
seg = []
for _ in range(m):
	l, r, c = map(int, input().split())
	seg.append([l-1, r-1, c])

seg.sort(reverse=True)
ans = [INF for _ in range(n)]
ans[0] = 0
tree = Rmin(n)

for i in range(1, n):
	while seg and seg[-1][0] == i-1:
		l, r, c = seg.pop()
		tree.Update(r, c + ans[l])
	ans[i] = tree.Get(i, n)

if ans[n-1] > 10**10:
	print(-1)
else:
	print(ans[n-1])