import heapq

def dijkstra_heap(s):

    d = [float("inf")] * n

    used = [True] * n 

    d[s] = 0

    used[s] = False

    edgelist = []

    for e in edge[s]:

        heapq.heappush(edgelist,e)

    while len(edgelist):

        minedge = heapq.heappop(edgelist)

        if not used[minedge[1]]:

            continue

        v = minedge[1]

        d[v] = minedge[0]

        used[v] = False

        for e in edge[v]:

            if used[e[1]]:

                heapq.heappush(edgelist,[e[0]+d[v],e[1]])

    return d





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



edge = [[] for i in range(n)]

for i in range(m):

    x,y,z = list(map(int,input().split()))

    edge[x-1].append([z,y-1])

    edge[y-1].append([z,x-1]) 

for i in range(1,n):

  edge[i].append([0,i-1])

x=dijkstra_heap(0)[n-1]

if x==float("inf"):

  print((-1))

else:

  print(x)
import sys

input=sys.stdin.readline

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

edge=[[] for i in range(n)]

for i in range(m):

  x,y,z=list(map(int,input().split()))

  edge[x-1].append([z,y-1])

  edge[y-1].append([z,x-1])

for i in range(1,n):

  edge[i].append([0,i-1])



import heapq

def dijkstra_heap(s):

  d=[float("inf")]*n

  used=[True]*n 

  d[s]=0

  used[s]=False

  edgelist=[]

  for e in edge[s]:

    heapq.heappush(edgelist,e)

  while len(edgelist):

    minedge=heapq.heappop(edgelist)

    if not used[minedge[1]]:

      continue

    v=minedge[1]

    d[v]=minedge[0]

    used[v]=False

    for e in edge[v]:

      if used[e[1]]:

        heapq.heappush(edgelist,[e[0]+d[v],e[1]])

  return d



x=dijkstra_heap(0)[n-1]

if x==float("inf"):

  print((-1))

else:

  print(x)
import heapq


def dijkstra_heap(s):
    d = [float("inf")] * n
    used = [True] * n
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist, e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist, [e[0] + d[v], e[1]])
    return d


n, m = list(map(int, input().split()))
edge = [[] for i in range(n)]
for i in range(m):
    x, y, z = list(map(int, input().split()))
    edge[x - 1].append([z, y - 1])
    edge[y - 1].append([z, x - 1])
for i in range(1, n):
    edge[i].append([0, i - 1])
x = dijkstra_heap(0)[n - 1]
if x == float("inf"):
    print((-1))
else:
    print(x)

import sys

input = sys.stdin.readline
n, m = list(map(int, input().split()))
edge = [[] for i in range(n)]
for i in range(m):
    x, y, z = list(map(int, input().split()))
    edge[x - 1].append([z, y - 1])
    edge[y - 1].append([z, x - 1])
for i in range(1, n):
    edge[i].append([0, i - 1])
import heapq


def dijkstra_heap(s):
    d = [float("inf")] * n
    used = [True] * n
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist, e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist, [e[0] + d[v], e[1]])
    return d


x = dijkstra_heap(0)[n - 1]
if x == float("inf"):
    print((-1))
else:
    print(x)

+import sys + +input = sys.stdin.readline +n, m = list(map(int, input().split())) +edge = [[] for i in range(n)] +for i in range(m): +    x, y, z = list(map(int, input().split())) +    edge[x - 1].append([z, y - 1]) +    edge[y - 1].append([z, x - 1]) +for i in range(1, n): +    edge[i].append([0, i - 1]) -n, m = list(map(int, input().split())) -edge = [[] for i in range(n)] -for i in range(m): -    x, y, z = list(map(int, input().split())) -    edge[x - 1].append([z, y - 1]) -    edge[y - 1].append([z, x - 1]) -for i in range(1, n): -    edge[i].append([0, i - 1])