import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

input = lambda:sys.stdin.readline().rstrip()



class SegmentTree(object):

    def __init__(self, A, dot, unit):

        n = 1 << (len(A) - 1).bit_length()

        tree = [unit] * (2 * n)

        for i, v in enumerate(A):

            tree[i + n] = v

        for i in range(n - 1, 0, -1):

            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])

        self._n = n

        self._tree = tree

        self._dot = dot

        self._unit = unit



    def __getitem__(self, i):

        return self._tree[i + self._n]



    def update(self, i, v):

        i += self._n

        self._tree[i] = v

        while i != 1:

            i >>= 1

            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])



    def add(self, i, v):

        self.update(i, self[i] + v)



    def sum(self, l, r):

        l += self._n

        r += self._n

        l_val = r_val = self._unit

        while l < r:

            if l & 1:

                l_val = self._dot(l_val, self._tree[l])

                l += 1

            if r & 1:

                r -= 1

                r_val = self._dot(self._tree[r], r_val)

            l >>= 1

            r >>= 1

        return self._dot(l_val, r_val)



def resolve():

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

    edges = []

    for _ in range(m):

        u, v, w = list(map(int, input().split()))

        u -= 1; v -= 1

        edges.append((u, v, w))

    edges.sort()



    tree = SegmentTree([INF] * n, min, INF)

    tree.update(0, 0)

    for u, v, w in edges:

        tree.update(v, min(tree[v], tree.sum(u, v) + w))



    ans = tree[n - 1]

    if ans == INF:

        ans = -1

    print(ans)

resolve()
import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

input = lambda:sys.stdin.readline().rstrip()



class SegmentTree(object):

    def __init__(self, A, dot, unit):

        n = 1 << (len(A) - 1).bit_length()

        tree = [unit] * (2 * n)

        for i, v in enumerate(A):

            tree[i + n] = v

        for i in range(n - 1, 0, -1):

            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])

        self._n = n

        self._tree = tree

        self._dot = dot

        self._unit = unit



    def __getitem__(self, i):

        return self._tree[i + self._n]



    def update(self, i, v):

        i += self._n

        self._tree[i] = v

        while i != 1:

            i >>= 1

            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])



    def add(self, i, v):

        self.update(i, self[i] + v)



    def sum(self, l, r):

        l += self._n

        r += self._n

        l_val = r_val = self._unit

        while l < r:

            if l & 1:

                l_val = self._dot(l_val, self._tree[l])

                l += 1

            if r & 1:

                r -= 1

                r_val = self._dot(self._tree[r], r_val)

            l >>= 1

            r >>= 1

        return self._dot(l_val, r_val)



def resolve():

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

    E = [[] for _ in range(n)]

    for _ in range(m):

        u, v, w = list(map(int, input().split()))

        u -= 1; v -= 1

        E[u].append((v, w))



    tree = SegmentTree([INF] * n, min, INF)

    tree.update(0, 0)

    for v in range(n):

        for nv, w in E[v]:

            tree.update(nv, min(tree[nv], tree.sum(v, nv) + w))



    ans = tree[n - 1]

    if ans == INF:

        ans = -1

    print(ans)

resolve()
import sys

INF = 1 << 60
MOD = 10**9 + 7  # 998244353
sys.setrecursionlimit(2147483647)
input = lambda: sys.stdin.readline().rstrip()


class SegmentTree(object):
    def __init__(self, A, dot, unit):
        n = 1 << (len(A) - 1).bit_length()
        tree = [unit] * (2 * n)
        for i, v in enumerate(A):
            tree[i + n] = v
        for i in range(n - 1, 0, -1):
            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])
        self._n = n
        self._tree = tree
        self._dot = dot
        self._unit = unit

    def __getitem__(self, i):
        return self._tree[i + self._n]

    def update(self, i, v):
        i += self._n
        self._tree[i] = v
        while i != 1:
            i >>= 1
            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])

    def add(self, i, v):
        self.update(i, self[i] + v)

    def sum(self, l, r):
        l += self._n
        r += self._n
        l_val = r_val = self._unit
        while l < r:
            if l & 1:
                l_val = self._dot(l_val, self._tree[l])
                l += 1
            if r & 1:
                r -= 1
                r_val = self._dot(self._tree[r], r_val)
            l >>= 1
            r >>= 1
        return self._dot(l_val, r_val)


def resolve():
    n, m = list(map(int, input().split()))
    edges = []
    for _ in range(m):
        u, v, w = list(map(int, input().split()))
        u -= 1
        v -= 1
        edges.append((u, v, w))
    edges.sort()
    tree = SegmentTree([INF] * n, min, INF)
    tree.update(0, 0)
    for u, v, w in edges:
        tree.update(v, min(tree[v], tree.sum(u, v) + w))
    ans = tree[n - 1]
    if ans == INF:
        ans = -1
    print(ans)


resolve()

import sys

INF = 1 << 60
MOD = 10**9 + 7  # 998244353
sys.setrecursionlimit(2147483647)
input = lambda: sys.stdin.readline().rstrip()


class SegmentTree(object):
    def __init__(self, A, dot, unit):
        n = 1 << (len(A) - 1).bit_length()
        tree = [unit] * (2 * n)
        for i, v in enumerate(A):
            tree[i + n] = v
        for i in range(n - 1, 0, -1):
            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])
        self._n = n
        self._tree = tree
        self._dot = dot
        self._unit = unit

    def __getitem__(self, i):
        return self._tree[i + self._n]

    def update(self, i, v):
        i += self._n
        self._tree[i] = v
        while i != 1:
            i >>= 1
            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])

    def add(self, i, v):
        self.update(i, self[i] + v)

    def sum(self, l, r):
        l += self._n
        r += self._n
        l_val = r_val = self._unit
        while l < r:
            if l & 1:
                l_val = self._dot(l_val, self._tree[l])
                l += 1
            if r & 1:
                r -= 1
                r_val = self._dot(self._tree[r], r_val)
            l >>= 1
            r >>= 1
        return self._dot(l_val, r_val)


def resolve():
    n, m = list(map(int, input().split()))
    E = [[] for _ in range(n)]
    for _ in range(m):
        u, v, w = list(map(int, input().split()))
        u -= 1
        v -= 1
        E[u].append((v, w))
    tree = SegmentTree([INF] * n, min, INF)
    tree.update(0, 0)
    for v in range(n):
        for nv, w in E[v]:
            tree.update(nv, min(tree[nv], tree.sum(v, nv) + w))
    ans = tree[n - 1]
    if ans == INF:
        ans = -1
    print(ans)


resolve()

-    edges = [] +    E = [[] for _ in range(n)] -        edges.append((u, v, w)) -    edges.sort() +        E[u].append((v, w)) -    for u, v, w in edges: -        tree.update(v, min(tree[v], tree.sum(u, v) + w)) +    for v in range(n): +        for nv, w in E[v]: +            tree.update(nv, min(tree[nv], tree.sum(v, nv) + w))