from heapq import *
from sys import stdin
n,m = stdin.readline().split(" ")
n=int(n)
m=int(m)*3
Lennot = [[] for i in range(n+1)]
c = stdin.read().split()
[Lennot[int(c[i])].append((int(c[i+1]),int(c[i+2]))) for i in range(0,m,3)]
n2 = n<<1
H = [True]*(n+1)
V = [2**62]*(n+1)
H[1] = 0
L = [(h,b) for b,h in Lennot[1]]
heapify(L)
#np = n
#nv = n2+1
while len(L):
h,b = heappop(L)
if H[b]:
H[b] = False
if h>=V[n]: break
if b==n: break
[V.__setitem__(bnv,h) or [heappush(L,(h+hn,bn)) for bn,hn in Lennot[bnv] if H[bn]] for bnv,asd in Lennot[b] if h<V[bnv]]
print(min(h,V[n]))