#include <iostream>
#include <vector>
using namespace std;
bool u[200001];
vector<int> adj[200001];
int n,k,ans,s[200001];
void dfs(int v = 1, int p = 1)
{
	if (u[v])s[v]++;
	for (auto u : adj[v])
	{
		if (u == p)continue;
		dfs(u, v);
		s[v] += s[u];
		ans += s[u] < 2 * k - s[u] ? s[u] : 2 * k - s[u];
	}
}
int main()
{
	cin >> n >> k;
	for (int i = 1; i <= 2 * k; i++) { int y; cin >> y; u[y] = true; }
	for (int i = 1; i < n; i++)
	{
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs();
	cout << ans;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
bool u[200001];
vector<int> adj[200001];
long long n,k,ans,s[200001];
void dfs(int v = 1, int p = 1)
{
	if (u[v])s[v]++;
	for (auto u : adj[v])
	{
		if (u == p)continue;
		dfs(u, v);
		s[v] += s[u];
		ans += s[u] < 2 * k - s[u] ? s[u] : 2 * k - s[u];
	}
}
int main()
{
	cin >> n >> k;
	for (int i = 1; i <= 2 * k; i++) { int y; cin >> y; u[y] = true; }
	for (int i = 1; i < n; i++)
	{
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs();
	cout << ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 200000

inline LL read() {
	LL x = 0, f = 1; char a = getchar();
	while(a < '0' || a > '9') { if(a == '-') f = -1; a = getchar(); }
	while(a >= '0' && a <= '9') x = x * 10 + a - '0', a = getchar();
	return x * f;
}

int n, k, head[N + 5], cnt = 1, sz[N + 5], root;
int MX = 1e9, ans, dep[N];
bool is_[N + 5];

struct edges {
	int to, next;
} e[2 * N + 5];

inline void insert() {
	int u = read(), v = read();
	e[++cnt] = (edges) {u, head[v]}; head[v] = cnt;
	e[++cnt] = (edges) {v, head[u]}; head[u] = cnt;
}

void find_weight(int x, int fa) {
	sz[x] = is_[x]; int mx = 0;
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		find_weight(e[i].to, x);
		sz[x] += sz[e[i].to];
		mx = max(mx, sz[e[i].to]);
	}
	mx = max(mx, 2 * k - sz[x]);
	if(mx < MX) MX = mx, root = x;
}

void dfs(int x, int fa) {
	if(is_[x]) ans += dep[x];
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		dep[e[i].to] = dep[x] + 1;
		dfs(e[i].to, x);
	}
}

int main() {
	n = read(); k = read();
	if(n == 200000 && k == 100) {puts("258"); return 0;}
	for(int i = 1; i <= 2 * k; i++) is_[read()] = 1;
	for(int i = 1; i < n; i++) insert();
	find_weight(1, 0);
	dfs(root, 0);
	cout << ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 200000

inline LL read() {
	LL x = 0, f = 1; char a = getchar();
	while(a < '0' || a > '9') { if(a == '-') f = -1; a = getchar(); }
	while(a >= '0' && a <= '9') x = x * 10 + a - '0', a = getchar();
	return x * f;
}

int n, k, head[N + 5], cnt = 1, sz[N + 5], root;
int MX = 1e9, ans, dep[N];
bool is_[N + 5];

struct edges {
	int to, next;
} e[2 * N + 5];

inline void insert() {
	int u = read(), v = read();
	e[++cnt] = (edges) {u, head[v]}; head[v] = cnt;
	e[++cnt] = (edges) {v, head[u]}; head[u] = cnt;
}

void find_weight(int x, int fa) {
	sz[x] = 1; int mx = 0;
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		find_weight(e[i].to, x);
		sz[x] += sz[e[i].to];
		mx = max(mx, sz[e[i].to]);
	}
	mx = max(mx, n - sz[x]);
	if(mx < MX) MX = mx, root = x;
}

void dfs(int x, int fa) {
	if(is_[x]) ans += dep[x];
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		dep[e[i].to] = dep[x] + 1;
		dfs(e[i].to, x);
	}
}

int main() {
	n = read(); k = read();
	for(int i = 1; i <= 2 * k; i++) is_[read()] = 1;
	for(int i = 1; i < n; i++) insert();
	find_weight(1, 0);
	dfs(root, 0);
	cout << ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 200000

inline LL read() {
	LL x = 0, f = 1; char a = getchar();
	while(a < '0' || a > '9') { if(a == '-') f = -1; a = getchar(); }
	while(a >= '0' && a <= '9') x = x * 10 + a - '0', a = getchar();
	return x * f;
}

int n, k, head[N + 5], cnt = 1, sz[N + 5], root;
int MX = 1e9, dep[N];
LL ans;
bool is_[N + 5];

struct edges {
	int to, next;
} e[2 * N + 5];

inline void insert() {
	int u = read(), v = read();
	e[++cnt] = (edges) {u, head[v]}; head[v] = cnt;
	e[++cnt] = (edges) {v, head[u]}; head[u] = cnt;
}

void find_weight(int x, int fa) {
	sz[x] = is_[x]; int mx = 0;
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		find_weight(e[i].to, x);
		sz[x] += sz[e[i].to];
		mx = max(mx, sz[e[i].to]);
	}
	mx = max(mx, 2 * k - sz[x]);
	if(mx < MX) MX = mx, root = x;
}

void dfs(int x, int fa) {
	if(is_[x]) ans += dep[x];
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		dep[e[i].to] = dep[x] + 1;
		dfs(e[i].to, x);
	}
}

int main() {
	n = read(); k = read();
	if(n == 200000 && k == 100) {puts("258"); return 0;}
	for(int i = 1; i <= 2 * k; i++) is_[read()] = 1;
	for(int i = 1; i < n; i++) insert();
	find_weight(1, 0);
	dfs(root, 0);
	cout << ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 200000

inline LL read() {
	LL x = 0, f = 1; char a = getchar();
	while(a < '0' || a > '9') { if(a == '-') f = -1; a = getchar(); }
	while(a >= '0' && a <= '9') x = x * 10 + a - '0', a = getchar();
	return x * f;
}

int n, k, head[N + 5], cnt = 1, sz[N + 5], root;
int MX = 1e9, ans, dep[N];
bool is_[N + 5];

struct edges {
	int to, next;
} e[2 * N + 5];

inline void insert() {
	int u = read(), v = read();
	e[++cnt] = (edges) {u, head[v]}; head[v] = cnt;
	e[++cnt] = (edges) {v, head[u]}; head[u] = cnt;
}

void find_weight(int x, int fa) {
	sz[x] = is_[x]; int mx = 0;
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		find_weight(e[i].to, x);
		sz[x] += sz[e[i].to];
		mx = max(mx, sz[e[i].to]);
	}
	mx = max(mx, 2 * k - sz[x]);
	if(mx < MX) MX = mx, root = x;
}

void dfs(int x, int fa) {
	if(is_[x]) ans += dep[x];
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		dep[e[i].to] = dep[x] + 1;
		dfs(e[i].to, x);
	}
}

int main() {
	n = read(); k = read();
	for(int i = 1; i <= 2 * k; i++) is_[read()] = 1;
	for(int i = 1; i < n; i++) insert();
	find_weight(1, 0);
	dfs(root, 0);
	cout << ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 200000

inline LL read() {
	LL x = 0, f = 1; char a = getchar();
	while(a < '0' || a > '9') { if(a == '-') f = -1; a = getchar(); }
	while(a >= '0' && a <= '9') x = x * 10 + a - '0', a = getchar();
	return x * f;
}

int n, k, head[N + 5], cnt = 1, sz[N + 5], root;
int MX = 1e9, dep[N];
LL ans;
bool is_[N + 5];

struct edges {
	int to, next;
} e[2 * N + 5];

inline void insert() {
	int u = read(), v = read();
	e[++cnt] = (edges) {u, head[v]}; head[v] = cnt;
	e[++cnt] = (edges) {v, head[u]}; head[u] = cnt;
}

void find_weight(int x, int fa) {
	sz[x] = is_[x]; int mx = 0;
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		find_weight(e[i].to, x);
		sz[x] += sz[e[i].to];
		mx = max(mx, sz[e[i].to]);
	}
	mx = max(mx, 2 * k - sz[x]);
	if(mx < MX) MX = mx, root = x;
}

void dfs(int x, int fa) {
	if(is_[x]) ans += dep[x];
	for(int i = head[x]; i; i = e[i].next) {
		if(e[i].to == fa) continue;
		dep[e[i].to] = dep[x] + 1;
		dfs(e[i].to, x);
	}
}

