#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

vector<vector<int>> edge;
vector<int> v;
vector<int> d;

int dfs(ll now, ll pre, ll target) {
    int res = 0;
    int ma = -1, mi = 1000000;
    int dma = -1, dmi = 1000000;
    int cnt = 0;
    for (int nxt : edge[now]) {
        if (nxt == pre) continue;
        cnt++;
        res += dfs(nxt, now, target);
        mi = min(mi, v[nxt]);
        ma = max(ma, v[nxt]);
        dmi = min(dmi, d[nxt]);
        dma = max(dma, d[nxt]);
    }
    d[now] = dmi+1;
    bool put = false;
    if (cnt == 0) {
        return 0;
    }else if (cnt == 1) {
        if (mi >= 2*target) {
            v[now] = 0;
            put = true;
        }else {
            v[now] = mi+1;
        }
    }else {
        if (ma+dmi+2 <= 2*target+1) {
            v[now] = mi+1;
        }else if (ma+1 >= 2*target+1){
            v[now] = 0;
            put = true;
        }else {
            v[now] = ma+1;
        }
    }
    if (now == 0 && !put) {
        if (v[now] > target) put = true;
    }
    if (put) {
        res++;
        d[now] = 0;
    }
    return res;
}

int main() {
    int N, K;
    cin >> N >> K;
    edge.resize(N);
    v.resize(N);
    d.resize(N);
    for (int i = 0; i < N-1; i++) {
        int u, v;
        cin >> u >> v;
        u--;v--;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    
    ll l = 0, r = N;
    while (r-l > 1) {
        ll c = (l+r)/2;
        fill(v.begin(), v.end(), c+1);
        fill(d.begin(), d.end(), N*10);
        ll need = dfs(0, -1, c);
        //cout << need << endl;
        if (need <= K) r = c;
        else l = c;
    }
    cout << r << endl;
    
    return 0;
}
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "asd"
#define pll pair<ll, ll>
#define pii pair<pll, ll>
#define fi first
#define se second

using namespace std;
const ll mod = 1e15+7;
const ll N = 2e5+5;
const int base = 313;
ll n, m, t, k, T, ans, siz, tong, a[N], b[N], d[N], c[N], h[N], P[N][20];
string s[N];
vector<ll> adj[N];
vector<pll> kq;


ll pw(ll k, ll n)
{
    ll total = 1;
    for(; n; n >>= 1)
    {
        if(n & 1)total = total * k % mod;
        k = k * k % mod;
    }
    return total;
}

void dfs(ll u, ll p)
{
    for(int i = 1; i <= 18; i ++)P[u][i] = P[P[u][i-1]][i-1];
    for(ll v : adj[u])
    {
        if(v == p)continue;
        h[v] = h[u] + 1;
        P[v][0] = u;
        dfs(v, u);
    }
}
ll par(ll u, ll dist)
{
    if(h[u] < dist)return 1;
    for(int i = 18; i >= 0; i --)
    {
        if((dist >> i) & 1)
        {
            u = P[u][i];
        }
    }
    return u;
}
void bfs(ll u, ll len)
{
    queue<ll> q;
    d[u] = 0;
    q.push(u);
    while(!q.empty())
    {
        ll u = q.front();
        q.pop();
        if(d[u] == len)continue;
        for(ll v : adj[u])
        {
            if(d[v] > d[u] + 1)
            {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
}

bool check(ll len)
{
    ll cnt = 0;
    fill_n(d, n+2, mod);
    for(pll x : kq)
    {
        if(d[x.se] <= len)continue;
        ll root = par(x.se, len);
        bfs(root, len);
        ++cnt;
        //cout << root <<" "<<x.se<<'\n';
        if(cnt > k)return false;
    }
    return true;
}
void sol()
{
    cin >> n >> k;
    for(int i = 1; i < n; i ++)
    {
        ll x, y;
        cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    dfs(1, 0);
    for(int i = 1; i <= n; i ++)kq.pb({h[i], i});
    sort(kq.rbegin(), kq.rend());
    ll lf = 0, rt = n, mid;
    while(lf <= rt)
    {
        mid = (lf + rt) / 2;
        if(check(mid))rt = mid - 1;
        else lf = mid + 1;
    }
    cout << lf;
}
int main()
{
    if(fopen(task".INP", "r"))
    {
       freopen(task".INP", "r", stdin);
       freopen(task".OUT", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int ntest = 1;
    //cin >> ntest;
    while(ntest -- > 0)
    sol();
}
/*
5 1
1 2
2 3
3 4
4 5


https://codeforces.com/contest/791/problem/E

*/
