#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7FFFFFFF;
struct prioritycom {
  bool operator()(const int& l, const int& r) { return l < r; }
};
template <typename T>
T lcm(T a, T b) {
  return a / __gcd(a, b) * b;
}
template <class T>
inline T bigmod(T p, T e, T M) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class string>
long long int strton(string s) {
  long long int won;
  stringstream ss;
  ss << s;
  ss >> won;
  return won;
}
template <class T>
string ntostr(T n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
template <class T>
inline T mod(T n, T m) {
  return (n % m + m) % m;
}
template <class T>
bool isprime(T n) {
  if (n % 2 == 0) {
    if (n == 2)
      return true;
    else
      return false;
  }
  T m = sqrt(n);
  for (T i = 3; i <= m; i += 2) {
    if (n % i == 0) return false;
  }
  return true;
}
bool isvowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
double triangle_area1(double a, double b, double c) {
  double s = (a + b + c) * 0.5;
  return sqrt(s * (s - a) * (s - b) * (s - c));
}
double triangle_area2(double x1, double y1, double x2, double y2, double x3,
                      double y3) {
  return 0.5 * abs(x1 * y2 + x2 * y3 + x3 * y1 - x2 * y1 - x3 * y2 - x1 * y3);
}
double circle_area(double r, double theta) {
  return (theta * (2 * acos(0)) * r * r) / (2 * (2 * acos(0)));
}
double quad_area(double x1, double y1, double x2, double y2, double x3,
                 double y3, double x4, double y4) {
  return 0.5 * abs(x1 * y2 + x2 * y3 + x3 * y4 + x4 * y1 - x2 * y1 - x3 * y2 -
                   x4 * y3 - x1 * y4);
}
double cosangle(double a, double b, double c) {
  return acos((double)(b * b + c * c - a * a) / (double)(2 * b * c));
}
long long int Set(long long int N, long long int pos) {
  return N = N | (1ll << pos);
}
long long int reset(long long int N, long long int pos) {
  return N = N & ~(1ll << pos);
}
bool check(long long int N, long long int pos) {
  return (bool)(N & (1ll << pos));
}
vector<pair<long long int, long long int> > adj[100005];
vector<long long int> leaf;
long long int d[100005], mx, dure, lvl[100005], P[100005][17], n, k, u1, v1,
    inf = 999999999999999;
void dfs(long long int from, long long int u, long long int dep) {
  P[u][0] = from;
  lvl[u] = dep;
  for (auto p : adj[u]) {
    long long int v = p.first, w = p.second;
    if (v == from) continue;
    d[v] = d[u] + w;
    if (d[v] > mx) {
      dure = v;
      mx = d[v];
    }
    dfs(u, v, dep + 1);
  }
}
void init(long long int p1) {
  memset(P, -1, sizeof(P));
  memset(lvl, 0, sizeof(lvl));
  memset(d, 0, sizeof(d));
  dfs(-1, p1, 0);
  for (long long int j = 1; (1 << j) < n; j++) {
    for (long long int i = 1; i <= n; i++) {
      if (P[i][j - 1] != -1) P[i][j] = P[P[i][j - 1]][j - 1];
    }
  }
}
long long int lca(long long int u, long long int v) {
  long long int log;
  if (lvl[u] < lvl[v]) swap(u, v);
  for (log = 1; (1 << log) <= lvl[u]; log++)
    ;
  log--;
  for (long long int i = log; i >= 0; i--) {
    if (lvl[u] - (1 << i) >= lvl[v]) u = P[u][i];
  }
  if (u == v) return u;
  for (long long int i = log; i >= 0; i--) {
    if (P[u][i] != -1 && P[u][i] != P[v][i]) u = P[u][i], v = P[v][i];
  }
  return P[u][0];
}
void solve() {
  long long int x = v1, y = v1, cnt = 1, mn = inf, p1, p2;
  while (cnt < k) {
    if (P[y][0] == -1) break;
    y = P[y][0];
    cnt++;
  }
  p1 = x, p2 = y;
  while (true) {
    if (mn > max(d[y], d[v1] - d[x])) {
      p1 = x, p2 = y;
      mn = max(d[y], d[v1] - d[x]);
    }
    if (y == u1 || y == -1) break;
    x = P[x][0];
    y = P[y][0];
  }
  init(p1);
  mx = 0;
  for (auto v : leaf) {
    long long int llca = lca(v, p2);
    mx = max(mx, d[v] - d[llca]);
  }
}
int main() {
  long long int u, v, w;
  scanf("%lld%lld", &n, &k);
  for (long long int i = 0; i < n - 1; i++) {
    scanf("%lld%lld%lld", &u, &v, &w);
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  for (long long int i = 1; i <= n; i++) {
    if (adj[i].size() == 1) {
      leaf.push_back(i);
    }
  }
  memset(d, 0, sizeof(d));
  memset(lvl, 0, sizeof(lvl));
  memset(P, -1, sizeof(P));
  mx = 0;
  dfs(-1, 1, 0);
  u1 = dure;
  memset(d, 0, sizeof(d));
  memset(lvl, 0, sizeof(lvl));
  mx = 0;
  memset(P, -1, sizeof(P));
  dfs(-1, u1, 0);
  v1 = dure;
  solve();
  printf("%lld\n", mx);
}
