import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
from functools import lru_cache
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, M = read_values()
    T = read_lists(M)
    T.sort()

    S = []
    G = set()

    t = 0
    i = 0
    res = 0
    while i < N - 1:
        while t < M and T[t][0] - 1 <= i:
            heapq.heappush(S, (T[t][2], T[t][1] - 1))
            G.add(t)
            t += 1
        
        while S:
            if S[0][1] > i:
                break
            heapq.heappop(S)

        if len(S) == 0:
            print(-1)
            return
        
        res += S[0][0]
        i = S[0][1]

    print(res)


if __name__ == "__main__":
    main()

