### BUGGY CODE ###

from heapq import heappop, heappush, heapify
from collections import defaultdict
import sys
from copy import copy
sys.setrecursionlimit(10**9)
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))
class SegmentTree:
    def __init__(self, value=[], N=0, comp=lambda x,y: x<=y, reverse=False):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [0] * (2*N-1)
        for i in range(N):
            self.node[N-1+i] = i
        self.value = [None] * N
        for i, v in enumerate(value):
            self.value[i] = v
        self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y)^reverse
        for i in range(N-2,-1,-1):
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.value[left_i], self.value[right_i]
            self.node[i] = left_i if self.comp(left_v, right_v) else right_i

    def update(self, n, v):
        self.value[n] = v
        i = (self.N-1) + n
        while i > 0:
            i = (i-1)//2
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.value[left_i], self.value[right_i]
            new_i = left_i if self.comp(left_v, right_v) else right_i
            if new_i == self.node[i] and new_i != n:
                break
            else:
                self.node[i] = new_i

    def at(self, n):
        if n is None:
            return None
        else:
            return self.value[n]

    def query(self, l=0, r=-1):
        return self.at(self.query_index(l,r))

    def query_index(self, l=0, r=-1):
        if r < 0: r = self.N
        L = l + self.N; R = r + self.N
        s = None
        while L < R:
            if R & 1:
                R -= 1
                if self.comp(self.at(self.node[R-1]), self.at(s)):
                    s = self.node[R-1]
            if L & 1:
                if self.comp(self.at(self.node[L-1]), self.at(s)):
                    s = self.node[L-1]
                L += 1
            L >>= 1; R >>= 1
        return s

N, M = inpl()
q = []
LRC = [inpl() for _ in range(M)]
LRC.sort(key=lambda x: x[2])
LRs = []
for m in range(M):
    L, R, C = LRC[m]
    LRs.append((L,0,m))
    LRs.append((R,1,m))
LRs.sort()
if LRs[0][0] > 1 or LRs[-1][0] < N:
    print(-1)
    exit()
prev = -1
points = []
starts = []
ends = []
positions = dict()
for p, s, m in LRs:
    if p > prev:
        prev = p
        positions[p] = len(points)
        points.append(p)
        if s == 0:
            starts.append({m})
            ends.append(set())
        else:
            starts.append(set())
            ends.append({m})
    else:
        if s == 0:
            starts[-1].add(m)
        else:
            ends[-1].add(m)

P = len(points)
neighbors = []
cur_neighbors = set()
for i in range(P):
    cur_neighbors |= starts[i]
    neighbors.append(copy(cur_neighbors))
    cur_neighbors -= ends[i]

sg = SegmentTree(value=[0], N=P)
alive = [True]*P
while True:
    s = sg.query_index()
    d = sg.at(s)
    if d == None:
        print(-1)
        break
    elif s == P-1:
        print(d)
        break
    else:
        sg.update(s, None)
        alive[s] = False
        for m in neighbors[s]:
            c = LRC[m][2]
            new = d+c
            for i in range(positions[LRC[m][0]],positions[LRC[m][1]]+1):
                if alive[i] and ((sg.at(i) is None) or (sg.at(i) > new)):
                    sg.update(i, new)


### FIXED CODE ###

from heapq import heappop, heappush, heapify
import sys
sys.setrecursionlimit(10**9)
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
q = []
LRC = sorted([inpl() for _ in range(M)])
points = sorted(list({lrc[i] for i in range(2) for lrc in LRC}))
if points[0] != 1 or points[-1] != N:
    print(-1)
    exit()
positions = dict()
P = 0
prev = -1
for p in points:
    if p > prev:
        prev = p
        positions[p] = P
        P += 1
m = 0
dq = [(0,0)]
for i in range(P):
    while dq and dq[0][1] < i:
        heappop(dq)
    if not dq:
        print(-1)
        break
    d = dq[0][0]
    while m < M and positions[LRC[m][0]] == i:
        heappush(dq, (d+LRC[m][2], positions[LRC[m][1]]))
        m += 1
else:
    print(d)