import heapq

N, M = map(int, input().split())

items = []
for _ in range(M):
	l, r, c = map(int, input().split())
	items.append([l, r, c])

items.sort()
def check():
	queue = []
	index = 0
	data = [None]
	for i in range(1, N):
		while index < M:
			l, r, c = items[index]
			if l <= i:
				heapq.heappush(queue, (c, r))
				index += 1
			else:
				break
		while True:
			if len(queue) == 0:
				return None
			c, r = queue[0]
			if r > i:
				break
			heapq.heappop(queue)
		data.append((c, r))
	return data

data = check()
if data != None:
	i = 1
	count = 0
	while i < N:
		c, r = data[i]
		count += c
		i = r
	if i == N:
		print(count)
	else:
		print(-1)
else:
	print(-1)
