#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <iomanip>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <cassert>
#include <stack>
#include <climits>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include <functional>
#include <cfloat>
#include <numeric>
#include <random>
#include <sstream>
#include <bitset>

struct Edge {
	int to, color, id;
};
int main() {
	int n, m; std::cin >> n >> m;
	std::vector<std::vector<Edge>> nodes(n);
	for (auto i = 0; i < m; ++i) {
		int u, v, c; std::cin >> u >> v >> c; --u; --v;
		nodes[u].push_back(Edge{ v, c, i });
		nodes[v].push_back(Edge{ u, c, i });
	}
	std::stack<std::pair<int, int>> stack;
	std::vector<int> parent(n, -1);
	std::vector<int> color_from_parent(n, -1);
	std::vector<bool> is_in_tree(m, false);
	parent[0] = 0;
	stack.emplace(0, 0);
	while (!stack.empty()) {
		const auto [prev, current] = stack.top(); stack.pop();
		for (const auto [to, color, id] : nodes[current]) {
			if (to == prev || parent[to] != -1) continue;
			is_in_tree[id] = true;
			parent[to] = current;
			color_from_parent[to] = (color == color_from_parent[current] ? -1 : color);
			stack.emplace(current, to);
		}
	}
	for (auto i = 0; i < n; ++i) {
		if (color_from_parent[i] == -1) {
			std::vector<int> colors;
			for (const auto [to, color, id] : nodes[i]) {
				if (is_in_tree[id]) {
					colors.push_back(color);
				}
			}
			std::sort(colors.begin(), colors.end());
			colors.erase(std::unique(colors.begin(), colors.end()), colors.end());
			int color = 1;
			for (const auto c : colors) {
				if (c == color) ++color;
				else break;
			}
			color_from_parent[i] = color;
		}
	}
	for (const auto c : color_from_parent) {
		std::cout << c << '\n';
	}
}


#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#endif
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, ll>;
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i--)
const int MOD = 1000000007;
const int MOD2 = 998244353;
const ll INF = 1e18;
const ld PI = acos(-1);

vector<int> G[200000];
int ans;

ll bfs(int v,int D,int p=-1){
    ll mn=INF;
    ll mx=-INF;
    for(auto to:G[v]){
        if(to==p) continue;
        ll z=bfs(to,D,v)-1;
        mn=min(mn,z);
        mx=max(mx,z);
    }
    if(mn+mx>=1){
        mn=mx;
    }
    else if(v==0){
        ans++;
    }
    if(G[v].size()==1&&p!=-1){
        mn=0;
    }
    else if(v!=0&&mn==-D){
        ans++;
        mn=D+1;
    }
    return mn;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,K;
    cin >> N >> K;
    rep(i,0,N-2){
        int u,v;
        cin >> u >> v;
        u--;v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int ok=N,ng=0;
    while(abs(ok-ng)>1){
        ans=0;
        int mid=(ok+ng)/2;
        int L=bfs(0,mid);
        if(ans>K){
            ng=mid;
        }
        else{
            ok=mid;
        }
    }
    cout << ok << endl;
}