from scipy.sparse.csgraph import csgraph_from_dense,dijkstra
import math
n,m=map(int,input().split())
edge=[[math.inf for _ in range(n)] for _ in range(n)]

for i in range(m):
    l,r,c=map(int,input().split())
    edge[l-1][r-1]=c
    edge[r-1][l-1]=c

G=csgraph_from_dense(edge,null_value=math.inf)
d=dijkstra(G,indices=0)
#print(d)
print(int(d[n-1]))