import sys





class SegTreeMin:



    def __init__(self, n, max_v):

        self.n = n

        n2 = 1

        while n2 < n:

            n2 <<= 1

        self.n2 = n2

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

        self.INF = max_v



    def update(self, i, x):

        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):

        result = self.INF

        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





def solve():

    input = sys.stdin.readline

    mod = 10 ** 9 + 7

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

    seg_tree = SegTreeMin(n, 10 ** 15)

    seg_tree.update(0, 0)

    lrc = [list(map(int, input().rstrip('\n').split())) for _ in range(m)]

    lrc.sort()

    for l, r, c in lrc:

        l -= 1

        r -= 1

        tm = seg_tree.get_min(l, r)

        seg_tree.update(r, min(tm + c, seg_tree.get_min(r, r + 1)))

    mt = seg_tree.get_min(n-1, n)

    print((mt if mt != 10 ** 15 else -1))





if __name__ == '__main__':

    solve()

import sys





class AlgSegmentTreeMin:

    def __init__(self, n, default_value):

        self.Nelem = n

        self.default_value = default_value

        self.size = 1 << (n.bit_length())

        self.data = [default_value] * (2 * self.size)



    def build(self, raw_data):

        for i, x in enumerate(raw_data):

            self.data[self.size + i] = x

        for i in range(self.size - 1, 0, -1):

            x = self.data[i + i]

            y = self.data[i + i + 1]

            self.data[i] = min(x, y)



    def update(self, i, x):

        i += self.size

        self.data[i] = x

        i >>= 1

        while i:

            x = self.data[i + i]

            y = self.data[i + i + 1]

            self.data[i] = min(x, y)

            i >>= 1



    def get_value(self, left, right):

        left += self.size

        right += self.size + 1

        x = self.default_value

        while left < right:

            if left & 1:

                y = self.data[left]

                x = min(x, y)

                left += 1

            if right & 1:

                right -= 1

                y = self.data[right]

                x = min(x, y)

            left >>= 1

            right >>= 1

        return x





def solve():

    input = sys.stdin.readline

    mod = 10 ** 9 + 7

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

    stree = AlgSegmentTreeMin(n, 10 ** 15)

    stree.update(0, 0)

    lrc = [list(map(int, input().rstrip('\n').split())) for _ in range(m)]

    lrc.sort()

    for l, r, c in lrc:

        l -= 1

        r -= 1

        tm = stree.get_value(l, r)

        stree.update(r, min(tm + c, stree.get_value(r, r + 1)))

    mt = stree.get_value(n-1, n)

    print((mt if mt != stree.default_value else -1))





if __name__ == '__main__':

    solve()

import sys


class SegTreeMin:
    def __init__(self, n, max_v):
        self.n = n
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [max_v] * (n2 << 1)
        self.INF = max_v

    def update(self, i, x):
        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):
        result = self.INF
        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


def solve():
    input = sys.stdin.readline
    mod = 10**9 + 7
    n, m = list(map(int, input().rstrip("\n").split()))
    seg_tree = SegTreeMin(n, 10**15)
    seg_tree.update(0, 0)
    lrc = [list(map(int, input().rstrip("\n").split())) for _ in range(m)]
    lrc.sort()
    for l, r, c in lrc:
        l -= 1
        r -= 1
        tm = seg_tree.get_min(l, r)
        seg_tree.update(r, min(tm + c, seg_tree.get_min(r, r + 1)))
    mt = seg_tree.get_min(n - 1, n)
    print((mt if mt != 10**15 else -1))


if __name__ == "__main__":
    solve()

import sys


class AlgSegmentTreeMin:
    def __init__(self, n, default_value):
        self.Nelem = n
        self.default_value = default_value
        self.size = 1 << (n.bit_length())
        self.data = [default_value] * (2 * self.size)

    def build(self, raw_data):
        for i, x in enumerate(raw_data):
            self.data[self.size + i] = x
        for i in range(self.size - 1, 0, -1):
            x = self.data[i + i]
            y = self.data[i + i + 1]
            self.data[i] = min(x, y)

    def update(self, i, x):
        i += self.size
        self.data[i] = x
        i >>= 1
        while i:
            x = self.data[i + i]
            y = self.data[i + i + 1]
            self.data[i] = min(x, y)
            i >>= 1

    def get_value(self, left, right):
        left += self.size
        right += self.size + 1
        x = self.default_value
        while left < right:
            if left & 1:
                y = self.data[left]
                x = min(x, y)
                left += 1
            if right & 1:
                right -= 1
                y = self.data[right]
                x = min(x, y)
            left >>= 1
            right >>= 1
        return x


def solve():
    input = sys.stdin.readline
    mod = 10**9 + 7
    n, m = list(map(int, input().rstrip("\n").split()))
    stree = AlgSegmentTreeMin(n, 10**15)
    stree.update(0, 0)
    lrc = [list(map(int, input().rstrip("\n").split())) for _ in range(m)]
    lrc.sort()
    for l, r, c in lrc:
        l -= 1
        r -= 1
        tm = stree.get_value(l, r)
        stree.update(r, min(tm + c, stree.get_value(r, r + 1)))
    mt = stree.get_value(n - 1, n)
    print((mt if mt != stree.default_value else -1))


if __name__ == "__main__":
    solve()

-class SegTreeMin: -    def __init__(self, n, max_v): -        self.n = n -        n2 = 1 -        while n2 < n: -            n2 <<= 1 -        self.n2 = n2 -        self.tree = [max_v] * (n2 << 1) -        self.INF = max_v +class AlgSegmentTreeMin: +    def __init__(self, n, default_value): +        self.Nelem = n +        self.default_value = default_value +        self.size = 1 << (n.bit_length()) +        self.data = [default_value] * (2 * self.size) + +    def build(self, raw_data): +        for i, x in enumerate(raw_data): +            self.data[self.size + i] = x +        for i in range(self.size - 1, 0, -1): +            x = self.data[i + i] +            y = self.data[i + i + 1] +            self.data[i] = min(x, y) -        i += self.n2 -        self.tree[i] = x -        while i > 1: -            self.tree[i >> 1] = x = min(x, self.tree[i ^ 1]) +        i += self.size +        self.data[i] = x +        i >>= 1 +        while i: +            x = self.data[i + i] +            y = self.data[i + i + 1] +            self.data[i] = min(x, y) -    def get_min(self, a, b): -        result = self.INF -        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 +    def get_value(self, left, right): +        left += self.size +        right += self.size + 1 +        x = self.default_value +        while left < right: +            if left & 1: +                y = self.data[left] +                x = min(x, y) +                left += 1 +            if right & 1: +                right -= 1 +                y = self.data[right] +                x = min(x, y) +            left >>= 1 +            right >>= 1 +        return x -    seg_tree = SegTreeMin(n, 10**15) -    seg_tree.update(0, 0) +    stree = AlgSegmentTreeMin(n, 10**15) +    stree.update(0, 0) -        tm = seg_tree.get_min(l, r) -        seg_tree.update(r, min(tm + c, seg_tree.get_min(r, r + 1))) -    mt = seg_tree.get_min(n - 1, n) -    print((mt if mt != 10**15 else -1)) +        tm = stree.get_value(l, r) +        stree.update(r, min(tm + c, stree.get_value(r, r + 1))) +    mt = stree.get_value(n - 1, n) +    print((mt if mt != stree.default_value else -1))