# -*- coding: utf-8 -*-



import sys

def input(): return sys.stdin.readline().strip()

def list2d(a, b, c): return [[c] * b for i in range(a)]

def list3d(a, b, c, d): return [[[d] * c for k in range(b)] for i in range(a)]

def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for k in range(b)] for i in range(a)]

def ceil(x, y=1): return int(-(-x // y))

def INT(): return int(eval(input()))

def MAP(): return list(map(int, input().split()))

def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes(): print('Yes')

def No(): print('No')

def YES(): print('YES')

def NO(): print('NO')

sys.setrecursionlimit(10 ** 9)

INF = 10 ** 18

MOD = 998244353



class SegTree:

    """

    セグメント木

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

    2.query: 区間[l, r)の値を得る

    """



    def __init__(self, n, func, intv, A=[]):

        """

        :param n: 要素数(0-indexed)

        :param func: 値の操作に使う関数(min, max, add, gcdなど)

        :param intv: 要素の初期値(単位元)

        :param A: 初期化に使うリスト(オプション)

        """

        self.n = n

        self.func = func

        self.intv = intv

        # nより大きい2の冪数

        n2 = 1

        while n2 < n:

            n2 <<= 1

        self.n2 = n2

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

        # 初期化の値が決まっている場合

        if A:

            # 1段目(最下段)の初期化

            for i in range(n):

                self.tree[n2+i] = A[i]

            # 2段目以降の初期化

            for i in range(n2-1, -1, -1):

                self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])



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

            i >>= 1

            self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])



    def query(self, a, b):

        """

        [a, b)の値を得る

        :param a: index(0-indexed)

        :param b: index(0-indexed)

        """

        l = a + self.n2

        r = b + self.n2

        s = self.intv

        while l < r:

            if r & 1:

                r -= 1

                s = self.func(s, self.tree[r])

            if l & 1:

                s = self.func(s, self.tree[l])

                l += 1

            l >>= 1

            r >>= 1

        return s



    def get(self, i):

        """ 一点取得 """

        return self.tree[i+self.n2]



    def all(self):

        """ 全区間[0, n)の取得 """

        return self.tree[1]



N, M = MAP()

LRC = []

for i in range(M):

    l, r, c = MAP()

    l -= 1

    r -= 1

    LRC.append((l, r, c))



LRC.sort()

st = SegTree(N, min, INF)

st.update(0, 0)

for l, r, c in LRC:

    mn = st.query(l, r+1)

    st.update(r, min(st.get(r), mn+c))



ans = st.get(N-1)

if ans != INF:

    print((st.get(N-1)))

else:

    print((-1))

# -*- coding: utf-8 -*-



import sys

def input(): return sys.stdin.readline().strip()

def list2d(a, b, c): return [[c] * b for i in range(a)]

def list3d(a, b, c, d): return [[[d] * c for k in range(b)] for i in range(a)]

def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for k in range(b)] for i in range(a)]

def ceil(x, y=1): return int(-(-x // y))

def INT(): return int(eval(input()))

def MAP(): return list(map(int, input().split()))

def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes(): print('Yes')

def No(): print('No')

def YES(): print('YES')

def NO(): print('NO')

sys.setrecursionlimit(10 ** 9)

INF = 10 ** 18

MOD = 998244353



class SegTree:

    """

    セグメント木

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

    2.query: 区間[l, r)の値を得る

    """



    def __init__(self, n, func, intv, A=[]):

        """

        :param n: 要素数(0-indexed)

        :param func: 値の操作に使う関数(min, max, add, gcdなど)

        :param intv: 要素の初期値(単位元)

        :param A: 初期化に使うリスト(オプション)

        """

        self.n = n

        self.func = func

        self.intv = intv

        # nより大きい2の冪数

        n2 = 1

        while n2 < n:

            n2 <<= 1

        self.n2 = n2

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

        # 初期化の値が決まっている場合

        if A:

            # 1段目(最下段)の初期化

            for i in range(n):

                self.tree[n2+i] = A[i]

            # 2段目以降の初期化

            for i in range(n2-1, -1, -1):

                self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])



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

            i >>= 1

            self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])



    def query(self, a, b):

        """

        [a, b)の値を得る

        :param a: index(0-indexed)

        :param b: index(0-indexed)

        """

        l = a + self.n2

        r = b + self.n2

        s = self.intv

        while l < r:

            if r & 1:

                r -= 1

                s = self.func(s, self.tree[r])

            if l & 1:

                s = self.func(s, self.tree[l])

                l += 1

            l >>= 1

            r >>= 1

        return s



    def get(self, i):

        """ 一点取得 """

        return self.tree[i+self.n2]



    def all(self):

        """ 全区間[0, n)の取得 """

        return self.tree[1]



N, M = MAP()

LRC = []

for i in range(M):

    l, r, c = MAP()

    l -= 1

    r -= 1

    LRC.append((l, r, c))



LRC.sort()

st = SegTree(N, min, INF)

st.update(0, 0)

for l, r, c in LRC:

    mn = st.query(l, r)

    if mn+c < st.get(r):

        st.update(r, mn+c)



ans = st.get(N-1)

if ans != INF:

    print((st.get(N-1)))

else:

    print((-1))

# -*- coding: utf-8 -*-
import sys


def input():
    return sys.stdin.readline().strip()


def list2d(a, b, c):
    return [[c] * b for i in range(a)]


def list3d(a, b, c, d):
    return [[[d] * c for k in range(b)] for i in range(a)]


def list4d(a, b, c, d, e):
    return [[[[e] * d for k in range(c)] for k in range(b)] for i in range(a)]


