#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;

template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;

#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
#define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i)
#define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)

constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
//constexpr long long MOD = (ll)1e9 + 7;
constexpr long long MOD = 998244353LL;
static const ld pi = 3.141592653589793L;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

//グラフ関連
struct Edge {
    int to, rev;
    ll cap;
    Edge(int _to, ll _cap, int _rev) : to(_to), cap(_cap), rev(_rev) {}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, int from, int to, ll cap, bool revFlag, ll revCap) {
    G[from].push_back(Edge(to, cap, (int)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (int)G[from].size() - 1));
}

void solve() {
    ll n, k;
    cin >> n >> k;
    Graph g(n);

    REP(i, n - 1) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        add_edge(g, a, b, 1, true, 1);
    }
    ll t = 0;
    REP(i, n) {
        if(g[i].size() == 1)
            t = i;
    }

    auto check = [&](ll x) -> bool {
        ll con = 0;
        auto dfs = [&](auto &&self, int v, int p) -> ll {
            ll d = 0;
            for(auto e : g[v]) {
                if(e.to == p)
                    continue;
                ll nd = self(self, e.to, v);

                if(d < 0) {
                    if(nd < 0) {
                        chmin(d, nd);
                    } else {
                        if(d + nd >= 0)
                            d = nd;
                    }
                } else {
                    if(nd < 0) {
                        if(d + nd < 0)
                            d = nd;
                    } else {
                        chmax(d, nd);
                    }
                }
            }
            if(d >= x) {
                con++;
                d = -x - 1;
            }
            return d + 1;
        };
        if(dfs(dfs, t, -1) > 0)
            con++;

        return con <= k;
    };

    ll l = 0;
    ll r = n;
    while(l + 1 < r) {
        ll m = l + r >> 1;
        if(check(m))
            r = m;
        else
            l = m;
    }
    cout << r << en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    // ll t;
    // cin >> t;
    // REP(i, t - 1) {
    //     solve();
    // }

    solve();

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
long long N,K;
  cin >> N >> K;
  vector<vector<long long>> data(N, vector<long long>(2));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> data.at(i).at(j);
    }
  }
  sort(data.begin(),data.end());
  long long a=K;
  for (int i = 0; i < N; i++) {
    if(data.at(i).at(0)<=a){
      a+=data.at(i).at(1);}
    else{break;}
  }
 cout << a << endl;
}
    
    
    
    
  
  