type re=record
point,x:longint;
end;
var n,m,i,j,k,l,x,ii,y,z,tot,top,point,v:longint;
head,next,vet,len,dist:array[0..1000005] of longint;
p:array[0..1000005] of re;
b:array[0..1000005] of boolean;
procedure put(point,len:longint);
var son,fa:longint;
y:re;
begin
inc(top);
p[top].point:=point;
p[top].x:=len;
son:=top;
while son>1 do begin
if p[son shr 1].x>p[son].x then begin
y:=p[son];p[son]:=p[son shr 1];p[son shr 1]:=y;
end else break;
son:=son shr 1;
end;
end;
procedure get;
var son,fa:longint;
y:re;
begin
point:=p[1].point;
p[1]:=p[top];
dec(top);
fa:=1;
while fa*2<=top do begin
if (fa*2+1>top)or(p[fa*2].x<p[fa*2+1].x) then son:=fa*2 else son:=fa*2+1;
if p[fa].x>p[son].x then begin y:=p[fa];p[fa]:=p[son];p[son]:=y;end else break;
fa:=son;
end;
end;
begin
readln(n,m);
for i:=1 to n do dist[i]:=maxlongint shr 1;
for i:=1 to m do begin
readln(x,y,z);
inc(tot);
next[tot]:=head[x];
vet[tot]:=y;
len[tot]:=z;
head[x]:=tot;
inc(tot);
next[tot]:=head[y];
vet[tot]:=x;
len[tot]:=z;
head[y]:=tot;
end;
top:=1;
p[top].point:=1;
p[top].x:=0;
dist[1]:=0;b[1]:=true;
while top>0 do begin //for i:=1 to n do begin
get;
if point=n then break;
ii:=head[point];
while ii<>0 do
begin
v:=vet[ii];
l:=len[ii];
if dist[point]+l<dist[v] then begin dist[v]:=dist[point]+l;
//if not b[v] then begin b[v]:=true;put(v,dist[v]);end;
put(v,dist[v]);
end;
ii:=next[ii];
end;
end;
writeln(dist[n]);
end.