import sys





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



        l = a + self.n2

        r = b + self.n2

        while l < r:

            if r & 1:

                result = min(result, self.tree[r - 1])

            if l & 1:

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

                l += 1

            l >>= 1

            r >>= 1



        return result



    def debug_print(self):

        a = 1

        while a <= self.n2:

            b = a << 1

            print((self.tree[a:b]))

            a = b

        print()





INF = 10 ** 18

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

segments = [tuple(map(int, line.split())) for line in sys.stdin]

segments.sort()

sgt = SegTreeMin(n, INF)

sgt.update(0, 0)



for l, r, c in segments:

    use_current = sgt.get_min(l - 1, r - 1) + c

    not_current = sgt.get_min(r - 1, r)



    if use_current < not_current:

        sgt.update(r - 1, use_current)



ans = sgt.get_min(n - 1, n)

if ans == INF:

    print((-1))

else:

    print(ans)

import sys

from heapq import heappop, heappush





def dijkstra(s, t, links):

    q = [(0, s)]

    visited = set()

    while q:

        cost, v = heappop(q)

        if v in visited:

            continue

        if v == t:

            return cost

        visited.add(v)

        for u, c in links[v]:

            if u in visited:

                continue

            heappush(q, (cost + c, u))

    return -1





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

links = [set() for _ in range(n)]

for v in range(n - 1):

    links[v + 1].add((v, 0))

for line in sys.stdin:

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

    links[l - 1].add((r - 1, c))

print((dijkstra(0, n - 1, links)))

import sys


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
        l = a + self.n2
        r = b + self.n2
        while l < r:
            if r & 1:
                result = min(result, self.tree[r - 1])
            if l & 1:
                result = min(result, self.tree[l])
                l += 1
            l >>= 1
            r >>= 1
        return result

    def debug_print(self):
        a = 1
        while a <= self.n2:
            b = a << 1
            print((self.tree[a:b]))
            a = b
        print()


INF = 10**18
n, m = list(map(int, input().split()))
segments = [tuple(map(int, line.split())) for line in sys.stdin]
segments.sort()
sgt = SegTreeMin(n, INF)
sgt.update(0, 0)
for l, r, c in segments:
    use_current = sgt.get_min(l - 1, r - 1) + c
    not_current = sgt.get_min(r - 1, r)
    if use_current < not_current:
        sgt.update(r - 1, use_current)
ans = sgt.get_min(n - 1, n)
if ans == INF:
    print((-1))
else:
    print(ans)

import sys
from heapq import heappop, heappush


def dijkstra(s, t, links):
    q = [(0, s)]
    visited = set()
    while q:
        cost, v = heappop(q)
        if v in visited:
            continue
        if v == t:
            return cost
        visited.add(v)
        for u, c in links[v]:
            if u in visited:
                continue
            heappush(q, (cost + c, u))
    return -1


n, m = list(map(int, input().split()))
links = [set() for _ in range(n)]
for v in range(n - 1):
    links[v + 1].add((v, 0))
for line in sys.stdin:
    l, r, c = list(map(int, line.split()))
    links[l - 1].add((r - 1, c))
print((dijkstra(0, n - 1, links)))

+from heapq import heappop, heappush -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 -        l = a + self.n2 -        r = b + self.n2 -        while l < r: -            if r & 1: -                result = min(result, self.tree[r - 1]) -            if l & 1: -                result = min(result, self.tree[l]) -                l += 1 -            l >>= 1 -            r >>= 1 -        return result - -    def debug_print(self): -        a = 1 -        while a <= self.n2: -            b = a << 1 -            print((self.tree[a:b])) -            a = b -        print() +def dijkstra(s, t, links): +    q = [(0, s)] +    visited = set() +    while q: +        cost, v = heappop(q) +        if v in visited: +            continue +        if v == t: +            return cost +        visited.add(v) +        for u, c in links[v]: +            if u in visited: +                continue +            heappush(q, (cost + c, u)) +    return -1 -INF = 10**18 -segments = [tuple(map(int, line.split())) for line in sys.stdin] -segments.sort() -sgt = SegTreeMin(n, INF) -sgt.update(0, 0) -for l, r, c in segments: -    use_current = sgt.get_min(l - 1, r - 1) + c -    not_current = sgt.get_min(r - 1, r) -    if use_current < not_current: -        sgt.update(r - 1, use_current) -ans = sgt.get_min(n - 1, n) -if ans == INF: -    print((-1)) -else: -    print(ans) +links = [set() for _ in range(n)] +for v in range(n - 1): +    links[v + 1].add((v, 0)) +for line in sys.stdin: +    l, r, c = list(map(int, line.split())) +    links[l - 1].add((r - 1, c)) +print((dijkstra(0, n - 1, links)))