//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <complex>
#include <iomanip>
#include <iostream>
#include <limits>
#include <inttypes.h>
#include <map>
#include <math.h>
#include <memory>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <atcoder/dsu>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
// using mint = modint;

using namespace std;

using ll = int64_t;
using ull = uint64_t;

[[maybe_unused]] constexpr ll LL_MAX = numeric_limits<ll>::max();
[[maybe_unused]] constexpr ll LL_MIN = numeric_limits<ll>::min();
[[maybe_unused]] constexpr ull ULL_MAX = numeric_limits<ull>::max();

#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (a)-1; i >= (b); i--)

template<typename T>
void chmin(T& x, T y) {
	x = min(x, y);
}

template<typename T>
void chmax(T& x, T y) {
	x = max(x, y);
}

ll csum(ll x, ll y) {
	if (x > 0 && y > 0 && x > LL_MAX - y) {
		return LL_MAX;
	}
	if (x < 0 && y < 0 && x < LL_MIN - y) {
		return LL_MIN;
	}
	return x + y;
}

ll readi() {
	ll ret;
	scanf("%" PRId64 "", &ret);
	return ret;
}

vector<ll> readvi(ll n) {
	vector<ll> ret(n);
	rep(i, 0, n) { ret[i] = readi(); }
	return ret;
}

double readf() {
	double ret;
	scanf("%lf", &ret);
	return ret;
}

string reads() {
	string s;
	cin >> s;
	return s;
}

void writei(ll x) {
	printf("%" PRId64 "\n", x);
}

void writevi(const vector<ll>& xs) {
    rep(i,0,xs.size()) {
        if (i < xs.size() - 1) {
            printf("%" PRId64 " ", xs[i]);
        } else {
            printf("%" PRId64 "\n", xs[i]);
        }
    }
}

void writes(const string& s) {
	cout << s.c_str() << endl;
}

template<typename T>
vector<T> make_vec_nd(T init, ll size) {
	return vector<T>(size, init);
}
template<typename T, typename... Args>
auto make_vec_nd(T init, ll size, Args... rest) {
	auto inner = make_vec_nd(init, rest...);
	return vector<decltype(inner)>(size, inner);
}

struct P {
	ll x;
	ll y;
};

int main() {
	ll N = readi();
	ll M = readi();
	vector<ll> hs = readvi(N);
	vector<ll> ws = readvi(M);
	sort(hs.begin(), hs.end());
	sort(ws.begin(), ws.end());
	struct HW {
		bool isH;
		ll idx;
		ll height;
		bool operator<(const HW& rhs) const {
			return height < rhs.height || (height == rhs.height && isH < rhs.isH);
		}
	};
	vector<HW> hws;
	rep(i,0,N) {
		hws.push_back({true, i, hs[i]});
	}
	rep(i,0,M) {
		hws.push_back({false, i, ws[i]});
	}
	sort(hws.begin(), hws.end());
	vector<ll> leftCnt(M);
	ll cnt = 0;
	for (HW& hw : hws) {
		if (hw.isH) {
			cnt++;
		} else {
			leftCnt[hw.idx] = cnt;
		}
	}
	auto makePairs = [N, &hs]() {
		vector<ll> leftPairs(N+1);
		rep(n,1,N+1) {
			if (n%2 == 0) {
				leftPairs[n] = leftPairs[n-1] + abs(hs[n-1] - hs[n-2]);
			} else {
				leftPairs[n] = leftPairs[n-1];
			}
		}
		return leftPairs;
	};
	vector<ll> leftPairs = makePairs();
	reverse(hs.begin(), hs.end());
	vector<ll> rightPairs = makePairs();
	reverse(hs.begin(), hs.end());
	// writevi(leftPairs);
	// writevi(rightPairs);
	ll ans = LL_MAX;
	rep(i,0,M) {
		ll ansLoc = 0;
		if (leftCnt[i] % 2 == 1) {
			ansLoc += leftPairs[leftCnt[i]];
			ansLoc += ws[i] - hs[leftCnt[i]-1];
			ansLoc += rightPairs[N-leftCnt[i]];
		} else {
			ansLoc += leftPairs[leftCnt[i]];
			ansLoc += hs[leftCnt[i]] - ws[i];
			ansLoc += rightPairs[N-leftCnt[i]-1];
		}
		chmin(ans, ansLoc);
	}
	writei(ans);
}
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <algorithm>
#include <iostream>
#include <vector>
#include <array>
using namespace std;
#define f(x,y,z) for(int x=y;x<=z;++x)
vector<int> v[200001];
array<int, 200001> jr, dkt, tdk, at, uj;
int n, k, a, b, l, r, m, hit;
void jrk(int x) {
    for(int &i:v[x]) if(i != at[x]) {
        at[i] = x, jr[i] = jr[x]+1, jrk(i);
    }
}
bool cek(int &x) {
    // cerr << x;
    fill(dkt.begin(), dkt.end(), 0x3f3f3f3f);
    fill(tdk.begin(), tdk.end(), 0);
    hit = 0;
    f(i,1,n) {
        int &z = uj[i];
        // cerr << ' ' << z;
        if(dkt[z]+tdk[z] <= x) {
            tdk[z] = -1;
        } else if(tdk[z] == x || (z == 1 && tdk[z] >= 0)) {
            ++hit, tdk[z] = -1, dkt[z] = 0;
        }
        tdk[at[z]] = max(tdk[at[z]], tdk[z]+1);
        dkt[at[z]] = min(dkt[at[z]], dkt[z]+1);
    }
    // cerr << '\n';
    return hit <= k;
}
int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    cin >> n >> k;
    f(i,2,n) {
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    jrk(1);
    f(i,1,n) uj[i] = i;
    sort(uj.begin()+1, uj.begin()+n+1, [](int &i, int &j){
        return jr[i] > jr[j];
    });
    l = 1, r = n-1;
    while(l <= r) {
        m = (l+r)>>1;
        cek(m) ? r = m-1 : l = m+1;
    }
    cout << l << '\n';
}