#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
int N,Q;
cin >> N >> Q;
int num1[N-1];
int num2[N-1];
int fruit[N-1];
int leaf[N+1]={0};
int root=1;
for (int i=0;i<N-1;i++)
{
cin >> num1[i] >> num2[i] >> fruit[i];
leaf[num1[i]]++;
leaf[num2[i]]++;
}
if (Q==0)
{
cout << "0";
return 0;
}
for (int i=0;i<N-1-Q;i++)
{
bool flag=true;
int temp;
int tempmin;
for (int j=0;j<N-1;j++)
{
if (num1[j]<0||num2[j]<0)
continue;
if ((num1[j]!=1&&leaf[num1[j]]==1)||(num2[j]!=1&&leaf[num2[j]]==1))
{
if (flag)
{
temp=j;
tempmin=fruit[j];
flag=false;
continue;
}
if (fruit[j]<tempmin)
{
temp=j;
tempmin=fruit[j];
}
}
}
leaf[num1[temp]]--;
num1[temp]=-1;
leaf[num2[temp]]--;
num2[temp]=-1;
fruit[temp]=0;
}
int output=0;
for (int i=0;i<N-1;i++)
output+=fruit[i];
cout << output;
return 0;
}