import sys

n, m = list(map(int, input().split()))
methods = []
for line in sys.stdin:
    l, r, c = map(int, line.split())
    methods.append((c, l - 1, r - 1))
methods.sort()

INF = 10 ** 18
jump = [-1] * n
cost = [0] * n

for c, l, r in methods:
    i = l
    while i < r:
        if jump[i] == -1:
            jump[i] = r
            cost[i] = c
            i += 1
            continue
        i = jump[i]

i = 0
ans = 0
n1 = n - 1
while i < n1:
    if jump[i] == -1:
        print(-1)
        exit()
    ans += cost[i]
    i = jump[i]

print(ans)
