class SegTreeMin:

    """

    以下のクエリを処理する

    1.update:  i番目の値をxに更新する

    2.get_min: 区間[l, r)の最小値を得る

    """



    def __init__(self, n, INF):

        """

        :param n: 要素数

        :param INF: 初期値（入りうる要素より十分に大きな数）

        """

        self.n = n

        # nより大きい2の冪数

        n2 = 1

        while n2 < n:

            n2 <<= 1

        self.n2 = n2

        self.tree = [INF] * (n2 << 1)

        self.INF = INF



    def update(self, i, x):

        """

        i番目の値をxに更新

        :param i: index(0-indexed)

        :param x: update value

        """

        i += self.n2

        self.tree[i] = x

        while i > 1:

            self.tree[i >> 1] = x = min(x, self.tree[i ^ 1])

            i >>= 1



    def get_min(self, a, b):

        """

        [a, b)の最小値を得る

        :param a: index(0-indexed)

        :param b: index(0-indexed)

        """

        result = self.INF



        # (k, l, r)

        # k   : 現在調べている区間のtree内index

        # l, r: kが表す区間の左右端index [l, r)

        q = [(1, 0, self.n2)]



        while q:

            k, l, r = q.pop()



            if a <= l and r <= b:

                result = min(result, self.tree[k])

                continue



            m = (l + r) // 2

            k <<= 1

            if a < m and l < b:

                q.append((k, l, m))

            if a < r and l < m:

                q.append((k + 1, m, r))



        return result



N,M = list(map(int,input().split()))

edges = [list(map(int,input().split())) for i in range(M)]

edges.sort()



ST = SegTreeMin(N+1,10**15)

ST.update(0,0)



for i in range(M):

    min_ = ST.get_min(edges[i][0]-1,edges[i][1]-1)

    if min_+edges[i][2] < ST.get_min(edges[i][1]-1,edges[i][1]):

        ST.update(edges[i][1]-1,min_+edges[i][2])

if ST.get_min(N-1,N)!=10**15:

    print((ST.get_min(N-1,N)))

else:print((-1))
from heapq import heappop, heappush, heapify



class Graph():  #directed

    def __init__(self, n, edge, indexed=1):

        self.n = n

        self.graph = [[] for _ in range(n)]

        self.deg = [0 for _ in range(n)]

        for e in edge:

            self.graph[e[0] - indexed].append((e[1] - indexed, e[2]))

            self.deg[e[1] - indexed] += 1



    def dijkstra(self, s, INF=10**18, restore_to=None):

        dist = [INF for _ in range(self.n)]

        dist[s] = 0

        heap = [(0, s)]

        while heap:

            cost, node = heappop(heap)

            if dist[node] < cost:

                continue

            for adj, adjcost in self.graph[node]:

                if dist[node] + adjcost < dist[adj]:

                    dist[adj] = dist[node] + adjcost

                    heappush(heap, (dist[adj], adj))

        if restore_to is not None:

            g = restore_to

            if dist[g] == INF:

                return dist, False

            path = [g]

            node = g

            while node != s:

                node = prev[node]

                path.append(node)

            return dist, path[::-1]

        return dist



import sys

input = sys.stdin.readline



N, M = list(map(int, input().split()))

E = list()



for _ in range(M):

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

    E.append((l - 1, r - 1, c))



for i in range(N - 1):

    E.append((i + 1, i, 0))



g = Graph(N, E, 0)

res = g.dijkstra(0)[N - 1]



print((res if res != 10**18 else -1))
class SegTreeMin:
    """
    以下のクエリを処理する
    1.update:  i番目の値をxに更新する
    2.get_min: 区間[l, r)の最小値を得る
    """

    def __init__(self, n, INF):
        """
        :param n: 要素数
        :param INF: 初期値（入りうる要素より十分に大きな数）
        """
        self.n = n
        # nより大きい2の冪数
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [INF] * (n2 << 1)
        self.INF = INF

    def update(self, i, x):
        """
        i番目の値をxに更新
        :param i: index(0-indexed)
        :param x: update value
        """
        i += self.n2
        self.tree[i] = x
        while i > 1:
            self.tree[i >> 1] = x = min(x, self.tree[i ^ 1])
            i >>= 1

    def get_min(self, a, b):
        """
        [a, b)の最小値を得る
        :param a: index(0-indexed)
        :param b: index(0-indexed)
        """
        result = self.INF
        # (k, l, r)
        # k   : 現在調べている区間のtree内index
        # l, r: kが表す区間の左右端index [l, r)
        q = [(1, 0, self.n2)]
        while q:
            k, l, r = q.pop()
            if a <= l and r <= b:
                result = min(result, self.tree[k])
                continue
            m = (l + r) // 2
            k <<= 1
            if a < m and l < b:
                q.append((k, l, m))
            if a < r and l < m:
                q.append((k + 1, m, r))
        return result


N, M = list(map(int, input().split()))
edges = [list(map(int, input().split())) for i in range(M)]
edges.sort()
ST = SegTreeMin(N + 1, 10**15)
ST.update(0, 0)
for i in range(M):
    min_ = ST.get_min(edges[i][0] - 1, edges[i][1] - 1)
    if min_ + edges[i][2] < ST.get_min(edges[i][1] - 1, edges[i][1]):
        ST.update(edges[i][1] - 1, min_ + edges[i][2])
