import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

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

from heapq import heappop, heappush

def resolve():

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

    N = 1 << (n - 1).bit_length()

    N2, now = N * 2, N * 3

    E = [[] for _ in range(N * 3 + m)]

    ref = lambda v : v if v < 3 * N else v - N2



    # build range-edge graph

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

        cl, cr = i << 1, i << 1 | 1

        E[i].append((cl, 0))

        E[i].append((cr, 0))

        E[ref(cl + N2)].append((ref(i + N2), 0))

        E[ref(cr + N2)].append((ref(i + N2), 0))



    # add range-edge

    for _ in range(m):

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

        u -= 1

        # lower

        l, r = u + N, v + N

        while l < r:

            if l & 1:

                E[ref(l + N2)].append((now, w))

                l += 1

            if r & 1:

                r -= 1

                E[ref(r + N2)].append((now, w))

            l >>= 1

            r >>= 1

        # upper

        l, r = u + N, v + N

        while l < r:

            if l & 1:

                E[now].append((l, 0))

                l += 1

            if r & 1:

                r -= 1

                E[now].append((r, 0))

            l >>= 1

            r >>= 1

        now += 1



    # Dijkstra (source : N, termination : N + n - 1)

    dist = [INF] * now

    dist[N] = 0

    heap = [(0, N)]

    while heap:

        d, v = heappop(heap)

        if dist[v] != d:

            continue

        for nv, w in E[v]:

            if dist[nv] > d + w:

                dist[nv] = d + w

                heappush(heap, (d + w, nv))



    ans = dist[N + 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()

from heapq import heappop, heappush

def resolve():

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

    N = 1 << (n - 1).bit_length()

    N2, N3 = N * 2, N * 3

    E = [[] for _ in range(N * 3 + m)]

    ref = lambda v : v if v < 3 * N else v - N2



    # build range-edge graph

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

        cl, cr = i << 1, i << 1 | 1

        E[i].append((cl, 0))

        E[i].append((cr, 0))

        E[ref(cl + N2)].append((ref(i + N2), 0))

        E[ref(cr + N2)].append((ref(i + N2), 0))



    # add range-edge

    for i in range(m):

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

        u -= 1

        # lower

        l, r = u + N, v + N

        while l < r:

            if l & 1:

                E[ref(l + N2)].append((N3 + i, w))

                l += 1

            if r & 1:

                r -= 1

                E[ref(r + N2)].append((N3 + i, w))

            l >>= 1

            r >>= 1

        # upper

        l, r = u + N, v + N

        while l < r:

            if l & 1:

                E[N3 + i].append((l, 0))

                l += 1

            if r & 1:

                r -= 1

                E[N3 + i].append((r, 0))

            l >>= 1

            r >>= 1



    # Dijkstra (source : N, termination : N + n - 1)

    dist = [INF] * (N3 + m)

    dist[N] = 0

    heap = [(0, N)]

    while heap:

        d, v = heappop(heap)

        if dist[v] != d:

            continue

        for nv, w in E[v]:

            if dist[nv] > d + w:

                dist[nv] = d + w

                heappush(heap, (d + w, nv))



    ans = dist[N + 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()
from heapq import heappop, heappush


def resolve():
    n, m = list(map(int, input().split()))
    N = 1 << (n - 1).bit_length()
    N2, now = N * 2, N * 3
    E = [[] for _ in range(N * 3 + m)]
    ref = lambda v: v if v < 3 * N else v - N2
    # build range-edge graph
    for i in range(N - 1, 0, -1):
        cl, cr = i << 1, i << 1 | 1
        E[i].append((cl, 0))
        E[i].append((cr, 0))
        E[ref(cl + N2)].append((ref(i + N2), 0))
        E[ref(cr + N2)].append((ref(i + N2), 0))
    # add range-edge
    for _ in range(m):
        u, v, w = list(map(int, input().split()))
        u -= 1
        # lower
        l, r = u + N, v + N
        while l < r:
            if l & 1:
                E[ref(l + N2)].append((now, w))
                l += 1
            if r & 1:
                r -= 1
                E[ref(r + N2)].append((now, w))
            l >>= 1
            r >>= 1
        # upper
        l, r = u + N, v + N
        while l < r:
            if l & 1:
                E[now].append((l, 0))
                l += 1
            if r & 1:
                r -= 1
                E[now].append((r, 0))
            l >>= 1
            r >>= 1
        now += 1
    # Dijkstra (source : N, termination : N + n - 1)
    dist = [INF] * now
    dist[N] = 0
    heap = [(0, N)]
    while heap:
        d, v = heappop(heap)
        if dist[v] != d:
            continue
        for nv, w in E[v]:
            if dist[nv] > d + w:
                dist[nv] = d + w
                heappush(heap, (d + w, nv))
    ans = dist[N + 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()
from heapq import heappop, heappush


def resolve():
    n, m = list(map(int, input().split()))
    N = 1 << (n - 1).bit_length()
    N2, N3 = N * 2, N * 3
    E = [[] for _ in range(N * 3 + m)]
    ref = lambda v: v if v < 3 * N else v - N2
    # build range-edge graph
    for i in range(N - 1, 0, -1):
        cl, cr = i << 1, i << 1 | 1
        E[i].append((cl, 0))
        E[i].append((cr, 0))
        E[ref(cl + N2)].append((ref(i + N2), 0))
        E[ref(cr + N2)].append((ref(i + N2), 0))
    # add range-edge
    for i in range(m):
        u, v, w = list(map(int, input().split()))
        u -= 1
        # lower
        l, r = u + N, v + N
        while l < r:
            if l & 1:
                E[ref(l + N2)].append((N3 + i, w))
                l += 1
            if r & 1:
                r -= 1
                E[ref(r + N2)].append((N3 + i, w))
            l >>= 1
            r >>= 1
        # upper
        l, r = u + N, v + N
        while l < r:
            if l & 1:
                E[N3 + i].append((l, 0))
                l += 1
            if r & 1:
                r -= 1
                E[N3 + i].append((r, 0))
            l >>= 1
            r >>= 1
    # Dijkstra (source : N, termination : N + n - 1)
    dist = [INF] * (N3 + m)
    dist[N] = 0
    heap = [(0, N)]
    while heap:
        d, v = heappop(heap)
        if dist[v] != d:
            continue
        for nv, w in E[v]:
            if dist[nv] > d + w:
                dist[nv] = d + w
                heappush(heap, (d + w, nv))
    ans = dist[N + n - 1]
    if ans == INF:
        ans = -1
    print(ans)


resolve()

-    N2, now = N * 2, N * 3 +    N2, N3 = N * 2, N * 3 -    for _ in range(m): +    for i in range(m): -                E[ref(l + N2)].append((now, w)) +                E[ref(l + N2)].append((N3 + i, w)) -                E[ref(r + N2)].append((now, w)) +                E[ref(r + N2)].append((N3 + i, w)) -                E[now].append((l, 0)) +                E[N3 + i].append((l, 0)) -                E[now].append((r, 0)) +                E[N3 + i].append((r, 0)) -        now += 1 -    dist = [INF] * now +    dist = [INF] * (N3 + m)