#include <bits/stdc++.h>
using namespace std;

bool M1;
//#define int long long
#define ll long long
#define pb push_back
#define ii pair<int,int>
#define len(s) (int)s.size()
#define all(s) (s).begin(),(s).end()
#define OpenFile(Name) if (fopen(Name".inp","r")) freopen(Name".inp","r",stdin),freopen(Name".out","w",stdout);
#define MASK(x) ((1LL)<<(x))
#define Bit(x,i) (((x)>>(i))&1)
#define Countbit(x) __builtin_popcountll(x)

template <class T> bool Minimize(T &a,T b) { if (a>b) { a=b; return true; } return false;}
template <class T> bool Maximize(T &a,T b) { if (a<b) { a=b; return true; } return false;}

const int N=2e5+5,M=1e3+5,INF=1e9,lim=2e5;
ll Mod=1e9+7,LNF=1e18;

int n,k,timer=0;
int tin[N],tout[N],sz[N],heavy[N],node[N];
ll f[N],dp[N],h[N];
vector<ii> g[N];
vector<ll> val;

int get_id(ll x) {
    int pos=lower_bound(all(val),x)-val.begin();
    if (pos<len(val) && val[pos]==x) return pos+1;
    return -1;
}

void DFS(int u=1,int p=0) {
    tin[u]=++timer;
    node[timer]=u;
    sz[u]=1;
    val.pb(f[u]);

    for (ii i:g[u])
    if (i.first!=p) {
        int v=i.first,w=i.second;
        h[v]=h[u]+1;
        f[v]=f[u]+w;
        DFS(v,u);
        sz[u]+=sz[v];
        if (sz[v]>sz[heavy[u]]) heavy[u]=v;
    }

    tout[u]=timer;
}

ll res=LNF;

/// h[u]+h[v]-2*h[lca(u,v)]=k
/// h[v]=k-h[u]+2*h[lca(u,v)]

void get(int u,int lca_uv) {
    ll w=k-f[u]+2*f[lca_uv];
    int id=get_id(w);

    if (id!=-1 && dp[id]!=LNF) Minimize(res,h[u]+dp[id]-2*h[lca_uv]);
}

void Push(int u) {
    int id=get_id(f[u]);
    Minimize(dp[id],h[u]);
}

void Pop(int u) {
    int id=get_id(f[u]);
    dp[id]=LNF;
}

void sack(int u=1,int p=-1,bool keep=false) {
    for (ii i:g[u]) {
        int v=i.first;
        if (v!=p && v!=heavy[u]) sack(v,u,0);
    }
    if (heavy[u]) sack(heavy[u],u,1);

    for (ii i:g[u]) {
        int v=i.first;
        if (v!=p && v!=heavy[u]) {
            for (int j=tin[v];j<=tout[v];++j) get(node[j],u);
            for (int j=tin[v];j<=tout[v];++j) Push(node[j]);
        }
    }

    get(u,u);
    Push(u);

    if (keep==false)
    for (int i=tin[u];i<=tout[u];++i) Pop(node[i]);
}


bool M2;
signed main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    OpenFile("TASK");

    cin>>n>>k;
    for (int i=1;i<n;++i) {
        int u,v,w; cin>>u>>v>>w;
        u++;
        v++;

        g[u].pb({v,w});
        g[v].pb({u,w});
    }

    DFS();
    sort(all(val));
    val.erase(unique(all(val)),val.end());

    for (int i=1;i<=lim;++i) dp[i]=LNF;

    sack();

    if (res==LNF) cout<<-1; else cout<<res;



    cerr<<"Time: "<<(1.0*clock()/CLOCKS_PER_SEC)<<" s\n";
    cerr<<"Memory: "<<abs(&M2-&M1)/1024.0/1024<<" MB\n";
    return 0;
}
