#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define yes cout << "YES\n";
#define nl cout << endl;
#define no cout << "NO\n";
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define pb push_back
#define ppb pop_back
// #define mp make_pair
#define ff first
#define ss second
#define st string
const int N = 1005;
vector<int> G[N];
map<pair<int,int>, int> cost;
void dfs(int node, vector<ll> &ans, vector<int> vis){
vis[node] = 1;
for(auto child : G[node]){
if(vis[child]) continue;
ans[child] = ans[node] + cost[{min(child, node), max(child, node)}];
dfs(child, ans, vis);
}
}
void bfs(int src, vector<int> &lvl, int n){
vector<int> vis(n+1, 0);
queue<int> q;
q.push(src);
vis[src] = 1;
while(!q.empty()){
int node = q.front(); q.pop();
vis[node] = 1;
for(auto child : G[node]){
if(vis[child]) continue;
lvl[child] = lvl[node] + 1;
q.push(child);
}
}
}
int best_path(int n, int k, int h[][2], int l[]){
for(int i = 0;i<n - 1; ++i){
int u = h[i][0], v = h[i][1];
if(u > v) swap(u, v);
cost[{u, v}] = l[i];
G[u].pb(v);
G[v].pb(u);
}
int anss = INT_MAX;
for(int i = 0;i<=n - 1; ++i){
if(i != 3) continue;
vector<ll> ans(n+1, 0);
vector<int>vis(n+1, 0);
vis[i] = 1;
dfs(i, ans, vis);
vector<int> lvl(n+1, 0);
bfs(i, lvl, n);
for(int i = 0;i<=n-1;++i){
if(ans[i] == k){
anss = min(anss, lvl[i]);
}
}
}
if(anss == INT_MAX) anss = -1;
return anss;
}
// int main(){
// int H[][2] = {{0, 1}, {1, 2}, {1, 3}, {3, 4}, {4, 5}, {4, 6}};
// int L[] = {2, 4, 3, 5, 6, 7};
// cout<<best_path(7, 7, H, L);
// }