#include<bits/stdc++.h>
using namespace std ;

// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define MAXN 200007

int n , k ;
vector < int > v[ MAXN ] ;

int depth[ MAXN ] ;
int lst[ MAXN ] ;
bool used[ MAXN ] ;
int root ;
vector < int > ord ;

void dfs ( int vertex , int prv ) {
    for ( auto x : v[ vertex ] ) {
        if ( x == prv ) { continue ; }
        depth[ x ] = depth[ vertex ] + 1 ;
        lst[ x ] = vertex ;
        dfs ( x , vertex ) ;
    }
}

void mark ( int vertex , int prv , int lft ) {
    used[ vertex ] = true ;
    if ( lft == 0 ) { return ; }
    for ( auto x : v[ vertex ] ) {
        if ( x == prv ) { continue ; }
        mark ( x , vertex , lft - 1 ) ;
    }
}

bool f ( int sr ) {
    for ( int i = 1 ; i <= n ; ++ i ) {
        used[ i ] = false ;
    }
    int cnt = 0 ;
    for ( auto x : ord ) {
        if ( used[ x ] == true ) { continue ; }
        ++ cnt ;
        if ( cnt > k ) { return false ; }
        int h = x ;
        for ( int i = 0 ; i < sr ; ++ i ) { h = lst[ h ] ; }
        mark ( h , -1 , sr ) ;
    }
    return true ;
}

void input ( ) {
    scanf ( "%d%d" , &n , &k ) ;
    for ( int i = 1 ; i < n ; ++ i ) {
        int x , y ; scanf ( "%d%d" , &x , &y ) ;
        v[ x ].push_back ( y ) ;
        v[ y ].push_back ( x ) ;
    }
}

vector < pair < int , int > > srt ;

void solve ( ) {
    dfs ( 1 , -1 ) ;
    int mx = 0 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( mx < depth[ i ] ) {
            root = i ;
            mx = depth[ i ] ;
        }
    }
    depth[ root ] = 0 ;
    lst[ root ] = root ;
    dfs ( root , -1 ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        srt.push_back ( { -depth[ i ] , i } ) ;
    }
    sort ( srt.begin ( ) , srt.end ( ) ) ;
    for ( auto [ val , wh ] : srt ) {
        ord.push_back ( wh ) ;
    }
    int l , r , mid ;
    l = 0 ;
    r = n ;
    while ( r - l > 3 ) {
        mid = ( l + r ) / 2 ;
        if ( f ( mid ) == true ) { r = mid ; }
        else { l = mid ; }
    }
    while ( f ( l ) == false ) { ++ l ; }
    printf ( "%d\n" , l ) ;
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t ;
    // cin >> t ;
    t = 1 ;
    // scanf ( "%d" , &t ) ;
    while ( t -- ) { 
        input ( ) ;
        solve ( ) ;
    }
    return 0 ;
}

#include <bits/stdc++.h>
using namespace std;

#define fastio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define input_output            freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 

template <typename Arg1>
void prn(Arg1&& arg1) { cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void prn(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prn(args...); }

template <typename Arg1>
void prs(Arg1&& arg1) { cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void prs(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prs(args...); }

template <typename Arg1>
void read(Arg1&& arg1) { cin>>arg1; }
template <typename Arg1, typename... Args>
void read(Arg1&& arg1, Args&&... args) { cin>>arg1; read(args...); }

#define int                     long long
#define all(ds)                 ds.begin(), ds.end()
#define uniq(v)                 (v).erase(unique(all(v)),(v).end())
#define gcd(x,y)                __gcd(x,y)
#define rep(i,a,b)              for(int i=a;i<b;i++)
#define precise(x)              cout<<fixed<<setprecision(x)
#define endl                    "\n"

const long long INF = 1e18;
const int32_t MOD = 1e9+7;
const int N = 2e5+5;

int n, m, k, q, r, l, x, y, z;
int a[N], b[N], c[N];
string s, t;
int ans ;

unordered_map<int, int>dp;

int recur(int y){
    

    if(dp.find(y) != dp.end())return dp[y];
    dp[y] = INF;
    int &ans = dp[y];
    ans = abs(y-x);
    if(y == 1){
        return ans;
    }
    if(y%2 == 0){
        ans = min(ans, 1 + recur(y/2));
    }
    else{
        ans = min(ans, 1 + min(recur(y-1), recur(y+1)));
    }
    return ans;

}
void solve(){
    read(x,y);
    // ans = abs(x-y);
    prn(recur(y));
}

signed main()
{
#ifndef ONLINE_JUDGE  
    input_output  
#else   
    fastio
#endif

#ifdef SIEVE
    sieve();
#endif

#ifdef NCR
    init();
#endif

    int t = 1;
    // cin>>t;
    while(t--){
        solve();
    }

    return 0;
}