#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
typedef vector<int> V;
#define V(x) vector<x>
const int INF=0x3f3f3f3f,MOD=1000000000+7,maxn=5000+10;
int vis[205][205],E,s,v,cnt=1,m,n,ans=INF;
int par[maxn],dr[]={-1,1,0,0},dc[]={0,0,-1,1};
struct edge{
int from,to,cost;
edge(int a=0,int b=0,int c=0):from(a),to(b),cost(c){}
};
int find(int x) { return x == par[x] ? find(par[x]) : x; }
bool same(int x, int y) { x = find(x); y = find(y); return x == y; }
void unite(int x, int y) { x = find(x); y = find(y); if (x != y) par[x] = y; }
void init(int len) { for (int i = 0; i <= len; i++) par[i] = i; }
int d[maxn],d2[maxn];
V(edge) G[maxn];
void Dijkstra(){
priority_queue<P,V(P),greater<P> > que;
que.push(make_pair(0,1));
d[1]=0;
while (!que.empty()){
P t=que.top(); que.pop();
int v=t.second,di=t.first;
if (d2[v]<di) continue;
// printf("di:%d v:%d\n",di,v);
for (int i=0;i<G[v].size();i++){
edge& e=G[v][i];
int dis=di+e.cost;
if (dis<d[e.to]) {
swap(d[e.to],dis);
// printf("dis:%d d[%d]:%d\n",dis,e.to,d[e.to]);
que.push(make_pair(d[e.to],e.to));
}
// printf("d:");
if (dis<d2[e.to]&&dis>d[e.to]){
// printf("dis:%d d2[%d]:%d\n",dis,e.to,d2[e.to]);
d2[e.to]=dis;
que.push(make_pair(d2[e.to],e.to));
}
/*for (int i=0;i<=n;i++){
printf("%d ",d[i]);
}putchar('\n');
printf("d2:");
for (int j=0;j<=n;j++){
printf("%d ",d2[i]);
}putchar('\n');*/
}
}
}
int main(){
scanf("%d%d",&n,&m);
fill(d,d+n+1,INF);
fill(d2,d2+n+1,INF);
for (int i=0;i<m;i++){
int u,v,cos;
scanf("%d%d%d",&u,&v,&cos);
G[u].push_back(edge(u,v,cos));
G[v].push_back(edge(v,u,cos));
}
Dijkstra();
printf("%d\n",d2[n]);
// printf("%d\n",d[n]);
}