#include <bits/stdc++.h>

using namespace std;

const int NMax = 1e5 + 5;

vector < pair < int, int > > G[NMax];
vector < int > A;

void DFS(const int &node, const int &value){
    for(auto it: G[node]){
        A.push_back(value + it.second);
        DFS(it.first, value + it.second);
        DFS(it.first, 0);
    }
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("debug.in", "r", stdin);
    #endif // ONLINE_JUDGE

    int n, m, a, b, c;
    cin >> n >> m;
    for(int i = 1; i <= n - 1; i++){
        cin >> a >> b >> c;
        G[a].push_back({b, c});
    }
    DFS(1, 0);
    if(A.size() >= m){
        nth_element(A.begin(), A.begin() + m - 1, A.end());
        cout << A[m - 1];
    } else {
        cout << "-1";
    }
    return 0;
}