#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN = 0x3f3f3f3f;
int n,m;
int source,end;
struct edge{
int to,wight;
int pre;
}e[3200000];
int vertex[320000];
int d[250000];
bool flag[250000];
int cnt = 0;
void add(int v,int u,int w){
e[++cnt].to = u;
e[cnt].wight = w;
e[cnt].pre = vertex[v];
vertex[v] = cnt;
}
void printGraph(int n){
for(int i=1;i<=n;i++){
int index = vertex[i];
cout<<i<<':';
while( index ){
edge newEdge = e[index];
cout<<newEdge.to<<' ';
index = newEdge.pre;
}
cout<<endl;
}
}
void spfa(int n){
queue<int> que;
que.push(source);
flag[source] = 1;
while(!que.empty()){
int v = que.front();
flag[v] = 0;
que.pop();
for(int i=vertex[v];i;i=e[i].pre){
int to = e[i].to;
int w = e[i].wight;
if( d[v] + w < d[to] ){
d[to] = d[v] + w ;
if( !flag[to]){
que.push(to);
flag[to] = 1;
}
}
}
}
}
int main(){
cin>>n>>m;
int v,u,w;
for(int i=0;i<m;i++){
cin>>v>>u>>w;
add(v,u,w);
add(u,v,w);
}
source=1;end=n;
fill(d,d+100005,MAXN);
d[source] = 0;
spfa(n);
cout<<d[n]<<endl;
return 0;
}