INF = 2**31 - 1

class RangeMinimunQuery:
    def __init__(self, n):
        self._n = 1 << n.bit_length()
        self._d = [INF] * (2 * self._n)

    def update(self, k, v):
        k += self._n
        self._d[k] = v
        k >>= 1
        while k:
            self._d[k] = min(self._d[2 * k], self._d[2 * k + 1])
            k >>= 1

    def query(self, l, r):
        l += self._n
        r += self._n
        res = INF
        while l < r:
            if l & 1:
                res = min(res, self._d[l])
                l += 1
            if r & 1:
                r -= 1
                res = min(res, self._d[r])
            l >>= 1
            r >>= 1
        return res

    def a(self, k):
        return self._d[k + self._n]

    def d(self):
        return self._d[self._n:]

n, m = map(int, raw_input().split())
edges = []
for _ in range(m):
    edges.append(map(int, raw_input().split()))
edges.sort()

segtree = RangeMinimunQuery(n)
segtree.update(1, 0)
for l, r, c in edges:
    cost = min(segtree.query(l, r) + c, segtree.a(r))
    segtree.update(r, cost)

if segtree.a(n) == INF:
    print -1
else:
    print segtree.a(n)
