import heapq
import bisect
n, m = map(int, input().split())
li = [list(map(int, reversed(input().split()))) for _ in range(m)]
li.sort(key=lambda x: x[2])
a = [x[2] for x in li]
idx = 0
t = 1
hq = []
d = [0] * n
while t < n:
    idxx = bisect.bisect_right(a, t)
    for i in range(idx, idxx):
        heapq.heappush(hq, li[i])
    idx = idxx
    if not hq:
        print(-1)
        exit(0)
    c, r, l = heapq.heappop(hq)
    for i in range(t, r):
        d[i] = d[l - 1] + c
    t = max(t, r)
print(d[n - 1])