#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

N,M = map(int, sys.stdin.readline().rstrip().split(" "))
pathes = collections.defaultdict(list)
for _ in range(M):
    L, R, C = map(int, sys.stdin.readline().rstrip().split(" "))
    pathes[L].append((R, C))

currents = {}
if not 1 in pathes:
    print(-1)
    exit(0)
current_minimum = 0
for n in range(1, N, 1):
    if n in pathes:
        for R, C in pathes[n]:
            if R in currents:
                currents[R] = min(currents[R], C+current_minimum)
            else:
                currents[R] = C+current_minimum
    if n == 1:
        current_minimum = min(currents.values())
    if n in currents:
        del currents[n]
        if len(currents) == 0:
            print(-1)
            exit(0)
        current_minimum = min(currents.values())
    # print(n, current_minimum, currents)


print(current_minimum)
exit(0)
