/* 有向图单源最短路径问题 权值 >= 0*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define M 1005
#define INF 900000000
using namespace std;
int t,n;
int g[M][M];
int d[M];
int vis_v[M];
int q;
void INITIALIZE()
{
memset(g,0x7f,sizeof(g));
memset(d,0x7f,sizeof(d));
memset(vis_v,0,sizeof(vis_v));
d[n] = 0;
}
int Find_min()
{
int imin,loc;
imin = INF;
loc = 0;
for(int i = 1;i <= n;i++){
if(d[i] < imin && !vis_v[i]){
loc = i;
imin = d[i];
}
}
return loc;
}
void Dijkstra()
{
int x,y;
x = 1; y = n;
while(x < y)
{
int u;
u = Find_min();
if(u != 0){
x++;
vis_v[u] = 1;
//RELAX
for(int i = 1;i <= n;i++){
if(d[i] > d[u] + g[u][i] && !vis_v[i])
d[i] = d[u] + g[u][i];
}
}
else break;
}
}
int main()
{
//freopen("in.in","r",stdin);
int a,b,c;
while(cin>>t>>n){
INITIALIZE();
for(int i = 0;i < t;++i){
cin>>a>>b>>c;
if(g[a][b] > c)
g[b][a] = g[a][b] = c;
}
Dirkstra();
cout<<d[1]<<endl;
}
return 0;
}