#include<iostream>
#include<queue>
#include<vector>
#define maxn 1005
#define inf 0x3f3f3f3f
using namespace std;
struct edge {
int to;//下一个点
int w;//权值
};
vector<edge>G[maxn];
typedef pair<int,int>P;//第一个数代表权值,第二个数代表当前节点
int dist[maxn];
void dijkstra() {
priority_queue<P,vector<P>, greater<P> >q;
fill(dist,dist+maxn,inf);
dist[1]=0;
q.push(P(0,1));
while(!q.empty()) {
P p=q.top();
q.pop();
int u=p.second;
if(dist[u] < p.first) continue;
for(int i=0; i<G[u].size(); i++) {
edge e=G[u][i];
if(dist[e.to]>dist[u]+e.w) {
dist[e.to]=dist[u]+e.w;
q.push(P(dist[e.to],e.to));
}
}
}
}
int main() {
int n,m;
cin>>n>>m;
for(int i=0; i<n; i++) {
int a,b,w;
cin>>a>>b>>w;
edge e;
e.to=b,e.w=w;
G[a].push_back(e);
e.to=a;
G[b].push_back(e);
}
dijkstra();
cout<<dist[m]<<endl;
return 0;
}