import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

input=lambda :sys.stdin.readline().rstrip()

def resolve():

    n,m=list(map(int,input().split()))

    E=[[] for _ in range(n)]

    for i in range(1,n):

        E[i].append((i-1,0))

    for _ in range(m):

        l,r,c=list(map(int,input().split()))

        l-=1; r-=1

        E[l].append((r,c))



    # Dijkstra with heap

    from heapq import heappop,heappush

    Q=[(0,0)]

    dist=[INF]*n

    dist[0]=0

    while(Q):

        d,v=heappop(Q)

        if(dist[v]<d): continue

        for nv,w in E[v]:

            if(dist[nv]>dist[v]+w):

                dist[nv]=dist[v]+w

                heappush(Q,(dist[nv],nv))

    ans=dist[-1]

    if(ans==INF): ans=-1

    print(ans)

resolve()
import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

input = lambda:sys.stdin.readline().rstrip()



class SegmentTree(object):

    def __init__(self, A, dot, unit):

        n = 1 << (len(A) - 1).bit_length()

        tree = [unit] * (2 * n)

        for i, v in enumerate(A):

            tree[i + n] = v

        for i in range(n - 1, 0, -1):

            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])

        self._n = n

        self._tree = tree

        self._dot = dot

        self._unit = unit



    def __getitem__(self, i):

        return self._tree[i + self._n]



    def update(self, i, v):

        i += self._n

        self._tree[i] = v

        while i != 1:

            i >>= 1

            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])



    def add(self, i, v):

        self.update(i, self[i] + v)



    def sum(self, l, r):

        l += self._n

        r += self._n

        l_val = r_val = self._unit

        while l < r:

            if l & 1:

                l_val = self._dot(l_val, self._tree[l])

                l += 1

            if r & 1:

                r -= 1

                r_val = self._dot(self._tree[r], r_val)

            l >>= 1

            r >>= 1

        return self._dot(l_val, r_val)



def resolve():

    n, m = list(map(int, input().split()))

    edges = []

    for _ in range(m):

        u, v, w = list(map(int, input().split()))

        u -= 1; v -= 1

        edges.append((u, v, w))

    edges.sort()



    tree = SegmentTree([INF] * n, min, INF)

    tree.update(0, 0)

    for u, v, w in edges:

        tree.update(v, min(tree[v], tree.sum(u, v) + w))



    ans = tree[n - 1]

    if ans == INF:

        ans = -1

    print(ans)

resolve()
import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n, m = list(map(int, input().split()))
    E = [[] for _ in range(n)]
    for i in range(1, n):
        E[i].append((i - 1, 0))
    for _ in range(m):
        l, r, c = list(map(int, input().split()))
        l -= 1
        r -= 1
        E[l].append((r, c))
    # Dijkstra with heap
    from heapq import heappop, heappush

    Q = [(0, 0)]
    dist = [INF] * n
    dist[0] = 0
    while Q:
        d, v = heappop(Q)
        if dist[v] < d:
            continue
        for nv, w in E[v]:
            if dist[nv] > dist[v] + w:
                dist[nv] = dist[v] + w
                heappush(Q, (dist[nv], nv))
    ans = dist[-1]
    if ans == INF:
        ans = -1
    print(ans)


resolve()

import sys

INF = 1 << 60
MOD = 10**9 + 7  # 998244353
sys.setrecursionlimit(2147483647)
input = lambda: sys.stdin.readline().rstrip()


class SegmentTree(object):
    def __init__(self, A, dot, unit):
        n = 1 << (len(A) - 1).bit_length()
        tree = [unit] * (2 * n)
        for i, v in enumerate(A):
            tree[i + n] = v
        for i in range(n - 1, 0, -1):
            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])
        self._n = n
        self._tree = tree
        self._dot = dot
        self._unit = unit

    def __getitem__(self, i):
        return self._tree[i + self._n]

    def update(self, i, v):
        i += self._n
        self._tree[i] = v
        while i != 1:
            i >>= 1
            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])

    def add(self, i, v):
        self.update(i, self[i] + v)

    def sum(self, l, r):
        l += self._n
        r += self._n
        l_val = r_val = self._unit
        while l < r:
            if l & 1:
                l_val = self._dot(l_val, self._tree[l])
                l += 1
            if r & 1:
                r -= 1
                r_val = self._dot(self._tree[r], r_val)
            l >>= 1
            r >>= 1
        return self._dot(l_val, r_val)


def resolve():
    n, m = list(map(int, input().split()))
    edges = []
    for _ in range(m):
        u, v, w = list(map(int, input().split()))
        u -= 1
        v -= 1
        edges.append((u, v, w))
    edges.sort()
    tree = SegmentTree([INF] * n, min, INF)
    tree.update(0, 0)
    for u, v, w in edges:
        tree.update(v, min(tree[v], tree.sum(u, v) + w))
    ans = tree[n - 1]
    if ans == INF:
        ans = -1
    print(ans)


resolve()

+INF = 1 << 60 +MOD = 10**9 + 7  # 998244353 -INF = float("inf") -MOD = 10**9 + 7 + + +class SegmentTree(object): +    def __init__(self, A, dot, unit): +        n = 1 << (len(A) - 1).bit_length() +        tree = [unit] * (2 * n) +        for i, v in enumerate(A): +            tree[i + n] = v +        for i in range(n - 1, 0, -1): +            tree[i] = dot(tree[i << 1], tree[i << 1 | 1]) +        self._n = n +        self._tree = tree +        self._dot = dot +        self._unit = unit + +    def __getitem__(self, i): +        return self._tree[i + self._n] + +    def update(self, i, v): +        i += self._n +        self._tree[i] = v +        while i != 1: +            i >>= 1 +            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1]) + +    def add(self, i, v): +        self.update(i, self[i] + v) + +    def sum(self, l, r): +        l += self._n +        r += self._n +        l_val = r_val = self._unit +        while l < r: +            if l & 1: +                l_val = self._dot(l_val, self._tree[l]) +                l += 1 +            if r & 1: +                r -= 1 +                r_val = self._dot(self._tree[r], r_val) +            l >>= 1 +            r >>= 1 +        return self._dot(l_val, r_val) -    E = [[] for _ in range(n)] -    for i in range(1, n): -        E[i].append((i - 1, 0)) +    edges = [] -        l, r, c = list(map(int, input().split())) -        l -= 1 -        r -= 1 -        E[l].append((r, c)) -    # Dijkstra with heap -    from heapq import heappop, heappush - -    Q = [(0, 0)] -    dist = [INF] * n -    dist[0] = 0 -    while Q: -        d, v = heappop(Q) -        if dist[v] < d: -            continue -        for nv, w in E[v]: -            if dist[nv] > dist[v] + w: -                dist[nv] = dist[v] + w -                heappush(Q, (dist[nv], nv)) -    ans = dist[-1] +        u, v, w = list(map(int, input().split())) +        u -= 1 +        v -= 1 +        edges.append((u, v, w)) +    edges.sort() +    tree = SegmentTree([INF] * n, min, INF) +    tree.update(0, 0) +    for u, v, w in edges: +        tree.update(v, min(tree[v], tree.sum(u, v) + w)) +    ans = tree[n - 1]