if ST.get_min(N - 1, N) != 10**15:
    print((ST.get_min(N - 1, N)))
else:
    print((-1))

from heapq import heappop, heappush, heapify


class Graph:  # directed
    def __init__(self, n, edge, indexed=1):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.deg = [0 for _ in range(n)]
        for e in edge:
            self.graph[e[0] - indexed].append((e[1] - indexed, e[2]))
            self.deg[e[1] - indexed] += 1

    def dijkstra(self, s, INF=10**18, restore_to=None):
        dist = [INF for _ in range(self.n)]
        dist[s] = 0
        heap = [(0, s)]
        while heap:
            cost, node = heappop(heap)
            if dist[node] < cost:
                continue
            for adj, adjcost in self.graph[node]:
                if dist[node] + adjcost < dist[adj]:
                    dist[adj] = dist[node] + adjcost
                    heappush(heap, (dist[adj], adj))
        if restore_to is not None:
            g = restore_to
            if dist[g] == INF:
                return dist, False
            path = [g]
            node = g
            while node != s:
                node = prev[node]
                path.append(node)
            return dist, path[::-1]
        return dist


import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))
E = list()
for _ in range(M):
    l, r, c = list(map(int, input().split()))
    E.append((l - 1, r - 1, c))
for i in range(N - 1):
    E.append((i + 1, i, 0))
g = Graph(N, E, 0)
res = g.dijkstra(0)[N - 1]
print((res if res != 10**18 else -1))

-class SegTreeMin: -    """ -    以下のクエリを処理する -    1.update:  i番目の値をxに更新する -    2.get_min: 区間[l, r)の最小値を得る -    """ - -    def __init__(self, n, INF): -        """ -        :param n: 要素数 -        :param INF: 初期値（入りうる要素より十分に大きな数） -        """ -        self.n = n -        # nより大きい2の冪数 -        n2 = 1 -        while n2 < n: -            n2 <<= 1 -        self.n2 = n2 -        self.tree = [INF] * (n2 << 1) -        self.INF = INF - -    def update(self, i, x): -        """ -        i番目の値をxに更新 -        :param i: index(0-indexed) -        :param x: update value -        """ -        i += self.n2 -        self.tree[i] = x -        while i > 1: -            self.tree[i >> 1] = x = min(x, self.tree[i ^ 1]) -            i >>= 1 - -    def get_min(self, a, b): -        """ -        [a, b)の最小値を得る -        :param a: index(0-indexed) -        :param b: index(0-indexed) -        """ -        result = self.INF -        # (k, l, r) -        # k   : 現在調べている区間のtree内index -        # l, r: kが表す区間の左右端index [l, r) -        q = [(1, 0, self.n2)] -        while q: -            k, l, r = q.pop() -            if a <= l and r <= b: -                result = min(result, self.tree[k]) -                continue -            m = (l + r) // 2 -            k <<= 1 -            if a < m and l < b: -                q.append((k, l, m)) -            if a < r and l < m: -                q.append((k + 1, m, r)) -        return result +from heapq import heappop, heappush, heapify +class Graph:  # directed +    def __init__(self, n, edge, indexed=1): +        self.n = n +        self.graph = [[] for _ in range(n)] +        self.deg = [0 for _ in range(n)] +        for e in edge: +            self.graph[e[0] - indexed].append((e[1] - indexed, e[2])) +            self.deg[e[1] - indexed] += 1 + +    def dijkstra(self, s, INF=10**18, restore_to=None): +        dist = [INF for _ in range(self.n)] +        dist[s] = 0 +        heap = [(0, s)] +        while heap: +            cost, node = heappop(heap) +            if dist[node] < cost: +                continue +            for adj, adjcost in self.graph[node]: +                if dist[node] + adjcost < dist[adj]: +                    dist[adj] = dist[node] + adjcost +                    heappush(heap, (dist[adj], adj)) +        if restore_to is not None: +            g = restore_to +            if dist[g] == INF: +                return dist, False +            path = [g] +            node = g +            while node != s: +                node = prev[node] +                path.append(node) +            return dist, path[::-1] +        return dist + + +import sys + +input = sys.stdin.readline -edges = [list(map(int, input().split())) for i in range(M)] -edges.sort() -ST = SegTreeMin(N + 1, 10**15) -ST.update(0, 0) -for i in range(M): -    min_ = ST.get_min(edges[i][0] - 1, edges[i][1] - 1) -    if min_ + edges[i][2] < ST.get_min(edges[i][1] - 1, edges[i][1]): -        ST.update(edges[i][1] - 1, min_ + edges[i][2]) -if ST.get_min(N - 1, N) != 10**15: -    print((ST.get_min(N - 1, N))) -else: -    print((-1)) +E = list() +for _ in range(M): +    l, r, c = list(map(int, input().split())) +    E.append((l - 1, r - 1, c)) +for i in range(N - 1): +    E.append((i + 1, i, 0)) +g = Graph(N, E, 0) +res = g.dijkstra(0)[N - 1] +print((res if res != 10**18 else -1))