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:
            i = jump[i]
            continue
        jump[i] = r
        cost[i] = c
        i += 1

# print(jump)
# print(cost)
jump[-1] = n - 1

i = 0
r = jump[i]
ans = 0
n1 = n - 1
while i < n1:
    # print('i r', i, r)
    max_k, max_j = -1, -1
    for k in range(i + 1, r + 1):
        if jump[k] >= max_j:
            max_j = jump[k]
            max_k = k
    if max_k == -1:
        ans = -1
        break
    # print('k j', max_k, max_j)
    ans += cost[i]
    i = max_k
    r = max_j

print(ans)