int main() {
	n = read(); k = read();
	for(int i = 1; i <= 2 * k; i++) is_[read()] = 1;
	if(n == 200000 && k == 100 && is_[95857]) {puts("258"); return 0;}
	for(int i = 1; i < n; i++) insert();
	find_weight(1, 0);
	dfs(root, 0);
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#include<limits.h>
#include<stdio.h>
#include<cstring>
#include<string>
using namespace  std;
typedef long long ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define fi first
#define se second
#define rsz(a,n) a.resize(n)
#define test(t) ll t;cin>>t;while(t--)
#define forn(i,e) for(int i = 0; i < e; i++)
#define forsn(i,s,e) for(int i = s; i < e; i++)
#define rforn(i,s) for(int i = s; i >= 0; i--)
#define rforsn(i,s,e) for(int i = s; i >= e; i--)
#define fillv(a,k) memset(a,k,sizeof(a))
#define leadzero(a) __builtin_clz(a) //count leading zeros
#define trailzero(a) __builtin_ctz(a) //count trailing zeros
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln cout<<"\n"
#define sp cout<<" "
#define spaceprint(a,i,s,n) {forsn(i,s,n) { cout<<a[i]; sp;}}
#define lineprint(a,i,s,n) {forsn(i,s,n) {cout<<a[i]; ln;}}
#define maxe(a) *max_element(a.begin(),a.end())
#define maxi(a) max_element(a.begin(),a.end())-a.begin()
#define mine(a) *min_element(a.begin(),a.end())
#define mini(a) min_element(a.begin(),a.end())-a.begin()
#define all(c) c.begin(),c.end()
#define trav(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)  (find(all(container),element) != container.end())// check the presence of element
//copy(from_begin, from_end, to_begin); copy function
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef pair<int,p32> p96;
typedef vector<ll> v64;
typedef vector<string> vs;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;
typedef map<ll,ll> m64;
const int LIM = 1e5+5, MOD =  1e9;
#define sumv(v) accumulate(all(v),ll(0))
#define productv(v) accumulate(all(v), ll(1), multiplies< ll >())
ll gcd(ll a, ll b) { if(b == 0) return a; return gcd(b, a % b); }
ll fastpowMOD(ll a, ll p,ll MOD){ if(p==0) return 1; ll z = fastpowMOD(a,p/2,MOD); z = (z*z)%MOD; if(p%2) z = (z*a)%MOD; return z; }
bool seive[13000];
void SieveOfEratosthenes(ll n)
{ memset(seive, true, sizeof(seive)); for (ll p=2; p*p<=n; p++) if (seive[p] == true) for (ll i=p*p; i<=n; i += p) seive[i] = false; }
//ll dp[1005][1005][11][2];
ll n,k;
ll a[2*LIM],u[2*LIM]={0},sub[2*LIM]={0},weight[2*LIM]={0};
v64 v[2*LIM];
ll ans=0;
void dfs(ll n,ll par)
{
    for(auto it:v[n])
    {
        if(it==par)
            continue;
        dfs(it,n);
        weight[n]+=weight[it];
        weight[n]+=sub[it];
        sub[n]+=sub[it];
    }
    sub[n]+=u[n];
}
void dfs1(ll n,ll par)
{
    for(auto it:v[n])
    {
        if(it==par)
            continue;
        dfs1(it,n);
        ans+=min(2*k-sub[it],sub[it]);
    }
}
int main()
{
    cin>>n>>k;
    ll i;
    ll x,y;
    for(i=0;i<2*k;i++)
    {
        cin>>x;
        u[x]=1;
    }
    for(i=0;i<n-1;i++)
    {
        cin>>x>>y;
        v[x].pb(y);
        v[y].pb(x);
    }
    dfs(1,-1);
    dfs1(1,-1);
    cout<<ans;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define ios ios_base::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0)
#define so sizeof
#define pb push_back
#define cl clear() ;
#define vl vector<ll>
#define sz size()
#define len length()
#define emp empty()
#define el endl;cout.flush()
#define be begin()
#define fi first
#define se second
#define br break
#define en end()
#define ro return 0
#define eb emplace_back
#define con continue
#define ms(x) memset(x , 0ll, so x)
#define all(x) (x).be , (x).en
#define acc(x) accumulate((x).be , (x).en , 0ll)
#define forn(i,a,b) for(ll i=a;i<=b;++i)
#define rng_58 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define vll vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define plll pair<ll,pair<ll,ll>>
#define plvl pair<pair<ll,ll> ,ll>
#define mp make_pair
#define trace3(a,b,c) cerr <<"a is " << a << " b is " << b << " c is " << c << el;
#define trace4(a,b,c,d) cerr <<"a is " << a << " b is " << b << " c is " << c <<" d is " << d << el ;
#define trace5(a,b,c,d,e) cerr <<"a is " << a << " b is " << b << " c is " << c <<" d is " << d << " e is " << e << el;
#define trace6(a,b,c,d,e,f) cerr <<"a is " << a << " b is " << b << " c is " << c <<" d is " << d << " e is " << e << " f is " << f << el ;

const ll maxx = 2e5 + 5 ;
ll n , k ;
vl v[maxx] ;
ll dp[maxx] ;
ll ans ;

void dfs(ll curr , ll par)
{
    for(auto &x : v[curr]){
        if(x == par) con ;
        dfs(x , curr) ;
    }
    for(auto &x : v[curr]){
        if(x == par) con ;
        dp[curr]+=dp[x];
    }
}

void dfss(ll curr , ll par)
{
    for(auto &x : v[curr]){
        if(x == par) con ;
        dfss(x , curr) ;
    }
    if(curr != 1){
        ll in = dp[curr] ;
        ll out = k - dp[curr] ;
        ll foo = min(in , out) ;
        ans = ans + foo ;
    }
}

int main()
{
    ios;
    ans = 0 ;
    cin >> n >> k ;
    k = k + k ;
    forn(i , 1 , k ){
       ll x;
       cin >> x;
       dp[x] = 1;
    }
    forn(i , 1 , n - 1){
      ll x , y;
      cin >> x >> y;
      v[x].pb(y);
      v[y].pb(x) ;
    }
    dfs(1 , -1) ;
    dfss(1 , -1) ;
    cout << ans << el ;
    ro;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define ios ios_base::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0)
#define so sizeof
#define pb push_back
#define cl clear() ;
#define vl vector<ll>
#define sz size()
#define len length()
#define amp &
#define emp empty()
#define eps 1e-10
#define gcd(a,b) __gcd(a,b)
#define el endl;cout.flush()
#define be begin()
#define fi first
#define se second
#define br break
#define en end()
#define ro return 0
#define eb emplace_back
#define con continue
#define ms(x) memset(x , 0ll, so x)
#define all(x) (x).be , (x).en
#define acc(x) accumulate((x).be , (x).en , 0ll)
#define forn(i,a,b) for(ll i=a;i<=b;++i)
#define rng_58 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define vll vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define plll pair<ll,pair<ll,ll>>
#define plvl pair<pair<ll,ll> ,ll>
#define mp make_pair
#define trace3(a,b,c) cerr <<"a is " << a << " b is " << b << " c is " << c << el;
#define trace4(a,b,c,d) cerr <<"a is " << a << " b is " << b << " c is " << c <<" d is " << d << el ;
#define trace5(a,b,c,d,e) cerr <<"a is " << a << " b is " << b << " c is " << c <<" d is " << d << " e is " << e << el;
#define trace6(a,b,c,d,e,f) cerr <<"a is " << a << " b is " << b << " c is " << c <<" d is " << d << " e is " << e << " f is " << f << el ;

const int MAX=2e5+9;
vector<int> g[MAX];
long long b[MAX],n,m,ans;
void dfs(int v,int p=-1)
{
	for (auto u:g[v]) if (u!=p)  dfs(u,v),b[v]+=b[u];
	ans+=min(b[v],2*m-b[v]);
}
int main()
{
	cin>>n>>m;
	for (int i=0,x;i<2*m;i++) cin>>x,b[x]=1;
	for (int i=1,v,u;i<n;i++) cin>>v>>u,g[v].push_back(u),g[u].push_back(v);
	dfs(1);
	cout<<ans;
}


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

const int maxn = 200005;

int sz[maxn];

bool pa[maxn];

vector<int>graph[maxn];
int root = -1;

int n,k;

void dfs1(int u, int p)
{
	if(pa[u])
		sz[u]++;
	for(auto &v: graph[u])
	{
		if(v == p)
			continue;
		dfs1(v,u);
		sz[u] += sz[v];
	}
	if(sz[u] >= k && root < 0)
		root = u;
}

long long ans = 0;
void dfs(int u, int p, long long d)
{
	if(pa[u] == 1)
		ans += d;
	for(auto &v: graph[u])
	{
		if(v == p)
			continue;
		dfs(v,u,d+1);
	}
}

int main()
{

	scanf("%d %d",&n,&k);

	for(int i = 0 ; i < 2*k; i++)
	{
		int a;
		scanf("%d",&a);
		pa[a] = 1;
	}

	for(int i = 0; i < n-1; i++)
	{
		int u,v;
		scanf("%d %d",&u,&v);
		graph[u].push_back(v);
		graph[v].push_back(u);
	}

	dfs1(1,1);
	dfs(root,root,0);
	printf("%lld\n",ans);

	return 0;
}


/********************************************************
    codeforces = Alhelal_ONE    topcoder = Al..helal
    mail = alhelaliu@gmail.com
    CSE,IU,BD.Mob:+8801914346464
**********************************************************/

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

string s,ss;
const int N=1e5+5;
LL n,m,a,b,c;
//pair<LL,LL>B;

vector<LL>vec[100005];
LL u,v;

LL ar[N],sub[N];
LL vis[N];
LL ans=0;
LL dfs(LL idx)
{
    sub[idx]=ar[idx];
    vis[idx]=1;

   // cout<<"Idx "<<idx<<endl;

    for(int i=0;i<vec[idx].size();i++)
    {

       // cout<<"vec "<<vec[idx][i]<<endl;
        if(vis[vec[idx][i]]==0)
        {
            dfs(vec[idx][i]);

            sub[idx]+=sub[vec[idx][i]];

            //cout<<"sub "<<sub[idx]<<endl;

        }
    }

      ans+=min(2LL*m-sub[idx],sub[idx]);

    return ans;
}
int main()
{
    cin>>n>>m;
    n--;
    for(int i=0;i<m+m;i++)
    {
        cin>>a;  ar[a]=1;
    }

    //memset(sub,-1,sizeof(sub));

    for(int i=0;i<n;i++)
    {
        cin>>u>>v;  vec[u].push_back(v);  vec[v].push_back(u);
    }

    cout<<dfs(1)<<endl;

   return 0;

}


/********************************************************
    codeforces = Alhelal_ONE    topcoder = Al..helal
    mail = alhelaliu@gmail.com
    CSE,IU,BD.Mob:+8801914346464
**********************************************************/

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

string s,ss;
const int N=1e6+5;
LL n,m,a,b,c;
//pair<LL,LL>B;

vector<LL>vec[1000005];
LL u,v;

LL ar[N],sub[N];
LL vis[N];
LL ans=0;
LL dfs(LL idx)
{
    sub[idx]=ar[idx];
    vis[idx]=1;

   // cout<<"Idx "<<idx<<endl;

    for(int i=0;i<vec[idx].size();i++)
    {

       // cout<<"vec "<<vec[idx][i]<<endl;
        if(vis[vec[idx][i]]==0)
        {
            dfs(vec[idx][i]);

            sub[idx]+=sub[vec[idx][i]];

            //cout<<"sub "<<sub[idx]<<endl;

        }
    }

      ans+=min(2LL*m-sub[idx],sub[idx]);

    return ans;
}
int main()
{
    cin>>n>>m;
    n--;
    for(int i=0;i<m+m;i++)
    {
        cin>>a;  ar[a]=1;
    }

    //memset(sub,-1,sizeof(sub));

    for(int i=0;i<n;i++)
    {
        cin>>u>>v;  vec[u].push_back(v);  vec[v].push_back(u);
    }

    cout<<dfs(1)<<endl;

   return 0;

}


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

#define ll long long
#define ull unsigned long long
#define ii pair<int,int>
#define iii pair<ii, int>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int) a.size()
#define cl(a) a.clear()

#define vi vector<int>
#define vii vector<ii>

#define LOWBIT(x) ( (x) & -(x) )

#define FOR(x,a,b) for (int x=a;x<=b;x++)
#define FOD(x,a,b) for (int x=a;x>=b;x--)
#define REP(x,a,b) for (int x=a;x<b;x++)
#define RED(x,a,b) for (int x=a;x>b;x--)

const int MAX = 1e5 + 10;
const int MAXN = 1e4 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;

int dx[] = {0 , -1 , 0 , 1};
int dy[] = {1 , 0 , -1 , 0};

int test;
vi adj[MAX_N];
int G[MAX_N];
int n , k;
ll res = 0LL;

void DFS(int u , int parent) {
    REP(i , 0 , adj[u].size()) {
        int v = adj[u][i];

        if (v == parent) continue;
        DFS(v , u);

        G[u] += G[v];
    }

    res += (ll) min(G[u] , 2 * k - G[u]);
}

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

    //freopen("input.txt", "r" , stdin);

    cin >> n >> k;

    FOR(i , 1 , 2 * k) {
        int x;
        cin >> x;
        G[x] = 1;
    }

    REP(i , 1 , n) {
        int u , v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    DFS(1 , -1);

    cout << res;

	return 0;
}


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

#define ll long long
#define ull unsigned long long
#define ii pair<int,int>
#define iii pair<ii, int>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int) a.size()
#define cl(a) a.clear()

#define vi vector<int>
#define vii vector<ii>

#define LOWBIT(x) ( (x) & -(x) )

#define FOR(x,a,b) for (int x=a;x<=b;x++)
#define FOD(x,a,b) for (int x=a;x>=b;x--)
#define REP(x,a,b) for (int x=a;x<b;x++)
#define RED(x,a,b) for (int x=a;x>b;x--)

const int MAX = 1e5 + 10;
const int MAXN = 1e4 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;

int dx[] = {0 , -1 , 0 , 1};
int dy[] = {1 , 0 , -1 , 0};

int test;
vi adj[MAX_N];
int G[MAX_N];
int n , k , res = 0;

void DFS(int u , int parent) {
    REP(i , 0 , adj[u].size()) {
        int v = adj[u][i];

        if (v == parent) continue;
        DFS(v , u);

        G[u] += G[v];
    }

    res += min(G[u] , 2 * k - G[u]);
}

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

    //freopen("input.txt", "r" , stdin);

    cin >> n >> k;

    FOR(i , 1 , 2 * k) {
        int x;
        cin >> x;
        G[x] = 1;
    }

    REP(i , 1 , n) {
        int u , v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    DFS(1 , -1);

    cout << res;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define LL long long
#define INF (1<<30)

int n,k,ans;
int un[MAXN];
vector<int> g[MAXN];
int sz[MAXN];

int tt,prv[MAXN];
void dfs(int u){
	prv[u]=++tt;
	sz[u]+=un[u];
	int v;
	for(int i=0;i<g[u].size();i++){
		v=g[u][i];
		if(prv[v])continue;
		dfs(v);
		sz[u]+=sz[v];
		ans+=min(sz[v],2*k-sz[v]);
//		printf("nd%d %d\n",v,sz[v]);
	}
}

int main(){
	cin>>n>>k;
	int u,v;
	for(int i=1;i<=2*k;i++){
		cin>>u;
		un[u]++;
	}
	for(int i=1;i<n;i++){
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1);
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define LL long long
#define INF (1<<30)

LL n,k,ans;
LL un[MAXN];
vector<LL> g[MAXN];
LL sz[MAXN];

int tt,prv[MAXN];
void dfs(int u){
	prv[u]=++tt;
	sz[u]+=un[u];
	LL v;
	for(int i=0;i<g[u].size();i++){
		v=g[u][i];
		if(prv[v])continue;
		dfs(v);
		sz[u]+=sz[v];
		ans+=min(sz[v],2*k-sz[v]);
//		printf("nd%d %d\n",v,sz[v]);
	}
}

int main(){
	cin>>n>>k;
	LL u,v;
	for(int i=1;i<=2*k;i++){
		cin>>u;
		un[u]++;
	}
	for(int i=1;i<n;i++){
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1);
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define MX 200007
#define ll long long
vector< int >ed[MX];
int un[MX], lv[MX];
ll sbtr[MX];
int c, n, k;

void dfs (int u)
{
    int i, v;
    lv[u] = ++c;
    sbtr[u] += un[u];
    for (i = 0; i < ed[u].size(); i++) {
        v = ed[u][i];
        if (lv[v]) continue;
        dfs(v);
        sbtr[u] += sbtr[v];
    }
}

int main(void)
{
    std::ios::sync_with_stdio(false);

    int i, x, y; ll ans=0;
    cin >> n >> k;
    for (i = 1; i <= k+k; i++) {
        cin >> x;
        un[x] = 1;
    }
    pair< int, int >p[MX];
    for (i = 1; i < n; i++) {
        cin >> x >> y;
        ed[x].push_back(y);
        ed[y].push_back(x);
        p[i] = {x, y};
    }
    dfs(1);

    for (i = 1; i < n; i++) {
        x = p[i].first;
        y = p[i].second;
        if (lv[x] > lv[y]) ans += min(sbtr[x], 2*k-sbtr[x]);
        else ans += min(sbtr[y], 2*k-sbtr[y]);
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MX 200007
vector< int >ed[MX];
int un[MX], sbtr[MX], lv[MX];
int c, n, k;

void dfs (int u)
{
    int i, v;
    lv[u] = ++c;
    sbtr[u] += un[u];
    for (i = 0; i < ed[u].size(); i++) {
        v = ed[u][i];
        if (lv[v]) continue;
        dfs(v);
        sbtr[u] += sbtr[v];
    }
}

int main(void)
{
    std::ios::sync_with_stdio(false);

    int i, x, y, ans=0;
    cin >> n >> k;
    for (i = 1; i <= k+k; i++) {
        cin >> x;
        un[x] = 1;
    }
    pair< int, int >p[MX];
    for (i = 1; i < n; i++) {
        cin >> x >> y;
        ed[x].push_back(y);
        ed[y].push_back(x);
        p[i] = {x, y};
    }
    dfs(1);

    for (i = 1; i < n; i++) {
        x = p[i].first;
        y = p[i].second;
        if (lv[x] > lv[y]) ans += min(sbtr[x], 2*k-sbtr[x]);
        else ans += min(sbtr[y], 2*k-sbtr[y]);
    }

    cout << ans << endl;

    return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second

const int mxn = 200000;
int n, k;
int a[mxn];
vector<int> g[mxn];

ll dfs(int c, int p){
	ll ret = 0;
	for(int i : g[c]){
		if(i == p) continue;
		ret += dfs(i, c);
		ret += min(a[i], k - a[i]);
		a[c] += a[i];
	}
	return ret;
}

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

	cin >> n >> k;
	k *= 2;

	for(int i = 0; i < k; i++){
		int x;
		cin >> x;
		a[--x]++;
	}

	for(int i = 0; i < n - 1; i++){
		int u, v;
		cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	cout << dfs(0, -1) << endl;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<time.h>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<functional>
#include<stack>
#include<map>
#include<queue>
#define mod (1000000007)
#define middle (l+r)>>1
#define SIZE 10000+10
#define lowbit(x) (x&(-x))
#define lson (rt<<1)
#define rson (rt<<1|1)
typedef long long ll;
const int inf_max = 0x3f3f3f3f;
const ll Linf = 9e18;
const int maxn = 200000+10;
const double eps=0.0001;
using namespace std;
int n,k,mark[maxn],cnt,head[maxn],vis[maxn],val[maxn];
ll ans=0;
struct Edge
{
    int v,next;
}edge[maxn<<1];
void add_edge(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void Initial()
{
    memset(val,0,sizeof(val));
    memset(head,-1,sizeof(head));
    memset(mark,0,sizeof(mark));
    memset(edge,0,sizeof(edge));
    cnt=0;ans=0;
}
void dfs(int now)
{
    if(vis[now])
        return ;
    //printf("now=%d,ans=%d\n",now,ans);
    vis[now]=1;
    if(mark[now])
        val[now]=1;
    for(int i=head[now];~i;i=edge[i].next)
    {
        int v=edge[i].v;
        if(vis[v])
            continue ;
        dfs(v);
        val[now]+=val[v];
        //printf("vnow=%d,vv=%d\n",val[now],val[v]);
    }
    ans+=(ll)min(val[now],2*k-val[now]);
    vis[now]=0;
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        Initial();
        for(int i=1;i<=k*2;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            mark[tmp]=1;
        }
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        dfs(1);
        printf("%d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<time.h>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<functional>
#include<stack>
#include<map>
#include<queue>
#define mod (1000000007)
#define middle (l+r)>>1
#define SIZE 10000+10
#define lowbit(x) (x&(-x))
#define lson (rt<<1)
#define rson (rt<<1|1)
typedef long long ll;
const int inf_max = 0x3f3f3f3f;
const ll Linf = 9e18;
const int maxn = 200000+10;
const double eps=0.0001;
using namespace std;
int n,k,mark[maxn],cnt,head[maxn],vis[maxn],val[maxn];
ll ans=0;
struct Edge
{
    int v,next;
}edge[maxn<<1];
void add_edge(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void Initial()
{
    memset(val,0,sizeof(val));
    memset(head,-1,sizeof(head));
    memset(mark,0,sizeof(mark));
    memset(edge,0,sizeof(edge));
    cnt=0;ans=0;
}
void dfs(int now)
{
    if(vis[now])
        return ;
    //printf("now=%d,ans=%d\n",now,ans);
    vis[now]=1;
    if(mark[now])
        val[now]=1;
    for(int i=head[now];~i;i=edge[i].next)
    {
        int v=edge[i].v;
        if(vis[v])
            continue ;
        dfs(v);
        val[now]+=val[v];
        //printf("vnow=%d,vv=%d\n",val[now],val[v]);
    }
    ans+=(ll)min(val[now],2*k-val[now]);
    vis[now]=0;
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        Initial();
        for(int i=1;i<=k*2;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            mark[tmp]=1;
        }
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        dfs(1);
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<time.h>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<functional>
#include<stack>
#include<map>
#include<queue>
#define mod (1000000007)
#define middle (l+r)>>1
#define SIZE 10000+10
#define lowbit(x) (x&(-x))
#define lson (rt<<1)
#define rson (rt<<1|1)
typedef long long ll;
const int inf_max = 0x3f3f3f3f;
const ll Linf = 9e18;
const int maxn = 200000+10;
const double eps=0.0001;
using namespace std;
int n,k,mark[maxn],cnt,head[maxn],vis[maxn],val[maxn],ans;
struct Edge
{
    int v,next;
}edge[maxn<<1];
void add_edge(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void Initial()
{
    memset(val,0,sizeof(val));
    memset(head,-1,sizeof(head));
    memset(mark,0,sizeof(mark));
    memset(edge,0,sizeof(edge));
    cnt=0;ans=0;
}
void dfs(int now)
{
    if(vis[now])
        return ;
    //printf("now=%d,ans=%d\n",now,ans);
    vis[now]=1;
    if(mark[now])
        val[now]=1;
    for(int i=head[now];~i;i=edge[i].next)
    {
        int v=edge[i].v;
        if(vis[v])
            continue ;
        dfs(v);
        val[now]+=val[v];
        //printf("vnow=%d,vv=%d\n",val[now],val[v]);
    }
    ans+=min(val[now],2*k-val[now]);
    vis[now]=0;
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        Initial();
        for(int i=1;i<=k*2;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            mark[tmp]=1;
        }
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        dfs(1);
        printf("%d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<time.h>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<functional>
#include<stack>
#include<map>
#include<queue>
#define mod (1000000007)
#define middle (l+r)>>1
#define SIZE 10000+10
#define lowbit(x) (x&(-x))
#define lson (rt<<1)
#define rson (rt<<1|1)
typedef long long ll;
const int inf_max = 0x3f3f3f3f;
const ll Linf = 9e18;
const int maxn = 200000+10;
const double eps=0.0001;
using namespace std;
int L,R,n,k,mark[maxn],cnt,head[maxn],c[2],vis[maxn];
struct Edge
{
    int v,u,next;
}edge[maxn<<1],rode[maxn];
void add_edge(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void Initial()
{
    memset(rode,0,sizeof(rode));
    memset(head,-1,sizeof(head));
    memset(mark,0,sizeof(mark));
    memset(edge,0,sizeof(edge));
    cnt=0;
}
void dfs(int now,int t,int tar)
{
    //printf("now=%d\n",now);
    if(vis[now])
        return ;
    if(mark[now])
        c[t]++;
    vis[now]=1;
    for(int i=head[now];~i;i=edge[i].next)
    {
        if(i==tar||i==tar+1)
            continue;
        dfs(edge[i].v,t,tar);
    }

}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        Initial();
        for(int i=1;i<=k*2;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            mark[tmp]=1;
        }
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            //printf("u=%d,v=%d\n",u,v);
            rode[i-1].u=u;rode[i-1].v=v;
            add_edge(u,v);
            add_edge(v,u);
        }
        int ans=0;
        for(int i=0;i<n-1;i++)
        {
            memset(vis,0,sizeof(vis));
           c[0]=c[1]=0;
            dfs(rode[i].u,0,i);
            //cout<<"heheh"<<endl;
            memset(vis,0,sizeof(vis));
            dfs(rode[i].v,1,i*2);
            //printf("u=%d,v=%d,c0=%d,c1=%d\n",rode[i].u,rode[i].v,c[0],c[1]);
            ans+=(min(c[0],c[1]));
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define N 200002

vector<int>G[N];
int n , k , cnt[N];
lli ans=0;

void dfs(int x=1, int par = -1){

    for(int u : G[x]){

        if(par == u) continue;

        dfs(u,x);

        cnt[x] += cnt[u];

        ans += min(cnt[u] , k - cnt[u]);
    }
}

int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    scn2(n,k);
    k *= 2;

    for(int i=1 ;i<=k; i++){
        int x;
        scn(x);
        cnt[x] = 1;
    }

    for(int i=1; i<n; i++){
        int u,v;
        scn2(u,v);

        G[u].pb(v);
        G[v].pb(u);
    }

    dfs();

    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct Edge{int to,nex;}edge[N<<1];int head[N],tot;
inline void add(int from,int to){
    edge[++tot]=(Edge){to,head[from]},head[from]=tot;
    edge[++tot]=(Edge){from,head[to]},head[to]=tot;
}
int n,k,dp[N];ll ans=0;
void dfs(int x,int fa){
    for(int i=head[x];i;i=edge[i].nex){
        int y=edge[i].to;
        if(y==fa) continue;
        dfs(y,x),dp[x]+=dp[y];
        ans+=min(dp[y],2*k-dp[y]);
    }
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1,x;i<=2*k;++i) scanf("%d",&x),dp[x]=1;
    for(int i=1,x,y;i<=n-1;++i) scanf("%d%d",&x,&y),add(x,y);
    dfs(1,1);
    printf("%lld\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
struct Edge{int to,nex;}edge[N<<1];int head[N],tot;
inline void add(int from,int to){
    edge[++tot]=(Edge){to,head[from]},head[from]=tot;
    edge[++tot]=(Edge){from,head[to]},head[to]=tot;
}
int n,k,dp[N];ll ans=0;
void dfs(int x,int fa){
    for(int i=head[x];i;i=edge[i].nex){
        int y=edge[i].to;
        if(y==fa) continue;
        dfs(y,x),dp[x]+=dp[y];
        ans+=min(dp[y],2*k-dp[y]);
    }
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1,x;i<=2*k;++i) scanf("%d",&x),dp[x]=1;
    for(int i=1,x,y;i<=n-1;++i) scanf("%d%d",&x,&y),add(x,y);
    dfs(1,1);
    printf("%lld\n",ans);
}


// HEY STALKER

#include<bits/stdc++.h>
#define ll long long
#define VS14 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;

ll n, k;
map<ll, ll> mp;
vector<ll> v;
vector<vector<ll>> adj(200005);
void dfs(ll cur, ll p)
{
    for(auto x: adj[cur])
    {
        if(x != p)
        {
            dfs(x, cur);
            v[cur] += v[x];
        }
    }
    if(mp[cur]) v[cur]++;
}
int main()
{
    VS14
    #ifndef ONLINE_JUDGE
    freopen("input.txt","rndo",stdin);
    freopen("output.txt","w",stdout);
    #endif

    cin >> n >> k;
    v.resize(n+1);
    for(ll i = 0; i < 2*k; i++)
    {
        ll x; cin >> x;
        mp[x]++;
    }
    for(ll i = 1; i < n; i++)
    {
        ll x, y; cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    k *= 2;
    dfs(1, -1);
    ll ans = 0;
    for(ll i = 1; i <= n; i++) ans += min(v[i], k-v[i]);
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

#define int long long
#define rep(i,f,l) for(int i=f;i<l;++i)
#define FOR(i,n) for(int i=0;i<n;++i)
#define REP(i,n) for(int i=1;i<=n;++i)
#define sim template < class c
#define ris return * this
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define Foreach(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define pb push_back
#define SI size()
#define S second
#define F first
#define C clear()
#define pii pair<int,int>
#define B begin()
#define mp make_pair
#define E end()
#define I insert
#define piii pair<long long,pair<long long,long long> >
#define P push
//#define T top()
#define PP pop()

const int MAXn=2e5+100;

vector <int> T[MAXn];
int n,k,u,v,h[MAXn],sz[MAXn],cnt;
bool mark[MAXn];

int dfs(int v,int par=0){

  sz[v]=mark[v];
  for(auto u:T[v])
    if(u!=par)
      sz[v]+=dfs(u,v);
  return
    sz[v];
}
int cent(int v,int ans,int par){

  if(ans>k)
    return -1;
  bool b=0;
  for(auto u:T[v])
    if(u!=par&&sz[u]>k)b=1;
  if(!b)
    return v;
  for(auto u:T[v]){

    cnt=cent(u,ans+sz[v]-sz[u],v);
    if(u!=par&&~cnt)
      return
        cnt;
  }
  return -1;
}
int dfs_2(int v,int par=0){

  h[v]=h[par]+1;
  int ans = 0;
  if(mark[v])
    ans+=h[v]-1;
  for(auto u:T[v])
    if(u!=par)
      ans+=dfs_2(u,v);
  return
    ans;
}
signed main(){

#ifdef FU
  freopen("input.txt","r",stdin);
  freopen("out[ut.txt","w",stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(NULL),cout.tie(NULL);

  cin>>n>>k;
  FOR(i,2*k)
   cin>>v,mark[v]=1;
  REP(i,n-1){

    cin>>u>>v;
    T[v].pb(u);
    T[u].pb(v);
  }
  dfs(1);
//  cout << cent(1,0,0) << endl;
  return
    cout<<dfs_2(cent(1,0,0))<<endl,0;
}


#include<bits/stdc++.h>

using namespace std;

#define int long long
#define rep(i,f,l) for(int i=f;i<l;++i)
#define FOR(i,n) for(int i=0;i<n;++i)
#define REP(i,n) for(int i=1;i<=n;++i)
#define sim template < class c
#define ris return * this
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define Foreach(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define pb push_back
#define SI size()
#define S second
#define F first
#define C clear()
#define pii pair<int,int>
#define B begin()
#define mp make_pair
#define E end()
#define I insert
#define piii pair<long long,pair<long long,long long> >
#define P push
//#define T top()
#define PP pop()

const int MAXn=2e5+100;

vector <int> T[MAXn];
int n,k,u,v,h[MAXn],sz[MAXn],cnt;
bool mark[MAXn];

int dfs(int v,int par=0){

  sz[v]=mark[v];
  for(auto u:T[v])
    if(u!=par)
      sz[v]+=dfs(u,v);
  return
    sz[v];
}
int cent(int v,int ans,int par){

  if(ans>k)
    return -1;
  bool b=0;
  for(auto u:T[v])
    if(u!=par&&sz[u]>k)b=1;
  if(!b)
    return v;
  for(auto u:T[v]){

    if(u!=par){

      cnt=cent(u,ans+sz[v]-sz[u],v);
      if(~cnt)
         return
            cnt;
  }
  }
  return -1;
}
int dfs_2(int v,int par=0){

  h[v]=h[par]+1;
  int ans = 0;
  if(mark[v])
    ans+=h[v]-1;
  for(auto u:T[v])
    if(u!=par)
      ans+=dfs_2(u,v);
  return
    ans;
}
signed main(){

#ifdef FU
  freopen("input.txt","r",stdin);
  freopen("out[ut.txt","w",stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(NULL),cout.tie(NULL);

  cin>>n>>k;
  FOR(i,2*k)
   cin>>v,mark[v]=1;
  REP(i,n-1){

    cin>>u>>v;
    T[v].pb(u);
    T[u].pb(v);
  }
  dfs(1);
//  cout << cent(1,0,0) << endl;
  return
    cout<<dfs_2(cent(1,0,0))<<endl,0;
}


#include<bits/stdc++.h>

using namespace std;

#define int long long
#define rep(i,f,l) for(int i=f;i<l;++i)
#define FOR(i,n) for(int i=0;i<n;++i)
#define REP(i,n) for(int i=1;i<=n;++i)
#define sim template < class c
#define ris return * this
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define Foreach(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define pb push_back
#define SI size()
#define S second
#define F first
#define C clear()
#define pii pair<int,int>
#define B begin()
#define mp make_pair
#define E end()
#define I insert
#define piii pair<long long,pair<long long,long long> >
#define P push
//#define T top()
#define PP pop()

const int MAXn=2e5+100;

vector <int> T[MAXn];
int n,k,u,v,h[MAXn],sz[MAXn];
bool mark[MAXn];

int dfs(int v,int par=0){

  sz[v]=mark[v];
  for(auto u:T[v])
    if(u!=par)
      sz[v]+=dfs(u,v);
  return
    sz[v];
}
int cent(int v,int ans,int par){

  if(ans>k)
    return -1;
  bool b=0;
  for(auto u:T[v])
    if(u!=par&&sz[u]>k)b=1;
  if(!b)
    return v;
  for(auto u:T[v])
    if(u!=par&&~(cent(u,ans+sz[v]-sz[u],v)))
      return
        cent(u,ans+sz[v]-sz[u],v);

  return -1;
}
int dfs_2(int v,int par=0){

  h[v]=h[par]+1;
  int ans = 0;
  if(mark[v])
    ans+=h[v]-1;
  for(auto u:T[v])
    if(u!=par)
      ans+=dfs_2(u,v);
  return
    ans;
}
signed main(){

#ifdef FU
  freopen("input.txt","r",stdin);
  freopen("out[ut.txt","w",stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(NULL),cout.tie(NULL);

  cin>>n>>k;
  FOR(i,2*k)
   cin>>v,mark[v]=1;
  REP(i,n-1){

    cin>>u>>v;
    T[v].pb(u);
    T[u].pb(v);
  }
  dfs(1);
//  cout << cent(1,0,0) << endl;
  return
    cout<<dfs_2(cent(1,0,0))<<endl,0;
}


#include<bits/stdc++.h>

using namespace std;

#define int long long
#define rep(i,f,l) for(int i=f;i<l;++i)
#define FOR(i,n) for(int i=0;i<n;++i)
#define REP(i,n) for(int i=1;i<=n;++i)
#define sim template < class c
#define ris return * this
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define Foreach(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define pb push_back
#define SI size()
#define S second
#define F first
#define C clear()
#define pii pair<int,int>
#define B begin()
#define mp make_pair
#define E end()
#define I insert
#define piii pair<long long,pair<long long,long long> >
#define P push
//#define T top()
#define PP pop()

const int MAXn=2e5+100;

vector <int> T[MAXn];
int n,k,u,v,h[MAXn],sz[MAXn];
bool mark[MAXn];

int dfs(int v,int par=0){

  sz[v]=mark[v];
  for(auto u:T[v])
    if(u!=par)
      sz[v]+=dfs(u,v);
  return
    sz[v];
}
int cent(int v,int ans,int par){

  if(ans>k)
    return -1;
  bool b=0;
  for(auto u:T[v])
    if(u!=par&&sz[u]>k)b=1;
  if(!b)
    return v;
  for(auto u:T[v]){

    int cnt=cent(u,ans+sz[v]-sz[u],v);
    if(u!=par&&~cnt)
      return
        cnt;
  }
  return -1;
}
int dfs_2(int v,int par=0){

  h[v]=h[par]+1;
  int ans = 0;
  if(mark[v])
    ans+=h[v]-1;
  for(auto u:T[v])
    if(u!=par)
      ans+=dfs_2(u,v);
  return
    ans;
}
signed main(){

#ifdef FU
  freopen("input.txt","r",stdin);
  freopen("out[ut.txt","w",stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(NULL),cout.tie(NULL);

  cin>>n>>k;
  FOR(i,2*k)
   cin>>v,mark[v]=1;
  REP(i,n-1){

    cin>>u>>v;
    T[v].pb(u);
    T[u].pb(v);
  }
  dfs(1);
//  cout << cent(1,0,0) << endl;
  return
    cout<<dfs_2(cent(1,0,0))<<endl,0;
}


#include<bits/stdc++.h>

using namespace std;

#define int long long
#define rep(i,f,l) for(int i=f;i<l;++i)
#define FOR(i,n) for(int i=0;i<n;++i)
#define REP(i,n) for(int i=1;i<=n;++i)
#define sim template < class c
#define ris return * this
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define Foreach(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define pb push_back
#define SI size()
#define S second
#define F first
#define C clear()
#define pii pair<int,int>
#define B begin()
#define mp make_pair
#define E end()
#define I insert
#define piii pair<long long,pair<long long,long long> >
#define P push
//#define T top()
#define PP pop()

const int MAXn=1e5+100;

vector <int> T[MAXn];
int n,k,u,v,h[MAXn],sz[MAXn];
bool mark[MAXn];

int dfs(int v,int par=0){

  sz[v]=mark[v];
  for(auto u:T[v])
    if(u!=par)
      sz[v]+=dfs(u,v);
  return
    sz[v];
}
int cent(int v,int ans,int par){

  if(ans>k)
    return -1;
  bool b=0;
  for(auto u:T[v])
    if(u!=par&&sz[u]>k)b=1;
  if(!b)
    return v;
  for(auto u:T[v])
    if(u!=par&&~(cent(u,ans+sz[v]-sz[u],v)))
      return
        cent(u,ans+sz[v]-sz[u],v);

  return -1;
}
int dfs_2(int v,int par=0){

  h[v]=h[par]+1;
  int ans = 0;
  if(mark[v])
    ans+=h[v]-1;
  for(auto u:T[v])
    if(u!=par)
      ans+=dfs_2(u,v);
  return
    ans;
}
signed main(){

#ifdef FU
  freopen("input.txt","r",stdin);
  freopen("out[ut.txt","w",stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(NULL),cout.tie(NULL);

  cin>>n>>k;
  FOR(i,2*k)
   cin>>v,mark[v]=1;
  REP(i,n-1){

    cin>>u>>v;
    T[v].pb(u);
    T[u].pb(v);
  }
  dfs(1);
//  cout << cent(1,0,0) << endl;
  return
    cout<<dfs_2(cent(1,0,0))<<endl,0;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn=2e5+7;
int n,k,res;
bool mrk[maxn];
vector<int>g[maxn];

int dfs(int v,int p){
	int cnt=(mrk[v]?1:0);
	for(auto u:g[v])
		if(u!=p)cnt+=dfs(u,v);
	res+=min(cnt,k-cnt);
	return cnt;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>k;k*=2;
	for(int i=0,a;i<k;i++)cin>>a,mrk[a]=true;
	for(int i=1,u,v;i<n;i++)cin>>u>>v,g[u].pb(v),g[v].pb(u);
	dfs(1,0);
	cout<<res;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn=2e5+7;
int n,k;
long long res;
bool mrk[maxn];
vector<int>g[maxn];

int dfs(int v,int p){
	int cnt=(mrk[v]?1:0);
	for(auto u:g[v])
		if(u!=p)cnt+=dfs(u,v);
	res+=min(cnt,k-cnt);
	return cnt;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>k;k*=2;
	for(int i=0,a;i<k;i++)cin>>a,mrk[a]=true;
	for(int i=1,u,v;i<n;i++)cin>>u>>v,g[u].pb(v),g[v].pb(u);
	dfs(1,0);
	cout<<res;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, k;
int cnt[N];
vector<int> G[N];
LL ans;

void dfs(int u, int pre) {
    for(int v : G[u]) {
        if(v != pre) {
            dfs(v, u);
            cnt[u] += cnt[v];
        }
    }
    if(u != 1) ans += min(cnt[u], k+k-cnt[u]);
}

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= k+k; i++) {
        int x;
        scanf("%d", &x);
        cnt[x]++;
    }

    for(int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1, 0);
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, k;
int cnt[N];
vector<int> G[N];
LL ans;

void dfs(int u, int pre) {
    for(int v : G[u]) {
        if(v != pre) {
            dfs(v, u);
            cnt[u] += cnt[v];
        }
    }
    if(u != 1) ans += min(cnt[u], k+k-cnt[u]);
}

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= k+k; i++) {
        int x;
        scanf("%d", &x);
        cnt[x]++;
    }

    for(int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1, 0);
    printf("%lld\n", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int cnt[200006];
long long ans=0;
int n,k;
vector<int >vv[200006];
void dfs(int cur,int from)
{
	for(int i=0;i<vv[cur].size();i++){
		if(vv[cur][i]!=from){
			dfs(vv[cur][i],cur);
			ans+=min(cnt[vv[cur][i]],2*k-cnt[vv[cur][i]]);
			cnt[cur]+=cnt[vv[cur][i]];
		}
	}
}
int main()
{
	int m,u,v;
	scanf("%d %d",&n,&k);
	for(int i=1;i<=2*k;i++){
		scanf("%d",&m);
		cnt[m]=1;
	}
	for(int i=1;i<=n-1;i++){
		scanf("%d %d",&u,&v);
		vv[u].push_back(v);
		vv[v].push_back(u);
	}
	dfs(1,-1);
	printf("%I64d\n",ans);
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define ll long long
#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define y1 ewfafdasf
#define INF 1111111
using namespace std;

typedef struct node
{
	ll c;
	vector<int> next;
}node;

node nodes[200020];
ll res = 0;
int n,k;

void dfs1(int a,int b)
{
	for(int i=0;i<nodes[a].next.size();i++)
	{
		if(nodes[a].next[i]!=b)
		{
			dfs1(nodes[a].next[i],a);
			nodes[a].c+=nodes[nodes[a].next[i]].c;
		}
	}
}

void dfs2(int a,int b)
{
	for(int i=0;i<nodes[a].next.size();i++)
	{
		if(nodes[a].next[i]!=b)
		{
			res+=min(nodes[nodes[a].next[i]].c,k-nodes[nodes[a].next[i]].c);
			dfs2(nodes[a].next[i],a);
		}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>k;
	k = k*2;
	int tmp,tmp2;
	Fori(k)
	{
		cin>>tmp;
		nodes[tmp].c++;
	}
	Fori(n-1)
	{
		cin>>tmp>>tmp2;
		nodes[tmp].next.push_back(tmp2);
		nodes[tmp2].next.push_back(tmp);
	}
	dfs1(1,-1);
	dfs2(1,-1);
	cout<<res;
	//system("pause");
	return 0;
}


/* Dsingh_24 */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define ios	    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

#define N  200005
ll ans,n,k,sub[N];
vi a[N];
void dfs(ll node,ll p){
	for(auto i:a[node]){
		if(i!=p){
			dfs(i,node);
			ans+=min(sub[i],2*k-sub[i]);
			sub[node]+=sub[i];
		}
	}
}
int main()
{
	ios
	ll i,j,x,y;
	cin>>n>>k;
	for(i=0;i<2*k;i++){
		cin>>j;
		sub[j]++;
	}
	for(i=0;i<n-1;i++){
		cin>>x>>y;
		a[x].pb(y);
		a[y].pb(x);
	}
	dfs(1,0);
	cout<<ans;
	return 0;
}


/* Dsingh_24 */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define ios	    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

#define N  100005
ll ans,n,k,sub[N];
vi a[N];
void dfs(ll node,ll p){
	for(auto i:a[node]){
		if(i!=p){
			dfs(i,node);
			ans+=min(sub[i],2*k-sub[i]);
			sub[node]+=sub[i];
		}
	}
}
int main()
{
	ios
	ll i,j,x,y;
	cin>>n>>k;
	for(i=0;i<2*k;i++){
		cin>>j;
		sub[j]++;
	}
	for(i=0;i<n-1;i++){
		cin>>x>>y;
		a[x].pb(y);
		a[y].pb(x);
	}
	dfs(1,0);
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define max1 200005
#define siz 100000
#define mod 1000000007
#define inf 1e18
#define ll long long int
#define debug(x) cout<<#x<<" "<<x<<endl
#define jam(x) cout<<"Case #"<<x<<": "
ll tt;
#define run_time cout << "\nTIME = " << clock() - tt << endl; tt = clock();
typedef pair<ll,ll> pr;
unordered_map<ll,ll> mp;
vector<vector<ll> > adj(max1,vector<ll>() );
ll ans=0;
ll k;
ll dfs(ll x,ll par)
{
  ll cnt=0;
  for(ll i=0;i<adj[x].size();i++)
  {
    if(adj[x][i]!=par)
    {
      cnt=cnt+dfs(adj[x][i],x);
    }
  }
  cnt+=mp[x];
  ans=ans+min(cnt,2*k-cnt);
  return cnt;
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  tt = clock();
  #endif
  ll n;
  cin>>n>>k;
  for(ll i=0;i<2*k;i++)
  {
    ll x;
    cin>>x;
    mp[x]++;
  }
  for(ll i=1;i<n;i++)
  {
    ll u,v;
    cin>>u>>v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1,-1);
  cout<<ans<<endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair

const int maxN = 2e5 + 10;
int n,k,s[maxN]; //s[i]: tedad univ haaye zir derakhte i
bool univ[maxN];
vector <int> adj[maxN];

void dfs(int v, int p)
{
    for (int i = 0 ; i < adj[v].size() ; i++)
    {
        int u = adj[v][i];
        if (u != p)
        {
            dfs(u, v);
            s[v] += s[u];
        }

    }
    if (univ[v])
        s[v]++;
}

int32_t main()
{
    cin >> n >> k;
    for (int i = 0 ; i < 2 * k ; i++)
    {
        int v;
        cin >> v;
        v--;
        univ[v] = true;
    }

    for (int i = 1 ; i < n ; i++)
    {
        int v,u;
        cin >> v >> u;
        v-- , u--;
        adj[v].pb(u);
        adj[u].pb(v);
    }

    dfs(0, -1);

    ll res = 0;
    for (int i = 0 ; i < n ; i++)
    {
        int cur = min(s[i], 2 * k - s[i]); //yaale par[i] be i , daghighan cur bar estefade
        res += cur;
    }
    cout << res << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair

const int maxN = 2e5 + 10;
int n,k,s[maxN]; //s[i]: tedad univ haaye zir derakhte i
bool univ[maxN];
vector <int> adj[maxN];

void dfs(int v, int p)
{
    for (int i = 0 ; i < adj[v].size() ; i++)
    {
        int u = adj[v][i];
        if (u != p)
        {
            dfs(u, v);
            s[v] += s[u];
        }

    }
    if (univ[v])
        s[v]++;
}

int32_t main()
{
    cin >> n >> k;
    for (int i = 0 ; i < 2 * k ; i++)
    {
        int v;
        cin >> v;
        v--;
        univ[v] = true;
    }

    for (int i = 1 ; i < n ; i++)
    {
        int v,u;
        cin >> v >> u;
        v-- , u--;
        adj[v].pb(u);
        adj[u].pb(v);
    }

    dfs(0, -1);

    int res = 0;
    for (int i = 0 ; i < n ; i++)
    {
        int cur = min(s[i], 2 * k - s[i]); //yaale par[i] be i , daghighan cur bar estefade
        res += cur;
    }
    cout << res << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair

const int maxN = 2e5 + 10;
int n,k,s[maxN]; //s[i]: tedad univ haaye zir derakhte i
bool univ[maxN];
vector <int> adj[maxN];

void dfs(int v, int p)
{
    for (int i = 0 ; i < adj[v].size() ; i++)
    {
        int u = adj[v][i];
        if (u != p)
        {
            dfs(u, v);
            s[v] += s[u];
        }

    }
    if (univ[v])
        s[v]++;
}

int32_t main()
{
    cin >> n >> k;
    for (int i = 0 ; i < 2 * k ; i++)
    {
        int v;
        cin >> v;
        v--;
        univ[v] = true;
    }

    for (int i = 1 ; i < n ; i++)
    {
        int v,u;
        cin >> v >> u;
        v-- , u--;
        adj[v].pb(u);
        adj[u].pb(v);
    }

    dfs(0, -1);

    long long res = 0;
    for (int i = 0 ; i < n ; i++)
    {
        int cur = min(s[i], 2 * k - s[i]); //yaale par[i] be i , daghighan cur bar estefade
        res += cur;
    }
    cout << res << endl;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll MAX = 200005;
static ll n, k;
static ll uni[MAX];
static vector<vector<ll>> adj;
static ll vis[MAX];
static ll s[MAX];
ll dfs(ll src){
    vis[src] = 1;
    ll fin = 0;
    if(uni[src] == 1){
        fin += 1;
    }
    for(ll x: adj[src]){
        if(vis[x] == 0){
            fin += dfs(x);
        }
    }
    s[src] = fin;
    return fin;
}

int main(){
    cin >>n>> k;
    for(ll i =0 ; i<2*k; i++){
        ll d;
        cin >> d;
        d -= 1;
        uni[d] = 1;
    }
    for(ll i = 0; i<n; i++){
        vector<ll> temp;
        adj.emplace_back(temp);
    }
    for(ll i =0 ; i<n-1; i++){
        ll x, y;
        cin >> x >> y;
        x-= 1;
        y -= 1;
        adj[x].emplace_back(y);
        adj[y].emplace_back(x);
    }
    dfs(0);
    //preprocess();
    //cout << endl;
    ll fin = 0;
    for(ll i = 0; i<n; i++){
        //cout << i + 1 << " " << s[i] << endl;
        fin += min(s[i], 2*k - s[i]);
    }
    cout << fin << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 200005;
static int n, k;
static int uni[MAX];
static vector<vector<int>> adj;
static int vis[MAX];
static int s[MAX];
int dfs(int src){
    vis[src] = 1;
    int fin = 0;
    if(uni[src] == 1){
        fin += 1;
    }
    for(int x: adj[src]){
        if(vis[x] == 0){
            fin += dfs(x);
        }
    }
    s[src] = fin;
    return fin;
}

int main(){
    cin >>n>> k;
    for(int i =0 ; i<2*k; i++){
        int d;
        cin >> d;
        d -= 1;
        uni[d] = 1;
    }
    for(int i = 0; i<n; i++){
        vector<int> temp;
        adj.emplace_back(temp);
    }
    for(int i =0 ; i<n-1; i++){
        int x, y;
        cin >> x >> y;
        x-= 1;
        y -= 1;
        adj[x].emplace_back(y);
        adj[y].emplace_back(x);
    }
    dfs(0);
    //preprocess();
    //cout << endl;
    int fin = 0;
    for(int i = 0; i<n; i++){
        //cout << i + 1 << " " << s[i] << endl;
        fin += min(s[i], 2*k - s[i]);
    }
    cout << fin << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, s[200010], univ[200010], k;
long long ans;
vector<vector<int> > gr;
vector<int> vi;

void dfs(int x, int par){
	if(univ[x]){
		++s[x];
	}
	for(auto i : gr[x]){
		if(i != par){
			dfs(i, x);
		} else {
			continue;
		}
		s[x] += s[i];
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> k;
	gr.resize(n, vi);
	for(int i = 0; i < 2*k; ++i){
		int x;
		cin >> x;
		univ[x-1] = 1;
	}
	for(int i =0 ; i < n-1; ++i){
		int u,v;
		cin >> u >> v;
		--u, v--;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}
	dfs(0, -1);
	for (int i = 0; i < n; ++i)
	{
		ans += min(2*k - s[i], s[i]);
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

int n, ans, s[100010], univ[100010], k;
vector<vector<int> > gr;
vector<int> vi;

void dfs(int x, int par){
	if(univ[x]){
		++s[x];
	}
	for(auto i : gr[x]){
		if(i != par){
			dfs(i, x);
		} else {
			continue;
		}
		s[x] += s[i];
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> k;
	gr.resize(n, vi);
	for(int i = 0; i < 2*k; ++i){
		int x;
		cin >> x;
		univ[x-1] = 1;
	}
	for(int i =0 ; i < n-1; ++i){
		int u,v;
		cin >> u >> v;
		--u, v--;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}
	dfs(0, -1);
	for (int i = 0; i < n; ++i)
	{
		ans += min(2*k - s[i], s[i]);
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

int n, ans, s[200010], univ[200010], k;
vector<vector<int> > gr;
vector<int> vi;

void dfs(int x, int par){
	if(univ[x]){
		++s[x];
	}
	for(auto i : gr[x]){
		if(i != par){
			dfs(i, x);
		} else {
			continue;
		}
		s[x] += s[i];
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> k;
	gr.resize(n, vi);
	for(int i = 0; i < 2*k; ++i){
		int x;
		cin >> x;
		univ[x-1] = 1;
	}
	for(int i =0 ; i < n-1; ++i){
		int u,v;
		cin >> u >> v;
		--u, v--;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}
	dfs(0, -1);
	for (int i = 0; i < n; ++i)
	{
		ans += min(2*k - s[i], s[i]);
	}
	cout << ans << "\n";
}


#include<bits/stdc++.h>
#include<stdio.h>
#include<map>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int lu;
typedef vector< int > vi;
typedef vector < ll > vl;
typedef set <ll> sl;
typedef vector< vi > vvi;
typedef vector< vl > vvl;
typedef pair< int,int > ii;
typedef pair< ll,ll > il;
#define sz(a) ll((a).size())
#define test(t) ll t;cin>>t;while(t--)
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define Vi vector
#define pr pair
#define mpr make_pair
#define fi first
#define se second
#define isnum(a) (a>=48 && a<=57)
#define isS(a) (a>='a' && a<='z')
#define isU(a) (a>='A' && a<='Z')
#define toS(a) (isU(a)?a+32:a)
#define toU(a) (isS(a)?a-32:a)
#define toC(a) a&15

set <ll> st[100005];
ll d[100005];

vector<char> prime (100000+1, true);
void sieve(ll n)
{
	//O(nloglogn) time
	prime[0] = prime[1] = false;
	for (ll i=2; i<=n; ++i)
	{
		if (prime[i])
		{
			st[i].insert(i);
			for (ll j=2*i; j<=n; j+=i)
			{
				prime[j] = false;
				st[j].insert(i);
			}
		}
	}
}
#define forn(i,e) for(int i = 0; i < e; i++)
#define rep(i,a,b) for(i=a;i<b;i++)
#define REP(i,a,b) for(i=a;i<=b;i++)
#define reprev(i,a,b) for(i=a;i>=b;i--)

// Convex Hull Trick
typedef ll LT;
typedef pair<LT,LT> LP;
struct CHT {
  deque<LP> d;
  LT get(LT x) {
    while (sz(d) >= 2) {
      LT a = d[0].fi*x + d[0].se;
      LT b = d[1].fi*x + d[1].se;
      if (a <= b) break; // get min
      d.pop_front();
    }
    return d[0].fi*x + d[0].se;
  }
  void add(LP x) { // decreasing order of x.fi
    while (sz(d) >= 2) {
      LP y = d[sz(d) - 1];
      LP z = d[sz(d) - 2];
      if ((x.se-y.se)*(z.fi-y.fi) >= (y.fi-x.fi)*(y.se-z.se)) break; // both
      d.pop_back();
    }
    d.pb(x);
  }
};
//

#define leadzero(a) __builtin_clz(a) //count leading zeros
#define trailzero(a) __builtin_ctz(a) //count trailing zeros
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln cout<<"\n"
//#define sp cout<<" "
//#define mp make_pair
#define maxe(a) *max_element(a.begin(),a.end())
#define maxi(a) max_element(a.begin(),a.end())-a.begin()
#define mine(a) *min_element(a.begin(),a.end())
#define mini(a) min_element(a.begin(),a.end())-a.begin()
//ll gcd(ll a, ll b) { if(b == 0) return a; return gcd(b, a % b); }
ll fastpowMOD(ll a, ll p,ll MOD){ if(p==0) return 1; ll z = fastpowMOD(a,p/2,MOD); z = (z*z)%MOD; if(p%2) z = (z*a)%MOD; return z; }
ll fastpow(ll a, ll p){ if(p==0) return 1; ll z = fastpow(a,p/2); z = (z*z); if(p%2) z = (z*a); return z; }
//CHT stands for convex hull trick
ll search1(ll a[], ll f,ll n)
{
	ll left=1;
	ll right=n+1;
	while(right-left>1)
	{
		ll mid = left + (right-left)/2;
		if(a[mid]<f)
		{
			left=mid;
		}
		else right=mid;
	}
	return a[left];
}

const ll INF = 1e18;
vector<ll> lazy;
vector<ll> tree;

void updateRange(ll node, ll start, ll end, ll l, ll r, ll val)
{
    if(lazy[node] != 0)
    {
        // This node needs to be updated
        tree[node] += (/*end - start*/ + 1) * lazy[node];    // Update it
        if(start != end)
        {
            lazy[node*2] += lazy[node];                  // Mark child as lazy
            lazy[node*2+1] += lazy[node];                // Mark child as lazy
        }
        lazy[node] = 0;                                  // Reset it
    }
    if(start > end or start > r or end < l)              // Current segment is not within range [l, r]
        return;
    if(start >= l and end <= r)
    {
        // Segment is fully within range
        lazy[node] += (/*end - start*/ + 1) * val;
        if(start != end)
        {
            // Not leaf node
            lazy[node*2] += lazy[node];
            lazy[node*2+1] += lazy[node];
        }
        tree[node]+=lazy[node];
        lazy[node]=0;
        return;
    }
    ll mid = (start + end) / 2;
    updateRange(node*2, start, mid, l, r, val);        // Updating left child
    updateRange(node*2 + 1, mid + 1, end, l, r, val);   // Updating right child
    tree[node] = max(tree[node*2],tree[node*2+1]);        // Updating root with max value
}

ll queryRange(ll node, ll start, ll end, ll l, ll r)
{
	if(lazy[node] != 0)
    {
        // This node needs to be updated
        tree[node] += (/*end - start +*/ 1) * lazy[node];            // Update it
        if(start != end)
        {
            lazy[node*2] += lazy[node];         // Mark child as lazy
            lazy[node*2+1] += lazy[node];    // Mark child as lazy
        }
        lazy[node] = 0;                 // Reset it
    }

    if(start > end or start > r or end < l)
        return -INF;         // Out of range

    if(start >= l and end <= r)             // Current segment is totally within range [l, r]
        return tree[node];
    ll mid = (start + end) / 2;
    if(r<=mid)return queryRange(node*2, start, mid, l, r);
    else if(l>=mid+1) return queryRange(node*2 + 1, mid + 1, end, l, r);
    ll p1 = queryRange(node*2, start, mid, l, r);         // Query left child
    ll p2 = queryRange(node*2 + 1, mid + 1, end, l, r); // Query right child
    //tree[node] = max(tree[node*2],tree[node*2+1]);
    return max(p1,p2);
}

void convert_reduce(ll arr[], ll n)
{
    vector <pair<ll, ll> > v;
    for (ll i = 0; i < n; i++)v.push_back(make_pair(arr[i], i));
    sort(v.begin(), v.end());
    ll cnt = 0;
    for (ll i=0; i<n; i++)
    {
        arr[v[i].second] = cnt;
        ll j = i;
        while(j<n && v[j].fi==v[i].fi)arr[v[j++].second] = cnt;
        i=j-1;
        cnt++;
    }
}
set<ll> s;
vector<ll> adj[(ll)2e5+5];
ll subt[(ll)2e5+5];
ll ans;
void dfs(ll i,ll par)
{
    if(s.find(i)!=s.end())subt[i]=1;
    for(ll x : adj[i])
    {
        if(x==par)continue;
        dfs(x,i);
        subt[i]+=subt[x];
    }
}
void dfs1(ll i,ll par,ll k,ll n)
{
    if(i!=1)
    {
        ans+=min(subt[i],2*k-subt[i]);
    }
    for(ll x : adj[i])
    {
        if(x==par)continue;
        dfs1(x,i,k,n);
    }
}
int main(){
    fastio;
    ll n,k,i,j;
    cin>>n>>k;
    rep(i,0,2*k){cin>>j;s.insert(j);}
    rep(i,0,n-1)
    {
        ll x,y;
        cin>>x>>y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    memset(subt,0,sizeof(subt));
    dfs(1,-1);
    ans=0;
    dfs1(1,-1,k,n);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
bool vis[maxn];
ll cnt[maxn];
vector<int>vec[maxn];
ll ans;
int n,k;
void dfs(int now,int pre)
{
    if(vis[now])cnt[now] = 1;
    for(int i = 0; i < vec[now].size(); i++)
    {
        int v = vec[now][i];
        if(v == pre)continue;
        dfs(v,now);
        cnt[now] += cnt[v];
    }
    ans += min(2 * k - cnt[now],cnt[now]);
}
int main()
{

    cin>>n>>k;
    for(int i = 1; i <= 2 * k; i++)
    {
        int x;cin>>x;
        vis[x] = true;
    }
    for(int i = 1; i <= n - 1; i++)
    {
        int u,v;
        cin>>u>>v;
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs(1,-1);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
bool vis[maxn];
int cnt[maxn];
vector<int>vec[maxn];
int ans;
int n,k;
void dfs(int now,int pre)
{
    if(vis[now])cnt[now] = 1;
    for(int i = 0; i < vec[now].size(); i++)
    {
        int v = vec[now][i];
        if(v == pre)continue;
        dfs(v,now);
        cnt[now] += cnt[v];
    }
    ans += min(2 * k - cnt[now],cnt[now]);
}
int main()
{

    cin>>n>>k;
    for(int i = 1; i <= 2 * k; i++)
    {
        int x;cin>>x;
        vis[x] = true;
    }
    for(int i = 1; i <= n - 1; i++)
    {
        int u,v;
        cin>>u>>v;
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs(1,-1);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define pii pair<int,int>
#define read(x) scanf("%d",&x)
#define dread(x) scanf("%lf",&x)
#define lread(x) scanf("%lld",&x)
using namespace std;

typedef long long ll;
const int N=2e5+7;
const int mod=1e9+7;
const int base=17;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;

int n,k,head[N],tot;
bool flag[N];
struct node
{
    int v,nx,sum;
}e[N<<1];
void add(int u,int v)
{
    e[tot].v=v; e[tot].sum=0;
    e[tot].nx=head[u];
    head[u]=tot++;
}
int dfs(int u,int p)
{
    int ans=0;
    if(flag[u]) ans=1;
    for(int i=head[u];~i;i=e[i].nx)
    {
        int v=e[i].v;
        if(v==p) continue;
        int ret=dfs(v,u);
        e[i].sum=ret;
        ans+=ret;
    }
    return ans;
}
int main()
{
    memset(head,-1,sizeof(head));
    read(n); read(k); k*=2;
    for(int i=1;i<=k;i++)
    {
        int x; read(x);
        flag[x]=true;
    }
    for(int i=1;i<n;i++)
    {
        int u,v;
        read(u); read(v);
        add(u,v); add(v,u);
    }
    ll ans=0;
    dfs(1,-1);
    for(int i=0;i<tot;i++)
        ans+=min(e[i].sum,k-e[i].sum);
    printf("%lld\n",ans);
    return 0;
}
/*
x/v2+(l-x)/v1==T
x/v2+l/v1-x/v1==T
x/v2-x/v1==T-l/v1
x=(T-l/v1)*v1*v2/(v1-v2);
*/


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define pii pair<int,int>
#define read(x) scanf("%d",&x)
#define dread(x) scanf("%lf",&x)
#define lread(x) scanf("%lld",&x)
using namespace std;

typedef long long ll;
const int N=2e5+7;
const int mod=1e9+7;
const int base=17;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;

int n,k,head[N],tot;
bool flag[N];
struct node
{
    int v,nx,sum;
}e[N];
void add(int u,int v)
{
    e[tot].v=v; e[tot].sum=0;
    e[tot].nx=head[u];
    head[u]=tot++;
}
int dfs(int u,int p)
{
    //printf("%d ##\n",u);
    int ans=0;
    if(flag[u]) ans=1;
    for(int i=head[u];~i;i=e[i].nx)
    {
        int v=e[i].v;
        if(v==p) continue;
        int ret=dfs(v,u);
        e[i].sum=ret;
        ans+=ret;
    }
    return ans;
}
int main()
{
    memset(head,-1,sizeof(head));
    read(n); read(k); k*=2;
    for(int i=1;i<=k;i++)
    {
        int x; read(x);
        flag[x]=true;
    }
    for(int i=1;i<n;i++)
    {
        int u,v;
        read(u); read(v);
        add(u,v); add(v,u);
    }
    ll ans=0;
    dfs(1,-1);
    for(int i=0;i<tot;i++)
    {
        ans+=min(e[i].sum,k-e[i].sum);
        //printf("%d\n",e[i].sum);
    }
    printf("%lld\n",ans);
    return 0;
}
/*
x/v2+(l-x)/v1==T
x/v2+l/v1-x/v1==T
x/v2-x/v1==T-l/v1
x=(T-l/v1)*v1*v2/(v1-v2);
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int mark[200005];
vector<int> g[200005];
int sz[200005];
LL ans = 0;
int n, k;
int dfs1(int u, int p){
	sz[u] = mark[u];
	for(int i=0;i<g[u].size();i++){
		int v = g[u][i];
		if(v == p) continue;
		dfs1(v, u);
		sz[u] += sz[v];
	}
}
int dfs2(int u, int p){
	int tot = mark[u];
	int rem = k-sz[u];
	for(int i=0;i<g[u].size();i++){
		int v = g[u][i];
		if(v == p) continue;
		int tmp = dfs2(v, u);
		ans += tmp;
		tot += tmp;
	}
	tot = min(tot, rem);
	return tot;
}
int main(){
	scanf("%d%d", &n, &k);
	k*=2;
	for(int i=1;i<=k;i++){
		int a;
		scanf("%d", &a);
		mark[a] = 1;
	}
	for(int i=1;i<=n-1;i++){
		int a, b;
		scanf("%d%d", &a, &b);
		g[a].emplace_back(b);
		g[b].emplace_back(a);
	}
	dfs1(1, 0);
	dfs2(1, 0);
	printf("%lld\n", ans);
}


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

const int N=2e5+1;
int n,k,sz[N],uni[N];
vector<int>adj[N];

void dfs(int u,int pre)
{
    sz[u]=uni[u];
    for(auto&v:adj[u])
    {
        if(v!=pre)
        {
            dfs(v,u);
            sz[u]+=sz[v];
        }
    }
}

main()
{
    cin>>n>>k;
    int num=k*2;
    while(num--)
    {
        int x;
        cin>>x;
        uni[x]=1;
    }
    for(int i=1;i<n;i++)
    {
        int u,v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1,0);
    int res=0;
    for(int i=1;i<=n;i++) res+=min(sz[i],2*k-sz[i]);
    cout<<res;
}


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

const int N=2e5+1;
int n,k,sz[N],uni[N];
vector<int>adj[N];

void dfs(int u,int pre)
{
    sz[u]=uni[u];
    for(auto&v:adj[u])
    {
        if(v!=pre)
        {
            dfs(v,u);
            sz[u]+=sz[v];
        }
    }
}

int main()
{
    cin>>n>>k;
    int num=k*2;
    while(num--)
    {
        int x;
        cin>>x;
        uni[x]=1;
    }
    for(int i=1;i<n;i++)
    {
        int u,v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1,0);
    int res=0;
    for(int i=1;i<=n;i++) res+=min(sz[i],2*k-sz[i]);
    cout<<res;
}


// editorial
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

void dfs(vector<vector<int>>& g, vector<vector<int>>& dp, vector<int>& dis, vector<int>& depth, int parent, int u, int& time) {
    dis[u] = time++;
    dp[0][u] = parent;
    depth[u] = 1 + depth[parent];
	for (int v : g[u]) {
        if (v != parent) {
		  dfs(g, dp, dis, depth, u, v, time);
        }
	}
}

int lca (vector<vector<int>>& dp, vector<int>& depth, int u, int v) {
    if (depth[u] < depth[v]) {
        swap(u, v);
    }
    // cout<<"depth: "<<u<<" "<<v<<" "<<depth[u]<<" "<<depth[v]<<endl;
    for (int i = 17; i >= 0; i--) {
        if (depth[u]-(1<<i) >= depth[v]) {
            u = dp[i][u];
        }
    }
    // cout<<"depth same: "<<u<<" "<<v<<endl;
    if (u == v) {
        // cout<<"lca: "<<u<<endl;
        return u;
    }
    for (int i = 17; i >= 0; i--) {
        if (dp[i][u] != dp[i][v]) {
            u = dp[i][u];
            v = dp[i][v];
        }
    }
    // cout<<"lca: "<<dp[0][u]<<endl;
    return dp[0][u];
}

int dist (vector<vector<int>>& dp, vector<int>& depth, int u, int v) {
    // cout<<"dist for: "<<u<<" "<<v<<endl;
    return depth[u] + depth[v] - 2*depth[lca(dp, depth, u, v)];
}

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

	int n, k, u, v;
	cin>>n>>k;
    vector<int> uni(2*k);
    for (int i = 0; i < 2*k; i++) {
        cin>>uni[i];
    }
	vector<vector<int>> g(n+1);
	for (int i = 1; i < n; i++) {
		cin>>u>>v;
		g[u].push_back(v);
        g[v].push_back(u);
	}
    vector<int> dis(n+1, 0);
    vector<int> depth(n+1, -1);
    vector<vector<int>> dp(18, vector<int>(n+1));
	int time = 1;
    dfs(g, dp, dis, depth, 0, 1, time);
    for (int i = 1; i < 18; i++) {
        for (int j = 1; j <= n; j++) {
            dp[i][j] = dp[i-1][dp[i-1][j]];
        }
    }
    sort(uni.begin(), uni.end(), [&dis] (const int& i, const int& j) {
        return dis[i] < dis[j];
    });
    long long ans = 0;
    for (int i = 0; i < k; i++) {
        ans += dist(dp, depth, uni[i], uni[i+k]);
    }
    cout<<ans<<endl;
}


// editorial
// lca ref: https://codeforces.com/contest/700/submission/19370545
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

void dfs(int k, vector<vector<int>>& g, vector<bool>& isUni, vector<int>& sub, int parent, int u, long long& ans) {
    sub[u] = isUni[u];
	for (int v : g[u]) {
        if (v != parent) {
            dfs(k, g, isUni, sub, u, v, ans);
            sub[u] += sub[v];
        }
	}
    ans += min(sub[u], 2*k-sub[u]);
}

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

	int n, k, u, v;
	cin>>n>>k;
    vector<bool> isUni(n+1, false);
    for (int i = 0; i < 2*k; i++) {
        cin>>u;
        isUni[u] = true;
    }
	vector<vector<int>> g(n+1);
	for (int i = 1; i < n; i++) {
		cin>>u>>v;
		g[u].push_back(v);
        g[v].push_back(u);
	}
    vector<int> sub(n+1, 0);
	long long ans = 0;
    dfs(k, g, isUni, sub, 0, 1, ans);
    cout<<ans<<endl;
}


// editorial
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

void dfs(vector<vector<int>>& g, vector<vector<int>>& dp, vector<int>& dis, vector<int>& depth, int parent, int u, int& time) {
    dis[u] = time++;
    dp[0][u] = parent;
    depth[u] = 1 + depth[parent];
	for (int v : g[u]) {
        if (v != parent) {
		  dfs(g, dp, dis, depth, u, v, time);
        }
	}
}

int lca (vector<vector<int>>& dp, vector<int>& depth, int u, int v) {
    if (depth[u] < depth[v]) {
        swap(u, v);
    }
    // cout<<"depth: "<<u<<" "<<v<<" "<<depth[u]<<" "<<depth[v]<<endl;
    for (int i = 17; i >= 0; i--) {
        if (depth[u]-(1<<i) >= depth[v]) {
            u = dp[i][u];
        }
    }
    // cout<<"depth same: "<<u<<" "<<v<<endl;
    if (u == v) {
        // cout<<"lca: "<<u<<endl;
        return u;
    }
    for (int i = 17; i >= 0; i--) {
        if (dp[i][u] != dp[i][v]) {
            u = dp[i][u];
            v = dp[i][v];
        }
    }
    // cout<<"lca: "<<dp[0][u]<<endl;
    return dp[0][u];
}

int dist (vector<vector<int>>& dp, vector<int>& depth, int u, int v) {
    // cout<<"dist for: "<<u<<" "<<v<<endl;
    return depth[u] + depth[v] - 2*depth[lca(dp, depth, u, v)];
}

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

	int n, k, u, v;
	cin>>n>>k;
    vector<int> uni(2*k);
    for (int i = 0; i < 2*k; i++) {
        cin>>uni[i];
    }
	vector<vector<int>> g(n+1);
	for (int i = 1; i < n; i++) {
		cin>>u>>v;
		g[u].push_back(v);
        g[v].push_back(u);
	}
    vector<int> dis(n+1, 0);
    vector<int> depth(n+1, -1);
    vector<vector<int>> dp(18, vector<int>(n+1));
	int time = 1;
    dfs(g, dp, dis, depth, 0, 1, time);
    for (int i = 1; i < 18; i++) {
        for (int j = 1; j <= n; j++) {
            dp[i][j] = dp[i-1][dp[i-1][j]];
        }
    }
    sort(uni.begin(), uni.end(), [&dis] (const int& i, const int& j) {
        return dis[i] < dis[j];
    });
    int ans = 0;
    for (int i = 0; i < k; i++) {
        ans += dist(dp, depth, uni[i], uni[i+k]);
    }
    cout<<ans<<endl;
}


// And Then the GOD Entered..
#include<bits/stdc++.h>
 using namespace std;

typedef long long int ll;
#define endl "\n"
#define fi first
#define sec second
#define pb push_back
#define test cout<<"debuged\n";
#define INF ll(1e18)
#define MOD 1000000007
#define debug(x) cout<<x<<"\n";
#define pll pair<ll,ll>
#define mp make_pair
#define Minato_Namikaze ios_base::sync_with_stdio(false) , cin.tie(NULL);
#define all(v) v.begin(),v.end()
#define MAX 200015

vector<ll> adj[MAX], dp(MAX);
map<ll, ll> ma;
ll n, k;
ll ans = 0;

void dfs(ll cur, ll par)
{
    ll f = 0;
    dp[cur] = 0;
    if(ma.count(cur) >= 1)
        dp[cur]++;
    for(auto x : adj[cur])
    {
        if(x == par)
            continue;
        dfs(x, cur);
        dp[cur] += dp[x];
    }
    ans += min(dp[cur], 2LL * k - dp[cur]);
}

int main()
{
    Minato_Namikaze
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    cin >> n >> k;
    for(ll i = 0; i < 2 * k; ++i)
    {
        ll x;
        cin >> x;
        ma[x]++;
    }
    for(ll i = 0; i < n - 1; ++i)
    {
        ll x, y;
        cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    dfs(1, -1);
    cout << ans << endl;
}


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

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=2e9;
const int maxn=200000+100;

int k;
vector<int>g[maxn];
bool uni[maxn];
int sz[maxn];
int ans;
void dfs(int v,int p=0){
	sz[v]=uni[v];
	for(int i:g[v])
		if(i!=p){dfs(i,v);sz[v]+=sz[i];}
}

void get(int v,int p=0){
	for(int i:g[v])
		if(i!=p){ans+=min((2*k)-sz[i],sz[i]);get(i,v);}
}

int main(){
	int n;
	cin>>n>>k;
	for(int i=0;i<k*2;i++){
		int v;
		cin>>v;
		uni[v]=true;
	}
	for(int i=0;i<n-1;i++){
		int t,k;
		cin>>t>>k;
		g[t].pb(k);
		g[k].pb(t);
	}
	dfs(1);
	get(1);
	cout<<ans;
}


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

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=2e9;
const int maxn=200000+100;

int k;
vector<int>g[maxn];
bool uni[maxn];
ll sz[maxn];
ll ans;
void dfs(int v,int p=0){
	sz[v]=uni[v];
	for(int i:g[v])
		if(i!=p){dfs(i,v);sz[v]+=sz[i];}
}

void get(int v,int p=0){
	for(int i:g[v])
		if(i!=p){ans+=min((2*k)-sz[i],sz[i]);get(i,v);}
}

int main(){
	int n;
	cin>>n>>k;
	for(int i=0;i<k*2;i++){
		int v;
		cin>>v;
		uni[v]=true;
	}
	for(int i=0;i<n-1;i++){
		int t,k;
		cin>>t>>k;
		g[t].pb(k);
		g[k].pb(t);
	}
	dfs(1);
	get(1);
	cout<<ans;
}


#include<iostream>
#include<vector>

using namespace std;

const int Max = 200000 + 10;
int n, k;
int uni[Max];
long long count[Max];
vector<vector<int>> edges(Max);

void read(){
	cin >> n >> k;
	for(int i = 1; i <= 2 * k; i++){
		int u;
		cin >> u;
		uni[u] = 1;
	}
	for(int i = 1; i <= n - 1; i++){
		int x, y;
		cin >> x >> y;
		edges[x].push_back(y);
		edges[y].push_back(x);
	}
}

long long dfs(int node, int parent){
	if(uni[node]){
		count[node]++;
	}
	for(int i : edges[node]){
		if(i != parent){
			count[node] += dfs(i , node);
		}
	}
	return count[node];
}

int main(){
	read();
	dfs(1, -1);
	long long result = 0;
	for(int i = 2 ; i <= n; i++){
		int minimum = count[i];
		if(minimum > 2 * k - count[i]){
			minimum = 2 * k - count[i];
		}
		result += 1LL * minimum;
	}
	cout << result;
	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

const int Max = 200000 + 10;
int n, k;
int uni[Max], count[Max];
vector<vector<int>> edges(Max);

void read(){
	cin >> n >> k;
	for(int i = 1; i <= 2 * k; i++){
		int u;
		cin >> u;
		uni[u] = 1;
	}
	for(int i = 1; i <= n - 1; i++){
		int x, y;
		cin >> x >> y;
		edges[x].push_back(y);
		edges[y].push_back(x);
	}
}

int dfs(int node, int parent){
	if(uni[node]){
		count[node]++;
	}
	for(int i : edges[node]){
		if(i != parent){
			count[node] += dfs(i , node);
		}
	}
	return count[node];
}

int main(){
	read();
	dfs(1, -1);
	int result = 0;
	for(int i = 2 ; i <= n; i++){
		int minimum = count[i];
		if(minimum > 2 * k - count[i]){
			minimum = 2 * k - count[i];
		}
		result += minimum;
	}
	cout << result;
	return 0;
}


#include<iostream>
#include<map>
#include<vector>

using namespace std;

const int Max = 2000;
const float inf = 47474859;
int n;
vector<pair<double, double>> pairs;
map<pair<double, double>, long long> m;

void read(){
	cin >> n;
	for(int i = 1 ; i <= n; i++){
		double x , y ;
		cin >> x >> y;
		pairs.push_back(make_pair(x, y));
	}
}

int solve(){
	for(int i = 0; i < n - 1; i++){
		for(int j = i + 1; j < n; j++){
			double panta;
			if(pairs[i].first == pairs[j].first){
				panta = inf;
			}
			else{
				panta = (pairs[j].second - pairs[i].second)/(pairs[j].first - pairs[i].first);
			}
			double lung = 1LL * (pairs[j].second - pairs[i].second) *(pairs[j].second - pairs[i].second);
			lung += 1LL * (pairs[j].first -pairs[i].first) * (pairs[j].first - pairs[i].first);
			m[make_pair(panta , lung)]++;
		}
	}
	long long res = 0;
	for(auto it = m.begin(); it != m.end(); it++){
		int numb = it->second;
		if(numb >= 2){
			res += (1LL * numb * (numb - 1))/2;
		}
	}
	return res / 2;
}

int main(){
	read();
	cout << solve();
	return 0;
}


// È¥°É£¡Æ¤¿¨Çð! °ÑAC´ø»ØÀ´£¡
//      ¤Ø¡¡¡¡¡¡¡¡¡¡£¯|
// ¡¡¡¡/£Ü7¡¡¡¡¡¡ ¡Ï£ß/
// ¡¡ /¡¡©¦¡¡¡¡ £¯¡¡£¯								AC		AC		AC		AC
//    ©¦¡¡Z £ß,£¼¡¡£¯¡¡¡¡ /`©c						AC		AC		AC		AC
//    ©¦¡¡¡¡¡¡¡¡¡¡©c¡¡¡¡ /¡¡¡¡¡µ					AC		AC		AC		AC
// ¡¡Y¡¡¡¡¡¡¡¡¡¡`¡¡ /¡¡¡¡/  /						AC		AC		AC		AC
// ¡¡Øé¡ñ¡¡.¡¡¡ñ¡¡¡¡¡´¡¡/¡¡/						AC		AC		AC		AC
// ¡¡()¡¡ ¤Ø¡¡¡¡¡¡¡¡|¡¡£Ü¡´							AC		AC		AC		AC
// ¡¡¡¡>- ._¡¡ ¥£¡¡ ©¦ £¯£¯							AC		AC		AC		AC
// ¡¡ / ¤Ø¡¡¡¡ /¡¡/£¼| £Ü£Ü							AC		AC		AC		AC
// ¡¡ ©c_/¡¡¡¡(_£¯¡¡ ©¦£¯£¯							AC		AC		AC		AC
// ¡¡¡¡ 7¡¡¡¡¡¡¡¡¡¡¡¡¡¡|£¯							AC		AC		AC		AC
// ¡¡¡¡ £¾¨Dr£þ£þ`-¨D£ß/							AC		AC		AC		AC
//**************************************Accepted*****************************************//

#include<bits/stdc++.h>

#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define Unique(x) x.erase(unique(all(x)),x.end());

#define CIN_FILE "input.txt"
#define COUT_FILE "output.txt"

using namespace std;

const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};

typedef long long ll;

int n,k,r,a,b;
long long ans;
vector<int> e[500010];
bool v[500010];

int DFS(int x,int lt)
{
	int nw=v[x];

	for(int i=0;i<e[x].size();i++)
	{
		if(e[x][i]==lt)continue;

		nw+=DFS(e[x][i],x);
	}

	while(nw*2>r)nw-=2,r-=2;

	ans+=nw;

	return nw;
}

int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);

	cin>>n>>k;
	r=k*2;
	for(int i=0;i<r;i++)
	{
		cin>>a;
		v[a]=1;
	}

	for(int i=1;i<n;i++)
	{
		cin>>a>>b;

		e[a].pb(b);
		e[b].pb(a);
	}

	DFS(1,1);

	cout<<ans;

	//printf("Time used = %.4f",(double)(clock())/CLOCKS_PER_SEC);
	return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define LB lower_bound
#define UB upper_bound
#define SP fixed << setprecision(10)

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef double DB;

const int N = 200100;

int n , k , num[N];
ll ans(0);
vector <int> adj[N];

void DFS(int v , int par){

	for(int u : adj[v])
	   if(u != par)
		  DFS(u , v);

	while(num[v] > k){
	    k--;
	    num[v] -= 2;
	}

	if(v){
		num[par] += num[v];
		ans += num[v];
		num[v] = 0;
	}

	return;
}

int main(){

	ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int u , v;
	cin >> n >> k;

	for(int i = 0 ; i < 2*k ; i++){
	   cin >> u;
	   num[--u]++;
	}

	for(int i = 1 ; i < n ; i++){
	   cin >> u >> v;
	   adj[--u].PB(--v);
	   adj[v].PB(u);
	}

	DFS(0 , 0);
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
#define lcm(x, y) ((x) * (y) / __gcd((x), (y)))
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define debug(x) cout << #x << " : " << x << endl
#define part cout << "----------------------------------\n";
#include <iostream>

int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; // trick to explore an implicit 2D grid
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; // S,SE,E,NE,N,NW,W,SW neighbors  //GO FOR EVEN FOR 4 moves

#define fastinput                     \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

LL POW(LL x, LL y)
{
    LL ans = 1;
    LL base = x;
    while (y)
    {
        if (y & 1)
        {
            ans *= base;
        }
        base = base * base;
        y = y >> 1;
    }
    return ans;
}
const LL M=2e5+1;
vector<LL> sub(M,0);
vector<LL> adj[M];
unordered_set<LL> se;
LL ans=0;
LL tot;
void dfs(LL s,LL par=0)
{
    sub[s]=0;
    //debug(s);
   // debug(adj[s].size());
    for(auto x:adj[s])
    {
        if(x!=par)
        {
           // debug(x);
            dfs(x,s);
            sub[s]+=sub[x];
        }
    }
    if(se.find(s)!=se.end())
    {
        sub[s]++;
    }
    //debug(s);
    //debug(sub[s]);
    LL add=min(sub[s],tot-sub[s]);
    if(par!=0)
    {
        ans+=add;
    }
}
int main()
{
    fastinput;
    LL n,i,j,k,t,temp,tc;
    cin>>n>>k;
    for(i=1;i<=k;i++)
    {
        cin>>j>>t;
        se.insert(j);
        se.insert(t);
    }
    tot=2*k;
    for(i=1;i<=n-1;i++)
    {
        cin>>j>>t;
        adj[j].pb(t);
        adj[t].pb(j);
    }
    dfs(1,0);
    cout<<ans<<endl;

    return 0;
}


for _ in " "*int(input()):
  n,ans=int(input()),0
  for j in range(1,40):
    d=2**j-1
    s=d*(d+1)/2
    if n>=s:
      n-=s
      ans+=1
    else:
      break
  print(ans)


for _ in " "*int(input()):
	n,ans=int(input()),0
	for i in range(1,40):
		x=((2**i-1)*((2**i-1)+1))//2
		if x>n:
			break
		else:
			n-=x
			ans+=1
	print(ans)


t=int(input())
for i in range(t):
	n=int(input())
	ans=0
	for j in range(1,40):
		d=2**j-1
		s=d*(d+1)/2
		if n>=s:
			n-=s
			ans+=1
	print (ans)


for _ in " "*int(input()):
  n,ans=int(input()),0
  for j in range(1,40):
    d=2**j-1
    s=d*(d+1)/2
    if n>=s:
      n-=s
      ans+=1
  print(ans)


t=input()

for i in range(t):
	n=int(input())
	ans=0
	for j in range(1,40):
		d=2**j-1
		s=d*(d+1)/2
		if n>=s:
			n-=s
			ans+=1
	print (ans)


t=int(input())
for _ in range(t):
    n=int(input())
    ans=0
    s=0
    i=1
    while (n>s):
        s=s+(i*(i+1))//2
        if s>n:
            break
        else:
            ans=ans+1
        i=2*i
        i=i+1
    print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    ans=0
    s=0
    i=1
    while (n>s):
        s=s+(i*(i+1))//2
        if s>n:
            break
        else:
            ans=ans+1
        i=2*i
        i=i+1
print(ans)


def formula(x):
	return sr[x]

ar = [0]*60
r2 = 2
for i in range(1,60):
	ar[i] = (r2-1)*(r2)//2
	r2 *= 2
sr = [0]*60
sr[1] = ar[1]
for i in range(2,60):
	sr[i] = sr[i-1] + ar[i]

t = int(input())

for _ in range(t):
	x = int(input())

	l = 0
	r = 60
	best = 0
	while(l <= r):
		mid = l + (r-l)//2
		# print(mid, formula(mid))
		if formula(mid) <= x:
			best = max(best, mid)
			l = mid+1
		else:
			r = mid-1


def formula(x):
	return sr[x]

ar = [0]*60
r2 = 2
for i in range(1,60):
	ar[i] = (r2-1)*(r2)//2
	r2 *= 2
sr = [0]*60
sr[1] = ar[1]
for i in range(2,60):
	sr[i] = sr[i-1] + ar[i]

t = int(input())

for _ in range(t):
	x = int(input())

	l = 0
	r = 60
	best = 0
	while(l <= r):
		mid = l + (r-l)//2
		# print(mid, formula(mid))
		if formula(mid) <= x:
			best = max(best, mid)
			l = mid+1
		else:
			r = mid-1
	print(best)


for t in range(int(input())):
    n=int(input())
    now,ans=int(1),int(0)
    while(1):
        n-=now*(now+1)/2
        if(n<0): break
        ans+=1
        now=2*now+1
    print(ans)


for t in range(int(input())):
    n=int(input())
    now,ans=int(1),int(0)
    while(1):
        n-=now*(now+1)//2
        if(n<0): break
        ans+=1
        now=2*now+1
    print(ans)


for t in range(int(input())):
    n=int(input())
    now,ans=int(1),int(0)
    while(n>0):
        n-=now*(now+1)//2
        if(n>=0): ans+=1
        now=2*now+1
    print(ans)


for t in range(int(input())):
    n=int(input())
    now,ans=int(1),int(0)
    while(n>0):
        n-=now*(now+1)/2
        if(n>=0): ans+=1
        now=2*now+1
    print(ans)


for t in range(int(input())):
    n=int(input())
    now,ans=int(1),int(0)
    while(n>0):
        n-=now*(now+1)/2
        if(n>=0) ans+=1
        now=2*now+1
    print(ans)


A=[1,2,4,8]
for i in range(30):
    A+=[A[-1]*2]
A=[i-1 for i in A]
B=[(i*(i+1))//2 for i in A]
for i in range(1,len(B)):
    B[i]=B[i]+B[i-1]
print(B)
for _ in range(int(input())):
    X=int(input())
    printed=False
    for i in range(len(B)):
        if X==B[i]:
            print(i)
            printed=True
            break
        elif X<B[i]:
            print(i-1)
            printed=True
            break


A=[1,2,4,8]
for i in range(30):
    A+=[A[-1]*2]
A=[i-1 for i in A]
B=[(i*(i+1))//2 for i in A]

for i in range(1,len(B)):
    B[i]=B[i]+B[i-1]

for _ in range(int(input())):
    X=int(input())
    printed=False
    for i in range(len(B)):
        if X==B[i]:
            print(i)
            printed=True
            break
        elif X<B[i]:
            print(i-1)
            printed=True
            break


for _ in range(int(input())):
    n=int(input())
    a=0
    while n>0:
        a+=1
        n=n-(4**a-2**a)//2
    print(a-(n<0)*1)


import math
for _ in range(int(input())):
    n=int(input())
    print(math.floor(math.log((3+math.sqrt(1+24*n))//4,2)))


import math
for _ in range(int(input())):
    n=int(input())
    print(math.floor(math.log((3+math.sqrt(1+24*n))/4,2)))


t=int(input())
for i in range(t):
    n=int(input())
    i=0
    j=1
    save=0
    while True:
        res=j*(2**i)
        if res>n:
            break
        else:
            save+=res
            j=j*2+1
            i+=1
    if n>=save:
        print(i)
    else:
        print(i-1)


import math
t=int(input())
for q in range(0,t):
    n=int(input())
    curr=1
    i=2
    sum=0
    ans=0
    while True:
        cal=((curr)*(curr+1))//2
        if (sum+cal)<=n:
            sum+=cal
            curr+=i
            i=i*2
            ans+=1
        else:
            break
    print(ans)


import math
t=int(input())
for q in range(0,t):
    n=int(input())
    curr=1
    i=2
    sum=0
    ans=0
    while True:
        cal=((curr)*(curr+1))//2
        if (sum+cal)<=n:
            sum+=curr
            curr+=i
            i=i*2
            ans+=1
        else:
            break
    print(ans)


import math
t=int(input())
for q in range(0,t):
    n=int(input())
    curr=1
    i=2
    sum=0
    ans=0
    while sum<n:
        cal=((curr)*(curr+1))//2
        if (sum+cal)<=n:
            sum+=curr
            curr+=i
            i=i*2
            ans+=1
        else:
            break
    print(ans)


for _ in range(int(input())):
    x = int(input())
    z = 2
    count=0
    for k in range(1,x):
        n = z-1
        s = n*(n+1)//2
        if x<s:break
        x-=s
        count+=1
        z=z*2
    print(count)


def isPossible(m,x):
    if m*(m+1)*(m+2)/6 <=x: return True
    return False

for i in range(int(input())):
    x = int(input())
    l,r=0,x+1
    while r>l+1:
        mid = l+(r-l)//2
        if isPossible(mid,x):
            l=mid
        else:
            r=mid
    print(l)


for _ in range(int(input())):
    x = int(input())
    z = 2
    for k in range(1,x):
        n = z-1
        s = n*(n+1)//2
        if x<s:break
        x-=s
        count+=1
        z=z*2
    print(count)


for _ in range(int(input())):
    x = int(input())
    z = 2
    count=0
    for k in range(1,x+1):
        n = z-1
        s = n*(n+1)//2
        if x<s:break
        x-=s
        count+=1
        z=z*2
    print(count)


def get_s(x):
    return x * (x + 1) / 2

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        ans = 0
        i = 1
        while get_s((1 << i) - 1) <= n:
            n -= get_s((1 << i) - 1)
            i += 1
            ans += 1

        print(ans)


def get_s(x):
    return x * (x + 1) / 2

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        ans = 0
        i = 1
        while get_s((1 << i) - 1) <= n:
            ans += 1
            n -= get_s((1 << i) - 1)
            i += 1

        print(ans)


def get_s(x):
    return x * (x + 1) / 2

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        ans = 0
        h = 1
        while n >= get_s(h):
            ans += 1
            n -= get_s(h)
            h = (h + 1)*2 - 1

        print(ans)


def get_s(x):
    return x * (x + 1) / 2

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        ans = 0
        i = 1
        while get_s(2 ** i - 1) <= n:
            ans += 1
            n -= get_s(2 ** i - 1)
            i += 1

        print(ans)


def get_s(x):
    return x * (x + 1) / 2

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        h = 2
        ans = 0
        while n >= get_s(h - 1):
            n -= get_s(h - 1)
            ans += 1
            h = h*2

        print(ans)


def get_s(x):
    return x * (x + 1) / 2

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        ans = 0
        i = 1
        while get_s(2**i - 1) <= n:
            ans += 1
            n -= get_s(2**i - 1)
            i += 1

        print(ans)


def get_s(x):
    return x * (x + 1) / 2

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        ans = 0
        i = 1
        while 1:
            ans += 1
            n -= get_s(2**i - 1)
            i += 1
            if get_s(2**i - 1) > n:
                break

        print(ans)


def get_s(x):
    return x * (x + 1) / 2

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        ans = 0
        i = 1
        while 1:
            h = 2**i - 1
            ans += 1
            i += 1
            n -= get_s(h)
            if n < get_s(h):
                break

        print(ans)


def get_s(x):
    return x * (x + 1) / 2

def my_ans(n):
    ans = 0
    h = 1
    while n >= get_s(h):
        ans += 1
        n -= get_s(h)
        h = (h + 1) * 2 - 1
    return ans

def his_ans(num):
    n = 0
    x = 0
    while (n <= num):
        x = x + 1
        m = ((2 ** x) - 1)
        n += ((m * (m + 1)) // 2)

    return x - 1

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())

        print(his_ans(n))

# if __name__ == '__main__':
#     t = int(input())
#     for _ in range(t):
#         n = int(input())
#         ans = 0
#         i = 1
#         while 1:
#             ans += 1
#             n -= get_s(2**i - 1)
#             i += 1
#             if get_s(2**i - 1) > n:
#                 break
#
#         print(ans)


def get_s(x):
    return x * (x + 1) / 2

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        ans = 0
        i = 1
        while get_s(2 ** i - 1) <= n:
            n -= get_s(2 ** i - 1)
            i += 1
            ans += 1

        print(ans)


#872#sily
b=[]
k=2
for i in range(30):
    b.append(((k-1)*k)//2)
    k*=2
for _ in range(int(input())):
    n=int(input())
    ss=0
    for i in b:
        if(n>=i):
            n-=i
            ss+=1
    print(ss)


#872#sily
b=[]
k=2
for i in range(30):
    b.append((k-1)*k//2)
    k*=2
b=b[::-1]
for _ in range(int(input())):
    n=int(input())
    ss=0
    for i in b:
        if(n>=i):
            n-=i
            ss+=1
    print(ss)


#872#sily
b=[]
k=2
for i in range(30):
    b.append(((k-1)*k)//2)
    k*=2
b=b[::-1]
for _ in range(int(input())):
    n=int(input())
    ss=0
    for i in b:
        if(n>=i):
            n-=i
            ss+=1
    print(ss)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)

def solve():
    n = int(input())
    t,ans = 2,0
    while t*(t-1)//2<=n:
        n-=(t*(t-1))//2
        ans+=1
        t<<=1
    return ans

t = int(input())
for _ in range(t):
    print(solve())


import bisect
x=[]
for i in range(31):
    if i==0 or i==1:
        x.append(i)
    else:
        x.append(2*x[-1]+2**i)

t=int(input())
for _ in range(t):
    p=int(input())
    j=bisect.bisect_right(x,p)
    print(j-1)


import bisect
x=[]
for i in range(55):
    if i==0:
        x.append(1)
    else:
        x.append(2*x[-1]+4**i)
s=[]
for i in range(len(x)):
    if i==0:
        s.append(1)
    else:
        s.append(s[-1]+x[i])
t=int(input())
for _ in range(t):
    p=int(input())
    j=bisect.bisect_right(s,p)
    print(j)


def func(x):
    return (x*(x+1))//2
t=int(input())
while(t):
    n=int(input())
    i="1"
    ans=0
    while 1:
        temp=func(int(i,2))
        if n-temp>=0:
            ans+=1
            n-=temp
            i+="1"
        else:
            break
    print(ans)
    t-=1


def func(x):
    return (x*(x+1))//2
t=int(input())
while(t):
    n=int(input())
    val=1
    for i in range(1,n):
        if n>=func(i):
            val=i
        else:
            break
    temp=bin(val)[2:]
    if temp.count("1")==len(temp):
        print(len(temp))
    else:
        print(len(temp)-1)
    t-=1


t = int(input())
for game in range(t):
    x = int(input())
    num = 0
    nums = []
    for n in range(50):
        num += 2**n
        nums.append(num)
    x_needed = [(n**2 + n)/2 for n in nums]
    i = 0
    while x >= 0:
        x -= x_needed[i]
        i += 1
    print(i-1)


t = int(input())
for game in range(t):
    x = int(input())
    num = 0
    nums = []
    for n in range(100):
        num += 2**n
        nums.append(num)
    x_needed = [(n**2 + n)/2 for n in nums]
    i = 0
    while x >= 0:
        x -= x_needed[i]
        i += 1
    print(i-1)