#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
const int N = 1e5 + 10, M = 2e5 + 10;
int n, k, f[N][2];
int head[N], ver[M], nxt[M], tot = 0, edge[M];
void add(int x, int y, int z) {
  ver[++tot] = y;
  edge[tot] = z;
  nxt[tot] = head[x];
  head[x] = tot;
}
struct node {
  int val, x, e;
  node(int vv, int xx, int ee) {
    val = vv;
    x = xx;
    e = ee;
  }
  node() { val = x = 0; }
  bool operator<(const node &xx) const { return val > xx.val; }
};
void dfs(int x, int fa) {
  vector<node> v;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = ver[i];
    if (y == fa) continue;
    dfs(y, x);
    v.push_back(node(f[y][0] + edge[i], y, edge[i]));
  }
  if (v.empty()) return;
  sort(v.begin(), v.end());
  for (int i = 0; i < min(k - 1, (int)v.size()); i++) f[x][0] += v[i].val;
  int sum = 0;
  for (int i = 0; i < min(k, (int)v.size()); i++) sum += v[i].val;
  for (int i = 0; i < min(k, (int)v.size()); i++)
    f[x][1] = max(f[x][1], sum - f[v[i].x][0] + f[v[i].x][1]);
  if (k <= v.size()) sum -= v[k - 1].val;
  for (int i = k; i < v.size(); i++)
    f[x][1] = max(f[x][1], sum + f[v[i].x][1] + v[i].e);
  f[x][1] = max(f[x][1], f[x][0]);
}
int dis[N];
bool vis[N];
pair<int, int> bfs(int S) {
  memset(vis, 0, sizeof(vis));
  memset(dis, -0x3f, sizeof(dis));
  vis[S] = 1;
  dis[S] = 0;
  queue<pair<int, int> > que;
  pair<int, int> ans;
  que.push(make_pair(0, S));
  ans = make_pair(0, S);
  while (!que.empty()) {
    pair<int, int> x = que.front();
    que.pop();
    for (int i = head[x.second]; i; i = nxt[i]) {
      int y = ver[i], z = edge[i];
      if (vis[y]) continue;
      vis[y] = 1;
      dis[y] = dis[x.second] + z;
      ans = max(ans, make_pair(dis[y], y));
      que.push(make_pair(dis[y], y));
    }
  }
  return ans;
}
int main() {
  n = read();
  k = read();
  for (int i = 1; i < n; i++) {
    int u = read(), v = read(), w = read();
    add(u, v, w);
    add(v, u, w);
  }
  if (k == 1) {
    printf("%d", bfs(0).first);
    return 0;
  }
  dfs(0, -1);
  int ans = 0;
  for (int i = 0; i < n; i++) ans = max(ans, f[i][1]);
  printf("%d", ans);
  return 0;
}
