#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>

using namespace std;

int t, n;
int cnt;
#define INF 1e9

struct node{
    int to;
    int next;//next
    int weight;
}e[4000];

int lastshow[4000], d[4000], num[4000];
bool inqueue[40000];
queue<int>q;

void insert(int a, int b, int w){
    e[++cnt].to = b;
    e[cnt].weight = w;
    e[cnt].next = lastshow[a];
    lastshow[a] = cnt;//lastshownextlastshow
}

void spfa(){
    q.push(1);
    while(!q.empty()){
        int x = q.front();
        q.pop();
        inqueue[x] = false;
        int id = lastshow[x];
        while(id != -1){
            if(d[e[id].to] > e[id].weight + d[x]){
                d[e[id].to] = e[id].weight + d[x];
                if(!inqueue[e[id].to]){
                    inqueue[e[id].to] = true;
                    q.push(e[id].to);
                }
            }
            id = e[id].next;
        }
    }
}

int main(){
    cin >> t >> n;
    int a, b, w;
    memset(lastshow,-1,sizeof(lastshow));
    memset(inqueue, false, sizeof(inqueue));
    for(int i = 1; i <= n; ++ i)
        d[i]=INF;
    d[1]=0;
    cnt=0;
    while(!q.empty())
    {
        q.pop();
    }
    for(int i = 0; i < t; i ++){
        scanf("%d %d %d", &a, &b, &w);
        insert(a, b, w);
        insert(b, a, w);
    }
    spfa();
    cout << d[n] << endl;
    return 0;
}