import sys
import heapq
input = sys.stdin.readline
heapify, push, pop = heapq.heapify, heapq.heappush, heapq.heappop

_n, _m = map(int, input().split())
_lrc = []
for _ in range(_m):
    l, r, c = map(int, input().split())
    _lrc.append((l - 1, r - 1, c))

_lrc.sort()


def chker(n, m, lrc):
    dst = [float('inf')] * n
    dst[0] = 0
    q = [(0, 0)]

    cnt = 0
    for i in range(n):
        while q:
            d, r = q[0]
            if i <= r:
                break
            pop(q)
        else:
            print(-1)
            return

        dst[i] = d
        while cnt < m and lrc[cnt][0] == i:
            l, r, c = lrc[cnt]
            push(q, (d + c, r))
            cnt += 1

    print(dst[-1])


chker(_n, _m, _lrc)
