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

INF = float('inf')

def solve():
    N, M = map(int, input().split())
    Es = []
    for _ in range(M):
        L, R, C = map(int, input().split())
        L, R = L-1, R-1
        Es.append((L, R, C))

    Es.sort()

    PQ = [(0, 0)]

    dists = [INF] * N
    iE = 0
    for tm in range(N):
        while PQ:
            dist, R = PQ[0]
            if tm <= R:
                break
            heappop(PQ)
        else:
            return -1

        dists[tm] = dist
        while iE < M and Es[iE][0] == tm:
            L, R, C = Es[iE]
            heappush(PQ, (dist+C, R))
            iE += 1

    return dists[-1]


print(solve())