def ceil(x, y=1):
    return int(-(-x // y))


def INT():
    return int(eval(input()))


def MAP():
    return list(map(int, input().split()))


def LIST(N=None):
    return list(MAP()) if N is None else [INT() for i in range(N)]


def Yes():
    print("Yes")


def No():
    print("No")


def YES():
    print("YES")


def NO():
    print("NO")


sys.setrecursionlimit(10**9)
INF = 10**18
MOD = 998244353


class SegTree:
    """
    セグメント木
    1.update:  i番目の値をxに更新する
    2.query: 区間[l, r)の値を得る
    """

    def __init__(self, n, func, intv, A=[]):
        """
        :param n: 要素数(0-indexed)
        :param func: 値の操作に使う関数(min, max, add, gcdなど)
        :param intv: 要素の初期値(単位元)
        :param A: 初期化に使うリスト(オプション)
        """
        self.n = n
        self.func = func
        self.intv = intv
        # nより大きい2の冪数
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [self.intv] * (n2 << 1)
        # 初期化の値が決まっている場合
        if A:
            # 1段目(最下段)の初期化
            for i in range(n):
                self.tree[n2 + i] = A[i]
            # 2段目以降の初期化
            for i in range(n2 - 1, -1, -1):
                self.tree[i] = self.func(self.tree[i * 2], self.tree[i * 2 + 1])

    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 > 0:
            i >>= 1
            self.tree[i] = self.func(self.tree[i * 2], self.tree[i * 2 + 1])

    def query(self, a, b):
        """
        [a, b)の値を得る
        :param a: index(0-indexed)
        :param b: index(0-indexed)
        """
        l = a + self.n2
        r = b + self.n2
        s = self.intv
        while l < r:
            if r & 1:
                r -= 1
                s = self.func(s, self.tree[r])
            if l & 1:
                s = self.func(s, self.tree[l])
                l += 1
            l >>= 1
            r >>= 1
        return s

    def get(self, i):
        """一点取得"""
        return self.tree[i + self.n2]

    def all(self):
        """全区間[0, n)の取得"""
        return self.tree[1]


N, M = MAP()
LRC = []
for i in range(M):
    l, r, c = MAP()
    l -= 1
    r -= 1
    LRC.append((l, r, c))
LRC.sort()
st = SegTree(N, min, INF)
st.update(0, 0)
for l, r, c in LRC:
    mn = st.query(l, r + 1)
    st.update(r, min(st.get(r), mn + c))
ans = st.get(N - 1)
if ans != INF:
    print((st.get(N - 1)))
else:
    print((-1))

# -*- coding: utf-8 -*-
import sys


def input():
    return sys.stdin.readline().strip()


def list2d(a, b, c):
    return [[c] * b for i in range(a)]


def list3d(a, b, c, d):
    return [[[d] * c for k in range(b)] for i in range(a)]


def list4d(a, b, c, d, e):
    return [[[[e] * d for k in range(c)] for k in range(b)] for i in range(a)]


def ceil(x, y=1):
    return int(-(-x // y))


def INT():
    return int(eval(input()))


def MAP():
    return list(map(int, input().split()))


def LIST(N=None):
    return list(MAP()) if N is None else [INT() for i in range(N)]


def Yes():
    print("Yes")


def No():
    print("No")


def YES():
    print("YES")


def NO():
    print("NO")


sys.setrecursionlimit(10**9)
INF = 10**18
MOD = 998244353


class SegTree:
    """
    セグメント木
    1.update:  i番目の値をxに更新する
    2.query: 区間[l, r)の値を得る
    """

    def __init__(self, n, func, intv, A=[]):
        """
        :param n: 要素数(0-indexed)
        :param func: 値の操作に使う関数(min, max, add, gcdなど)
        :param intv: 要素の初期値(単位元)
        :param A: 初期化に使うリスト(オプション)
        """
        self.n = n
        self.func = func
        self.intv = intv
        # nより大きい2の冪数
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [self.intv] * (n2 << 1)
        # 初期化の値が決まっている場合
        if A:
            # 1段目(最下段)の初期化
            for i in range(n):
                self.tree[n2 + i] = A[i]
            # 2段目以降の初期化
            for i in range(n2 - 1, -1, -1):
                self.tree[i] = self.func(self.tree[i * 2], self.tree[i * 2 + 1])

    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 > 0:
            i >>= 1
            self.tree[i] = self.func(self.tree[i * 2], self.tree[i * 2 + 1])

    def query(self, a, b):
        """
        [a, b)の値を得る
        :param a: index(0-indexed)
        :param b: index(0-indexed)
        """
        l = a + self.n2
        r = b + self.n2
        s = self.intv
        while l < r:
            if r & 1:
                r -= 1
                s = self.func(s, self.tree[r])
            if l & 1:
                s = self.func(s, self.tree[l])
                l += 1
            l >>= 1
            r >>= 1
        return s

    def get(self, i):
        """一点取得"""
        return self.tree[i + self.n2]

    def all(self):
        """全区間[0, n)の取得"""
        return self.tree[1]


N, M = MAP()
LRC = []
for i in range(M):
    l, r, c = MAP()
    l -= 1
    r -= 1
    LRC.append((l, r, c))
LRC.sort()
st = SegTree(N, min, INF)
st.update(0, 0)
for l, r, c in LRC:
    mn = st.query(l, r)
    if mn + c < st.get(r):
        st.update(r, mn + c)
ans = st.get(N - 1)
if ans != INF:
    print((st.get(N - 1)))
else:
    print((-1))

-    mn = st.query(l, r + 1) -    st.update(r, min(st.get(r), mn + c)) +    mn = st.query(l, r) +    if mn + c < st.get(r): +        st.update(r, mn + c)