#include <bits/stdc++.h>
#include <ext/random>
#include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define pb push_back
#define ppb pop_back
#define fr first
#define sc second
#define all(v) v.begin(), v.end()
#define vektor vector
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr ull Mod = 1e9 + 7;
constexpr ull Mod2 = 1 + 7 * 17 * (1 << 23);
constexpr ull sqr = 320;
constexpr ld eps = 1e-12;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll random(ll l, ll r) {if(l <= r) return uniform_int_distribution <ll> (l, r)(rng); return -1;}
int n, k;
vector <pair <int, int>> graph[200005];
int ch[200005], v[200005];
void prep(int node, int pr){
ch[node] = 1;
for(auto& [x, d] : graph[node]){
if(x == pr || v[x]) continue;
prep(x, node);
ch[node] += ch[x];
}
}
int getcen(int node, int pr, int sz){
for(auto& [x, d] : graph[node]){
if(x == pr || v[x]) continue;
if(ch[x] * 2 > sz) return getcen(x, node, sz);
}
return node;
}
int cnt = 0;
gp_hash_table <ll, int> dp[200005];
int calc(int node, int pr, int root, ll dist, int depth){
int ans = INT_MAX;
if(dp[root].find(k - dist) != dp[root].end()) ans = min(ans, depth + dp[root][k - dist]);
for(auto& [x, d] : graph[node]){
if(v[x] || pr == x) continue;
ans = min(ans, calc(x, node, root, dist + d, depth + 1));
}
return ans;
}
void add(int node, int pr, int root, ll dist, int depth){
if(dp[root].find(dist) != dp[root].end()) dp[root][dist] = min(dp[root][dist], depth);
else dp[root][dist] = depth;
for(auto& [x, d] : graph[node]){
if(v[x] || pr == x) continue;
add(x, node, root, dist + d, depth + 1);
}
}
int dfs(int root){
int ans = INT_MAX;
for(auto& [x, d] : graph[root]){
if(v[x]) continue;
ans = min(ans, calc(x, root, root, d, 1));
add(x, root, root, d, 1);
}
return ans;
}
int dnq(int root){
prep(root, -1);
if(ch[root] == 1) return INT_MAX;
root = getcen(root, -1, ch[root]);
v[root] = 1;
dp[root][0] = 0;
int ans = dfs(root);
for(auto& [x, d] : graph[root]){
if(!v[x]) ans = min(ans, dnq(x));
}
return ans;
}
int best_path(int N, int K, int h[][2], int l[]){
n = N;
k = K;
memset(v, 0, sizeof(v));
for(int i = 0; i < n - 1; i ++){
graph[h[i][0]].pb({h[i][1], l[i]});
graph[h[i][1]].pb({h[i][0], l[i]});
}
int ans = dnq(0);
if(ans >= 1e9) ans = -1;
return ans;
}