// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
using namespace std;

//#undef KVARK_DEBUG

#ifdef KVARK_DEBUG
	#include "../h/debug.h"
#else
#define dbg(...) (void(1));
#define dbg2(...) (void(1));
#define dbg3(...) (void(1));
#define dbg4(...) (void(1));
#define dbg5(...) (void(1));
#define dbgp(...) (void(1));
#define dbg_arr(...) (void(1));
#define dbg_vec(...) (void(1));
#endif

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<pii> > vvpii;
typedef vector<vector<long long> > vvl;
typedef vector<long long> vl;
typedef long long int llint;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))
#define endl "\n"

void task();

int main(){
#ifdef KVARK
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	srand(time(0));
#endif
//	ios_base::sync_with_stdio(0);

	task();

#ifdef KVARK_DEBUG
	my_debug::printProcessInfo();
#endif
	return 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
const int M = 3e5+10;

#define double long double

struct Rib {
	int from;
	int to;
	double f;
	int c;
};

int n, m, x;
vi g[51];
int pt[51];
Rib e[510*2];
int esz = 0;
double W;
int S = 0;
int T = 0;

void pushRib(int a, int b, int c) {
	g[a].push_back(esz);
	e[esz++] = Rib{a, b, 0, c};
	g[b].push_back(esz);
	e[esz++] = Rib{b, a, 0, 0};
}

double dfs(int v, double f) {
	if (v == T)
		return f;
	if (f < W)
		return 0;
	double r = 0;
	for (;pt[v] < g[v].size(); ++pt[v]) {
		Rib& rib = e[g[v][pt[v]]];
		{
			double yet = llint((rib.c - rib.f) / W) * W;
			double add = dfs(rib.to, min(f, yet));
			r += add;
			f -= add;
			rib.f += add;
			e[g[v][pt[v]]^1].f -= add;
			if (f < W)
				return r;
		}
	}
	return r;
}

bool solve() {
	double flow = 0;
	for (int i = 0; i < esz; ++i)
		e[i].f = 0;
	while (1) {
		memset(pt, 0, sizeof(pt));
		double s = dfs(S, INF);
		flow += s;
		if (s < W)
			break;
	}
	return flow / W >= x;
}

void task(){
	cin >> n >> m >> x;
	T = n - 1;
	for (int i = 0; i < m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		--a;
		--b;
		pushRib(a, b, c);
	}
	double l = 0;
	double r = 1e6+1;
	for (int step = 0; step < 64; ++step) {
		W = (l + r) / 2;
		if (solve())
			l = W;
		else
			r = W;
	}
	cout << fixed << setprecision(10) << l * x;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
using namespace std;

//#undef KVARK_DEBUG

#ifdef KVARK_DEBUG
	#include "../h/debug.h"
#else
#define dbg(...) (void(1));
#define dbg2(...) (void(1));
#define dbg3(...) (void(1));
#define dbg4(...) (void(1));
#define dbg5(...) (void(1));
#define dbgp(...) (void(1));
#define dbg_arr(...) (void(1));
#define dbg_vec(...) (void(1));
#endif

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<pii> > vvpii;
typedef vector<vector<long long> > vvl;
typedef vector<long long> vl;
typedef long long int llint;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))
#define endl "\n"

void task();

int main(){
#ifdef KVARK
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	srand(time(0));
#endif
//	ios_base::sync_with_stdio(0);

	task();

#ifdef KVARK_DEBUG
	my_debug::printProcessInfo();
#endif
	return 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
const int M = 3e5+10;

struct Rib {
	int from;
	int to;
	double f;
	int c;
};

int n, m, x;
vi g[51];
int pt[51];
Rib e[510];
int esz = 0;
double W;
int S = 0;
int T = 0;
int d[51];

void pushRib(int a, int b, int c) {
	g[a].push_back(esz);
	e[esz++] = Rib{a, b, 0, c};
	g[b].push_back(esz);
	e[esz++] = Rib{b, a, 0, 0};
}

bool bfs() {
	memset(d, 0x3f, sizeof(d));
	d[S] = 0;
	queue<int> q;
	q.push(S);
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (int i: g[v]) {
			Rib& r = e[i];
			if (d[r.to] == INF && r.c - r.f >= W) {
				d[r.to] = d[v] + 1;
				q.push(r.to);
			}
		}
	}
	return d[T] != INF;
}

double dfs(int v, double f) {
	if (v == T)
		return f;
	if (f < W)
		return 0;
	double r = 0;
	for (;pt[v] < g[v].size(); ++pt[v]) {
		Rib& rib = e[g[v][pt[v]]];
		if (d[v] + 1 == d[rib.to]) {
			double yet = int((rib.c - rib.f) / W) * W;
			double add = dfs(rib.to, min(f, yet));
			r += add;
			f -= add;
			rib.f += add;
			e[g[v][pt[v]]^1].f -= add;
			if (f < W)
				return r;
		}
	}
	return r;
}

bool solve() {
	double flow = 0;
	for (int i = 0; i < esz; ++i)
		e[i].f = 0;
	while (bfs()) {
		memset(pt, 0, sizeof(pt));
		flow += dfs(S, INF);
	}
	return flow / W >= x;
}

void task(){
	cin >> n >> m >> x;
	T = n - 1;
	for (int i = 0; i < m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		--a;
		--b;
		pushRib(a, b, c);
	}
	double l = 0;
	double r = 1e6+1;
	while (r - l > 1e-6) {
		W = (l + r) / 2;
		if (solve())
			l = W;
		else
			r = W;
	}
	cout << fixed << setprecision(10) << l * x;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
using namespace std;

//#undef KVARK_DEBUG

#ifdef KVARK_DEBUG
	#include "../h/debug.h"
#else
#define dbg(...) (void(1));
#define dbg2(...) (void(1));
#define dbg3(...) (void(1));
#define dbg4(...) (void(1));
#define dbg5(...) (void(1));
#define dbgp(...) (void(1));
#define dbg_arr(...) (void(1));
#define dbg_vec(...) (void(1));
#endif

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<pii> > vvpii;
typedef vector<vector<long long> > vvl;
typedef vector<long long> vl;
typedef long long int llint;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))
#define endl "\n"

void task();

int main(){
#ifdef KVARK
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	srand(time(0));
#endif
//	ios_base::sync_with_stdio(0);

	task();

#ifdef KVARK_DEBUG
	my_debug::printProcessInfo();
#endif
	return 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
const int M = 3e5+10;

#define double long double

struct Rib {
	int from;
	int to;
	double f;
	int c;
};

int n, m, x;
vi g[51];
int pt[51];
Rib e[510*2];
int esz = 0;
double W;
int S = 0;
int T = 0;
int d[51];

void pushRib(int a, int b, int c) {
	g[a].push_back(esz);
	e[esz++] = Rib{a, b, 0, c};
	g[b].push_back(esz);
	e[esz++] = Rib{b, a, 0, 0};
}

bool bfs() {
	memset(d, 0x3f, sizeof(d));
	d[S] = 0;
	queue<int> q;
	q.push(S);
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (int i: g[v]) {
			Rib& r = e[i];
			if (d[r.to] == INF && r.c - r.f >= W) {
				d[r.to] = d[v] + 1;
				q.push(r.to);
			}
		}
	}
	return d[T] != INF;
}

double dfs(int v, double f) {
	if (v == T)
		return f;
	if (f < W)
		return 0;
	double r = 0;
	for (;pt[v] < g[v].size(); ++pt[v]) {
		Rib& rib = e[g[v][pt[v]]];
		if (d[v] + 1 == d[rib.to]) {
			double yet = int((rib.c - rib.f) / W) * W;
			double add = dfs(rib.to, min(f, yet));
			r += add;
			f -= add;
			rib.f += add;
			e[g[v][pt[v]]^1].f -= add;
			if (f < W)
				return r;
		}
	}
	return r;
}

bool solve() {
	double flow = 0;
	for (int i = 0; i < esz; ++i)
		e[i].f = 0;
	while (bfs()) {
		memset(pt, 0, sizeof(pt));
		flow += dfs(S, INF);
	}
	return flow / W >= x;
}

void task(){
	cin >> n >> m >> x;
	T = n - 1;
	for (int i = 0; i < m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		--a;
		--b;
		pushRib(a, b, c);
	}
	double l = 0;
	double r = 1e6+1;
	for (int step = 0; step < 55; ++step) {
		W = (l + r) / 2;
		if (solve())
			l = W;
		else
			r = W;
	}
	cout << fixed << setprecision(10) << l * x;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
using namespace std;

//#undef KVARK_DEBUG

#ifdef KVARK_DEBUG
	#include "../h/debug.h"
#else
#define dbg(...) (void(1));
#define dbg2(...) (void(1));
#define dbg3(...) (void(1));
#define dbg4(...) (void(1));
#define dbg5(...) (void(1));
#define dbgp(...) (void(1));
#define dbg_arr(...) (void(1));
#define dbg_vec(...) (void(1));
#endif

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<pii> > vvpii;
typedef vector<vector<long long> > vvl;
typedef vector<long long> vl;
typedef long long int llint;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))
#define endl "\n"

void task();

int main(){
#ifdef KVARK
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	srand(time(0));
#endif
//	ios_base::sync_with_stdio(0);

	task();

#ifdef KVARK_DEBUG
	my_debug::printProcessInfo();
#endif
	return 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
const int M = 3e5+10;

#define double long double

struct Rib {
	int from;
	int to;
	double f;
	int c;
};

int n, m, x;
vi g[51];
int pt[51];
Rib e[510*2];
int esz = 0;
double W;
int S = 0;
int T = 0;
int d[51];

void pushRib(int a, int b, int c) {
	g[a].push_back(esz);
	e[esz++] = Rib{a, b, 0, c};
	g[b].push_back(esz);
	e[esz++] = Rib{b, a, 0, 0};
}

bool bfs() {
	memset(d, 0x3f, sizeof(d));
	d[S] = 0;
	queue<int> q;
	q.push(S);
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (int i: g[v]) {
			Rib& r = e[i];
			if (d[r.to] == INF && r.c - r.f >= W + 1e-9) {
				d[r.to] = d[v] + 1;
				q.push(r.to);
			}
		}
	}
	return d[T] != INF;
}

double dfs(int v, double f) {
	if (v == T)
		return f;
	if (f < W - 1e-9)
		return 0;
	double r = 0;
	for (;pt[v] < g[v].size(); ++pt[v]) {
		Rib& rib = e[g[v][pt[v]]];
		if (d[v] + 1 == d[rib.to]) {
			double yet = llint((rib.c - rib.f) / W + 1e-9) * W;
			double add = dfs(rib.to, min(f, yet));
			r += add;
			f -= add;
			rib.f += add;
			e[g[v][pt[v]]^1].f -= add;
			if (f < W)
				return r;
		}
	}
	return r;
}

bool solve() {
	double flow = 0;
	for (int i = 0; i < esz; ++i)
		e[i].f = 0;
	while (bfs()) {
		memset(pt, 0, sizeof(pt));
		double s = dfs(S, INF);
		flow += s;
	}
	return flow / W >= x;
}

void task(){
	cin >> n >> m >> x;
	T = n - 1;
	for (int i = 0; i < m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		--a;
		--b;
		pushRib(a, b, c);
	}
	double l = 0;
	double r = 1e6+1;
	for (int step = 0; step < 64; ++step) {
		W = (l + r) / 2;
		if (solve())
			l = W;
		else
			r = W;
	}
	cout << fixed << setprecision(10) << l * x;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
using namespace std;

//#undef KVARK_DEBUG

#ifdef KVARK_DEBUG
	#include "../h/debug.h"
#else
#define dbg(...) (void(1));
#define dbg2(...) (void(1));
#define dbg3(...) (void(1));
#define dbg4(...) (void(1));
#define dbg5(...) (void(1));
#define dbgp(...) (void(1));
#define dbg_arr(...) (void(1));
#define dbg_vec(...) (void(1));
#endif

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<pii> > vvpii;
typedef vector<vector<long long> > vvl;
typedef vector<long long> vl;
typedef long long int llint;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))
#define endl "\n"

void task();

int main(){
#ifdef KVARK
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	srand(time(0));
#endif
//	ios_base::sync_with_stdio(0);

	task();

#ifdef KVARK_DEBUG
	my_debug::printProcessInfo();
#endif
	return 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
const int M = 3e5+10;

#define double long double

struct Rib {
	int from;
	int to;
	double f;
	int c;
};

int n, m, x;
vi g[51];
int pt[51];
Rib e[510*2];
int esz = 0;
double W;
int S = 0;
int T = 0;
int d[51];

void pushRib(int a, int b, int c) {
	g[a].push_back(esz);
	e[esz++] = Rib{a, b, 0, c};
	g[b].push_back(esz);
	e[esz++] = Rib{b, a, 0, 0};
}

bool bfs() {
	memset(d, 0x3f, sizeof(d));
	d[S] = 0;
	queue<int> q;
	q.push(S);
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (int i: g[v]) {
			Rib& r = e[i];
			if (d[r.to] == INF && r.c - r.f >= W) {
				d[r.to] = d[v] + 1;
				q.push(r.to);
			}
		}
	}
	return d[T] != INF;
}

double dfs(int v, double f) {
	if (v == T)
		return f;
	if (f < W)
		return 0;
	double r = 0;
	for (;pt[v] < g[v].size(); ++pt[v]) {
		Rib& rib = e[g[v][pt[v]]];
		{
			double yet = llint((rib.c - rib.f) / W) * W;
			double add = dfs(rib.to, min(f, yet));
			r += add;
			f -= add;
			rib.f += add;
			e[g[v][pt[v]]^1].f -= add;
			if (f < W)
				return r;
		}
	}
	return r;
}

bool solve() {
	double flow = 0;
	for (int i = 0; i < esz; ++i)
		e[i].f = 0;
	while (1) {
		memset(pt, 0, sizeof(pt));
		double s = dfs(S, INF);
		flow += s;
		if (s < 1e-9)
			break;
	}
	return flow / W >= x;
}

void task(){
	cin >> n >> m >> x;
	T = n - 1;
	for (int i = 0; i < m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		--a;
		--b;
		pushRib(a, b, c);
	}
	double l = 0;
	double r = 1e6+1;
	for (int step = 0; step < 64; ++step) {
		W = (l + r) / 2;
		if (solve())
			l = W;
		else
			r = W;
	}
	cout << fixed << setprecision(10) << l * x;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
using namespace std;

//#undef KVARK_DEBUG

#ifdef KVARK_DEBUG
	#include "../h/debug.h"
#else
#define dbg(...) (void(1));
#define dbg2(...) (void(1));
#define dbg3(...) (void(1));
#define dbg4(...) (void(1));
#define dbg5(...) (void(1));
#define dbgp(...) (void(1));
#define dbg_arr(...) (void(1));
#define dbg_vec(...) (void(1));
#endif

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<pii> > vvpii;
typedef vector<vector<long long> > vvl;
typedef vector<long long> vl;
typedef long long int llint;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))
#define endl "\n"

void task();

int main(){
#ifdef KVARK
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	srand(time(0));
#endif
//	ios_base::sync_with_stdio(0);

	task();

#ifdef KVARK_DEBUG
	my_debug::printProcessInfo();
#endif
	return 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
const int M = 3e5+10;

struct Rib {
	int from;
	int to;
	double f;
	int c;
};

int n, m, x;
vi g[51];
int pt[51];
Rib e[510*2];
int esz = 0;
double W;
int S = 0;
int T = 0;
int d[51];

void pushRib(int a, int b, int c) {
	g[a].push_back(esz);
	e[esz++] = Rib{a, b, 0, c};
	g[b].push_back(esz);
	e[esz++] = Rib{b, a, 0, 0};
}

bool bfs() {
	memset(d, 0x3f, sizeof(d));
	d[S] = 0;
	queue<int> q;
	q.push(S);
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (int i: g[v]) {
			Rib& r = e[i];
			if (d[r.to] == INF && r.c - r.f >= W) {
				d[r.to] = d[v] + 1;
				q.push(r.to);
			}
		}
	}
	return d[T] != INF;
}

double dfs(int v, double f) {
	if (v == T)
		return f;
	if (f < W)
		return 0;
	double r = 0;
	for (;pt[v] < g[v].size(); ++pt[v]) {
		Rib& rib = e[g[v][pt[v]]];
		if (d[v] + 1 == d[rib.to]) {
			double yet = int((rib.c - rib.f) / W) * W;
			double add = dfs(rib.to, min(f, yet));
			r += add;
			f -= add;
			rib.f += add;
			e[g[v][pt[v]]^1].f -= add;
			if (f < W)
				return r;
		}
	}
	return r;
}

bool solve() {
	double flow = 0;
	for (int i = 0; i < esz; ++i)
		e[i].f = 0;
	while (bfs()) {
		memset(pt, 0, sizeof(pt));
		flow += dfs(S, INF);
	}
	return flow / W >= x;
}

void task(){
	cin >> n >> m >> x;
	T = n - 1;
	for (int i = 0; i < m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		--a;
		--b;
		pushRib(a, b, c);
	}
	double l = 0;
	double r = 1e6+1;
	while (r*x - l*x > 1e-7) {
		W = (l + r) / 2;
		if (solve())
			l = W;
		else
			r = W;
	}
	cout << fixed << setprecision(10) << l * x;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
using namespace std;

//#undef KVARK_DEBUG

#ifdef KVARK_DEBUG
	#include "../h/debug.h"
#else
#define dbg(...) (void(1));
#define dbg2(...) (void(1));
#define dbg3(...) (void(1));
#define dbg4(...) (void(1));
#define dbg5(...) (void(1));
#define dbgp(...) (void(1));
#define dbg_arr(...) (void(1));
#define dbg_vec(...) (void(1));
#endif

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<pii> > vvpii;
typedef vector<vector<long long> > vvl;
typedef vector<long long> vl;
typedef long long int llint;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))
#define endl "\n"

void task();

int main(){
#ifdef KVARK
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	srand(time(0));
#endif
//	ios_base::sync_with_stdio(0);

	task();

#ifdef KVARK_DEBUG
	my_debug::printProcessInfo();
#endif
	return 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
const int M = 3e5+10;

#define double long double

struct Rib {
	int from;
	int to;
	double f;
	int c;
};

int n, m, x;
vi g[51];
int pt[51];
Rib e[510*2];
int esz = 0;
double W;
int S = 0;
int T = 0;
int d[51];

void pushRib(int a, int b, int c) {
	g[a].push_back(esz);
	e[esz++] = Rib{a, b, 0, c};
	g[b].push_back(esz);
	e[esz++] = Rib{b, a, 0, 0};
}

bool bfs() {
	memset(d, 0x3f, sizeof(d));
	d[S] = 0;
	queue<int> q;
	q.push(S);
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (int i: g[v]) {
			Rib& r = e[i];
			if (d[r.to] == INF && r.c - r.f >= W) {
				d[r.to] = d[v] + 1;
				q.push(r.to);
			}
		}
	}
	return d[T] != INF;
}

double dfs(int v, double f) {
	if (v == T)
		return f;
	if (f < W)
		return 0;
	double r = 0;
	for (;pt[v] < g[v].size(); ++pt[v]) {
		Rib& rib = e[g[v][pt[v]]];
		if (d[v] + 1 == d[rib.to]) {
			double yet = int((rib.c - rib.f) / W) * W;
			double add = dfs(rib.to, min(f, yet));
			r += add;
			f -= add;
			rib.f += add;
			e[g[v][pt[v]]^1].f -= add;
			if (f < W)
				return r;
		}
	}
	return r;
}

bool solve() {
	double flow = 0;
	for (int i = 0; i < esz; ++i)
		e[i].f = 0;
	while (bfs()) {
		memset(pt, 0, sizeof(pt));
		flow += dfs(S, INF);
	}
	return flow / W >= x;
}

void task(){
	cin >> n >> m >> x;
	T = n - 1;
	for (int i = 0; i < m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		--a;
		--b;
		pushRib(a, b, c);
	}
	double l = 0;
	double r = 1e6+1;
	for (int step = 0; step < 100; ++step) {
		W = (l + r) / 2;
		if (solve())
			l = W;
		else
			r = W;
	}
	cout << fixed << setprecision(10) << l * x;
}


#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
#define mset(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
#define N 70
#define inf (1<<30)
#define EPS 1e-12
vi graph[N], extra;
int bears, n, P[N], dist[N], cost[N][N], cpy[N][N];
bool bfs(){
    for(auto &it:dist) it = inf;
    queue<int> fila;
    dist[1] = 0;
    P[1] = -1;
    fila.push(1);
    while(!fila.empty()){
        int cur = fila.front();
        fila.pop();
        for(auto it:graph[cur]){
            if(!cost[cur][it]) continue;
            if(dist[it] > dist[cur] + 1){
                dist[it] = dist[cur] + 1;
                P[it] = cur;
                fila.push(it);
            }
        }
    }
    return dist[n] != inf;
}
int augment(int cur, int mi){
    if(P[cur] == -1) return mi;
    int f = augment(P[cur], min(mi, cost[P[cur]][cur]));
    cost[P[cur]][cur] -= f;
    cost[cur][P[cur]] += f;
    return f;
}
int flow(double lim){
    for(int e = 0; e < N; e++) for(int f = 0; f < N; f++) cpy[e][f] = cost[e][f];
    int ans = 0;
    while(bfs())
        ans += int(augment(n, inf)/lim);
    for(int e = 0; e < N; e++) for(int f = 0; f < N; f++) cost[e][f] = cpy[e][f];
    return ans;
}
int main(){
    int m, x, y;
    ll c;
    scanf("%d %d %d", &n, &m, &bears);
    while(m--){
        scanf("%d %d %lld", &x, &y, &c);
        cost[x][y] = c;
        graph[x].pb(y);
    }
    double lo = 0, hi = 1e14;
    while(hi - lo > EPS){
        double mid = (lo+hi)*0.5;
        if(flow(mid) >= bears) lo = mid;
        else hi = mid;
    }
    printf("%.10lf\n", lo*bears);
    return 0;
}

// 1481914193510


#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
#define mset(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
#define N 55
#define inf (1<<30)
#define EPS 1e-11
vi graph[N], extra;
int bears, n, P[N], dist[N], cost[N][N], cpy[N][N];
bool bfs(){
    for(auto &it:dist) it = inf;
    queue<int> fila;
    dist[1] = 0;
    P[1] = -1;
    fila.push(1);
    while(!fila.empty()){
        int cur = fila.front();
        fila.pop();
        for(auto it:graph[cur]){
            if(!cost[cur][it]) continue;
            if(dist[it] > dist[cur] + 1){
                dist[it] = dist[cur] + 1;
                P[it] = cur;
                fila.push(it);
            }
        }
    }
    return dist[n] != inf;
}
int augment(int cur, int mi){
    if(P[cur] == -1) return mi;
    int f = augment(P[cur], min(mi, cost[P[cur]][cur]));
    cost[P[cur]][cur] -= f;
    cost[cur][P[cur]] += f;
    return f;
}
int flow(double lim){
    for(int e = 0; e < N; e++) for(int f = 0; f < N; f++) cpy[e][f] = cost[e][f], cost[e][f] /= lim;
    int ans = 0;
    while(bfs())
        ans += augment(n, inf);
    for(int e = 0; e < N; e++) for(int f = 0; f < N; f++) cost[e][f] = cpy[e][f];
    return ans;
}
int main(){
    int m, x, y;
    ll c;
    scanf("%d %d %d", &n, &m, &bears);
    while(m--){
        scanf("%d %d %lld", &x, &y, &c);
        cost[x][y] = c;
        graph[x].pb(y);
    }
    double lo = 0, hi = 1e6;
    while(hi - lo > EPS){
        //cout << "aqui" << endl;
        double mid = (lo+hi)*0.5;
        if(flow(mid) >= bears) lo = mid;
        else hi = mid;
    }
    printf("%.10lf\n", lo*bears);
    return 0;
}

// 1481928456104


nclude <cstring>
#include <iostream>
#include <bitset>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <utility>
#include <algorithm>
#include <iomanip>
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
#define imprime(v)                                        \
  for (int X = 0; X < v.size(); X++) cout << v[X] << " "; \
  cout << endl;
#define grid(v)                                                   \
  for (int X = 0; X < v.size(); X++) {                            \
    for (int Y = 0; Y < v[X].size(); Y++) cout << v[X][Y] << " "; \
    cout << endl;                                                 \
  }
#define endl "\n"
#define double long double
using namespace std;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;
typedef long long int ll;
struct Edge {
  int at,where;
  ll cap;
  void init(int _at, ll _cap, int _where) { at = _at, cap = _cap, where = _where; }
};
struct dad {
  int at, up, down;
  dad() {at=-1;}
  dad(int _at, int _up, int _down) { at = _at, up = _up, down = _down; }
};
class MaxFlow {
 private:
  vector<vector<Edge> > g;
  ll mf, f;
  int s, t;
  vector<dad> p;

 public:
  void augment(int v, ll minEdge)
  {
    if (v == s) {
      f = minEdge;
      return;
    }
    else if (p[v].at != -1) {
      augment(p[v].at, min(minEdge, g[p[v].at][p[v].up].cap));
      g[p[v].at][p[v].up].cap -= f;
      g[v][p[v].down].cap += f;
    }
  }
  void init(int N)
  {
    for (int i = 0; i < g.size(); i++) g[i].clear();
    mf = 0, f = 0;
    g.resize(N);
  }
  void addEdge(int u, int v, ll cap)
  {
    Edge A;
    A.init(v, cap, g[v].size());
    Edge B;
    B.init(u, 0, g[u].size());
    g[u].pb(A);
    g[v].pb(B);
  }
  int maxFlow(int source, int sink)
  {
    s = source;
    t = sink;
    mf = 0;
    while (true) {
      f = 0;
      vector<int> dist(g.size(), INF);
      dist[s] = 0;
      queue<int> q;
      q.push(s);
      p.clear();
      p.resize(g.size());
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        if (u == t) break;
        for (int i = 0; i < g[u].size(); i++) {
          Edge prox = g[u][i];
          if (dist[prox.at] == INF and prox.cap > 0) {
            dist[prox.at] = dist[u] + 1;
            q.push(prox.at);
            dad paizao(u, i, prox.where);
            p[prox.at] = paizao;
          }
        }
      }
      augment(t, INF);
      if (f == 0) break;
      mf += f;
    }
    return mf;
  }
};

ll n, m, x;
vector< pair<int, pair<ll,int> > > cu;
MaxFlow graph;
double bb(double i, double f)
{
  if (f - i < 1e-12) return i;
  MaxFlow graph;
  graph.init(n+1);
  double meio = (i + f) / (double)2;
  for (ll i = 0; i < cu.size(); i++) {
    ll u, v;
    ll cap;
    u = cu[i].first;
    v = cu[i].second.first;
    cap = cu[i].second.second;
    cap = cap / meio;
    graph.addEdge(u, v, (ll)cap);
  }
  double resp = graph.maxFlow(1, n);
  if (resp >= x) return bb(meio, f);
  return bb(i, meio);
}
int main()
{
  cin >> n >> m >> x;
  for (ll i = 0; i < m; i++) {
    ll u, v, cap;
    cin >> u >> v >> cap;
    pair<int, pair<int,ll> > xablau;
    xablau.first = u;
    xablau.second.first = v;
    xablau.second.second = cap;
    cu.pb(xablau);
  }
  double r = bb(0, 100000000);
  r *= (double)x;
  cout << setprecision(10) << fixed << r << endl;
  return 0;
}

// 1469554227829


#include <bits/stdc++.h>
#define f(a,b,c) for(int a=b;a<c;a++)
#define scnaf scanf
#define frg(a,b) for(int a=adj[b];~a;a=ant[a])
using namespace std;
int to[5100000],cap[5100000],ant[5100000],adj[60],z;
int n,m,brs;
void insert(int a,int b,int c=1){
	//cout<<"\t"<<a<<","<<b<<endl;
	ant[z]=adj[a];
	cap[z]=c;
	to[z]=b;
	adj[a]=z++;
	ant[z]=adj[b];
	cap[z]=0;
	to[z]=a;
	adj[b]=z++;
}
int level[60],fila[900000],copy_adj[60];
int bfs(int source,int sink){
	//cout<<"b";
	memset(level,-1,sizeof level);
	level[source]=0;
	int pos=0,tam=0;
	fila[tam++]=source;
	//cout<<"bfs\n";
	while(pos<tam){
		int now=fila[pos++];
		//cout<<"# - "<<now<<endl;
		frg(i,now){
			if(cap[i]&&level[to[i]]==-1){
				level[to[i]]=level[now]+1;
				fila[tam++]=to[i];
			}
		}
	}
	return ~level[sink];
}
int dfs(int no,int sink,int flow){
	//cout<<"d";
	if(no==sink)return flow;
	for(int &i=copy_adj[no];~i;i=ant[i]){
		if(cap[i]&&level[no]+1==level[to[i]]){
			int nflow=dfs(to[i],sink,min(flow,cap[i]));
			if(nflow){
				cap[i]-=nflow;cap[i^1]+=nflow;
				return nflow;
			}
		}
	}
	return 0;
}
int dinic(int source,int sink){
	int mf=0,add;//cout<<"dinic\n";
	while(bfs(source,sink)){
		memcpy(copy_adj,adj,sizeof adj);
		while(add=dfs(source,sink,10000)){
			mf+=add;
		}
	}
	//cout<<"oi";
	return mf;
}
double lido[600];int lidoa[600][2];
bool can(double x){
	z=0;memset(adj,-1,sizeof adj);
	f(i,0,m){
		insert(lidoa[i][0],lidoa[i][1],int(lido[i]/x));
	}
	if(dinic(1,n)>=brs)return true;
	return false;
}
double bs(){
	double l=0,r=100000000000.0,iter=500;
	while(iter--){
		double m=(l+r)/2.0;
		if(can(m))l=m;
		else r=m;
	}
	return r*double(brs);
}
int main(){
	int sz=z;int aa,bb;double cc;int needed=1000000,cse=1;
	scanf("%d%d%d",&n,&m,&brs);
	f(i,0,m){
		scanf("%d%d%lf",&aa,&bb,&cc);
		lido[i]=cc;
		lidoa[i][0]=aa;
		lidoa[i][1]=bb;
	}
	printf("%.8lf\n",bs());
}
// 1473709773533


#include<bits/stdc++.h>

using namespace std;

void arquivo(){
    freopen("in","r",stdin);
}

const long long oo = 0x3f3f3f3f;
const double EPS = 1e-9;
long long n,m,x,u,v,w,res[55][55],mf,f;
long long s;
vector<long long> p;
map<long long,long long> mapa;

void augment(long long v,long long minEdge){
    if(v == 1){ f = minEdge; return ;}
    else if(p[v] != -1){
        augment(p[v],min(minEdge,res[p[v]][v]));
        res[p[v]][v] -= f;
        res[v][p[v]] += f;
    }
}

int  main(){
    scanf("%lld%lld%lld",&n,&m,&x);
    for(long long i=0;i<m;i++){
        scanf("%lld%lld%lld",&u,&v,&w);
        res[u][v] = w;
    }
    mf = 0;
    while(1){
        f = 0;
        vector<long long> dist(55,oo);
        dist[1] = 0;
        queue<long long> q;
        q.push(1);
        p.assign(55,-1);
        while(!q.empty()){
            long long u = q.front(); q.pop();
            if(u == n) break;
            for(long long v=1;v<=n;v++){
                if(res[u][v] > 0 && dist[v] == oo){
                    dist[v] = dist[u] + 1, q.push(v) , p[v] = u;
                }
            }
        }
        augment(n,oo);
        if(f == 0){

            break;
        }
        mapa[f]++;
        mf += f;
    }
    double l = 0, r = (double)mf/(double)x, mid ;
    for(long long i=0;i<1000;i++){
        mid = (l+r)/2;

        if(mid <= EPS){
            mid = 0;
            break;
        }
        s = 0;
        for(map<long long,long long>::iterator it = mapa.begin();it!=mapa.end();it++){
            s += ((long long) ((double)it->first / (double)mid) ) * it->second;
        }
        assert(s >= 0);
        if(s < x){
            r = mid;
        }
        else if (s >= x){
            l = mid;
        }

    }
    printf("%.8lf\n",x*mid);
    return 0;
}

// 1469559658325


#include <bits/stdc++.h>

using namespace std;

const double eps = 1e-7;

const int inf = 0x3f3f3f3f;
const int N = 55000, E = 4000000;

int cmp(double a, double b) {
  if (fabs(a - b) < eps) return 0;
  return a < b ? -1 : 1;
}

int to[E], cap[E], ant[E], adj[N], inde;
int level[N], fila[N], copy_adj[N];

inline void add_edge(int a, int b, int c) {
  to[inde] = b; cap[inde] = c; ant[inde] = adj[a]; adj[a] = inde++;
  to[inde] = a; cap[inde] = 0; ant[inde] = adj[b]; adj[b] = inde++;
}

int bfs(int source, int sink) {
  memset(level, -1, sizeof level);
  level[source] = 0;
  int front = 0, size = 0, v;
  fila[size++] = source;
  while (front < size) {
    v = fila[front++];
    for (int i = adj[v]; ~i; i = ant[i]) {
      if (cap[i] && level[to[i]] == -1) {
        level[to[i]] = level[v] + 1;
        fila[size++] = to[i];
      }
    }
  }
  return level[sink] != -1;
}

int dfs(int v, int sink, int flow) {
  if (v == sink) return flow;
  int f;
  for (int &i = copy_adj[v]; ~i; i = ant[i]) {
    if (cap[i] && level[to[i]] == level[v] + 1 && (f = dfs(to[i], sink, min(flow, cap[i])))) {
      cap[i] -= f; cap[i ^ 1] += f;
      return f;
    }
  }
  return 0;
}

int maxflow(int source, int sink) {
  int ret = 0, flow;
  while (bfs(source, sink)) {
    memcpy(copy_adj, adj, sizeof adj);
    while (flow = dfs(source, sink, 1 << 30)) ret += flow;
  }
  return ret;
}

tuple<int, int, int> edges[505];

bool is_int(double x) {
  int a = int(floor(x));
  int b = int(ceil(x));
  return a == b;
}

int main() {
  int n, m, x; scanf("%d %d %d", &n, &m, &x);

  int max_edge = 0;
  for (int i = 0; i < m; ++i) {
    int a, b, c; scanf("%d %d %d", &a, &b, &c);
    edges[i] = make_tuple(a, b, c);
    max_edge = max(max_edge, c);
  }

  double lo = 0.0, hi = max_edge, ans = -1.0;
  for (int _ = 0; _ < 1000; ++_) {
    memset(adj, -1, sizeof adj); inde = 0;

    double mid = (lo + hi) / 2;
    for (int i = 0; i < m; ++i) {
      int a, b, c;
      tie(a, b, c) = edges[i];

      if (cmp(c, mid) > 0 || (is_int(mid) && cmp(c, mid) == 0)) {
        add_edge(a, b, int(c / mid));
      }
    }

    add_edge(0, 1, x);

    if (maxflow(0, n) == x) {
      lo = mid;
      ans = mid;
    } else {
      hi = mid;
    }
  }

  printf("%.10lf\n", ans * x);
  return 0;
}
// 1472164491423


#include <cstring>
#include <iostream>
#include <bitset>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <utility>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(s) scanf("%s", s)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define imprime(v)                                        \
  for (int X = 0; X < v.size(); X++) cout << v[X] << " "; \
  cout << endl;
#define grid(v)                                                   \
  for (int X = 0; X < v.size(); X++) {                            \
    for (int Y = 0; Y < v[X].size(); Y++) cout << v[X][Y] << " "; \
    cout << endl;                                                 \
  }
#define endl "\n"
const int INF = 0x3f3f3f3f;
const long double pi = acos(-1);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vector<int> > vvi;
typedef vector<vector<ii> > vvii;
const int MOD = 1e9 + 7;
const int N = 300000;
int bit[N];
void add(int idx, int val)
{
  while (idx < N) {
    bit[idx] += val;
    idx += (idx & -idx);
  }
}
int sum(int idx)
{
  int r = 0;
  while (idx > 0) {
    r += bit[idx];
    idx -= (idx & -idx);
  }
  return r;
}
void solve()
{
  int n, m;
  sc2(n, m);
  int mapa[n + 1];
  for (int i = 1; i <= n; i++) mapa[i] = n - i + 1;
  for (int i = 1; i <= n; i++) add(mapa[i], 1);
  add(n+1,-5);
  int tam=n+1;
  for(int i=0;i<m;i++){
    int x;
    sc(x);
    int resp=sum(mapa[x])-sum(tam);
    prie(resp);
    add(mapa[x],1);
    add(tam,-1);
    tam++;
  }
  cout<<endl;
}
int main()
{
  int t;
  sc(t);
  while (t--) {
    solve();
  }
  return 0;
}

// 1470096064075


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned long long uInt;
typedef unsigned uint;

const int MAXN = 510;
const int INF = 1001;

int N, M, K;
vector<int> paths;
int A[MAXN], B[MAXN], C[MAXN];

struct edge {
    int to,rev;
    double cap;
    edge(int to, Int cap, int rev): to(to), cap(cap), rev(rev) {}
};

vector<edge> G[MAXN];
Int level[MAXN];
int iter[MAXN];

void init(int N) {
    for (int i = 0; i < N; i++) {
        G[i].clear();
    }
}

void add_edge(int from,int to,Int cap) {
    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size()-1));
}

void bfs(int s) {
    memset(level, -1, sizeof(level));
    queue<int> que;
    level[s] = 0;
    que.push(s);

    while(!que.empty()) {
        int v = que.front();
        que.pop();
        for (int i = 0; i < (int) G[v].size(); i++) {
            edge& e = G[v][i];
            if(e.cap > 0 && level[e.to] < 0) {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

Int dfs(int v, int t, double f) {
    if(v == t) return f;
    for(int& i = iter[v]; i < (int) G[v].size(); i++) {
        edge &e = G[v][i];
        if(e.cap > 0 && level[v] < level[e.to]) {
            Int d = dfs(e.to, t, min(f, e.cap));
            if (d > 0) {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

double max_flow(int s, int t) {
    double flow = 0;
    for( ; ; ) {
        bfs(s);
        if (level[t] < 0) {
            return flow;
        }
        memset(iter, 0, sizeof(iter));
        int f;
        while ((f=dfs(s,t,INF*INF)) > 0) {
            flow += f;
        }
    }
}

double func(int pos, double carry, int used) {
    if (pos == (int) paths.size()) {
        return carry;
    } else if (used == 0) {
        return -INF;
    } else {
        double ans = -1000;

        for (int i = used; i >= 1; i--) {
            if (used - i >= (int) paths.size() - pos + 1) {
                ans = max(ans, func(pos + 1, min(carry, paths[pos] / (double) i), used - i));
            }
        }

        return ans;
    }
}

int main(void) {
    cin >> N >> M >> K;

    init(N + 1);

    for (int i = 0; i < M; i++) {
        cin >> A[i] >> B[i] >> C[i];
    }

    double l = 0.0, h = 10000000000000.0;

    for (int x = 0; x < 100; x++) {
        double m = (l + h) / 2;

        init(N + 1);

        for (int i = 0; i < M; i++) {
            add_edge(A[i], B[i], C[i] / m);
        }

        if (max_flow(1, N) < K) {
            h = m;
        } else {
            l = m;
        }
    }

    cout << fixed << setprecision(12) << l * K << endl;

    return 0;
}

// 1469557672465


#include<bits/stdc++.h>
#define sc scanf
#define pt printf
#define fi first
#define pb push_back
#define se second
#define fr(i, a, b) for(int i = a; i < b; ++i)
#define cl(x, n) memset(x, n, sizeof x)
#define rp(i, u) for(int i = adj[u]; i != -1; i = ant[i])
#define crp(i, u) for(int &i = cadj[u]; i != -1; i = ant[i])

using namespace std;

typedef pair<int, int> ii;

#define MAXN 100
#define MAXM 10000
#define src 0
#define snk (n-1)

int to[MAXM], ant[MAXM], adj[MAXN], cadj[MAXN], lvl[MAXN], z, n, m, p;
double cap[MAXM], ccap[MAXM];

int add(int u, int v, double c){
	to[z] = v, cap[z] = c, ant[z] = adj[u]; adj[u] = z++;
	to[z] = u, cap[z] = 0, ant[z] = adj[v]; adj[v] = z++;
}

int bfs(double mf){
	cl(lvl, -1);
	lvl[src] = 0;
	queue<int> q;
	q.push(src);
	while(!q.empty()){
		int u = q.front(); q.pop();
		rp(i, u) if(cap[i] >= mf && lvl[to[i]] == -1) {
			lvl[to[i]] = lvl[u] + 1;
			q.push(to[i]);
		}
	}
	return lvl[snk] != -1;
}

double dfs(int u, double df){
	if(u == snk) return df;
	crp(i, u) if(cap[i] >= df && (lvl[to[i]] == lvl[u] + 1) && (dfs(to[i], df) >= df)){
		cap[i] -= df, cap[i^1] += df;
		return df;
	}
	return 0;
}

double mf(double df){
	double ans = 0, f;
	int pp = p;
	while(bfs(df) && pp){
		memcpy(cadj, adj, sizeof adj);
		while(f = dfs(src, df) > 10e-9 && pp--) { ans += df;}
	}
	if(pp > 0) return -1;
	return ans;
}

int main(){
	cl(adj, -1), z = 0;
	sc("%d %d %d", &n, &m, &p);
	fr(i, 0, m){
		int a, b, x;
		sc("%d %d %d", &a, &b, &x);
		a--; b--;
		add(a,b,x);
	}
	memcpy(ccap, cap, sizeof cap);
	double l = 0, r = 1000000;
	while(r-l > 10e-12){
		memcpy(cap, ccap, sizeof ccap);
		double  m = (l+r)/2;
		if(mf(m) != -1) l = m;
		else r = m;
	}
	memcpy(cap, ccap, sizeof ccap);
	pt("%.10lf\n", mf(l));

	return 0;
}
// 1472772581270


#include <bits/stdc++.h>

using namespace std;

const double eps = 1e-7;

const int inf = 0x3f3f3f3f;
const int N = 55000, E = 4000000;

int cmp(double a, double b) {
  if (fabs(a - b) < eps) return 0;
  return a < b ? -1 : 1;
}

int to[E], cap[E], ant[E], adj[N], inde;
int level[N], fila[N], copy_adj[N];

inline void add_edge(int a, int b, int c) {
  to[inde] = b; cap[inde] = c; ant[inde] = adj[a]; adj[a] = inde++;
  to[inde] = a; cap[inde] = 0; ant[inde] = adj[b]; adj[b] = inde++;
}

int bfs(int source, int sink) {
  memset(level, -1, sizeof level);
  level[source] = 0;
  int front = 0, size = 0, v;
  fila[size++] = source;
  while (front < size) {
    v = fila[front++];
    for (int i = adj[v]; ~i; i = ant[i]) {
      if (cap[i] && level[to[i]] == -1) {
        level[to[i]] = level[v] + 1;
        fila[size++] = to[i];
      }
    }
  }
  return level[sink] != -1;
}

int dfs(int v, int sink, int flow) {
  if (v == sink) return flow;
  int f;
  for (int &i = copy_adj[v]; ~i; i = ant[i]) {
    if (cap[i] && level[to[i]] == level[v] + 1 && (f = dfs(to[i], sink, min(flow, cap[i])))) {
      cap[i] -= f; cap[i ^ 1] += f;
      return f;
    }
  }
  return 0;
}

int maxflow(int source, int sink) {
  int ret = 0, flow;
  while (bfs(source, sink)) {
    memcpy(copy_adj, adj, sizeof adj);
    while (flow = dfs(source, sink, 1 << 30)) ret += flow;
  }
  return ret;
}

tuple<int, int, int> edges[505];

int main() {
  // int a = 1, b = 100000;
  // printf("%lf\n", a / (1.0 / b));

  int n, m, x; scanf("%d %d %d", &n, &m, &x);

  int max_edge = 0;
  for (int i = 0; i < m; ++i) {
    int a, b, c; scanf("%d %d %d", &a, &b, &c);
    edges[i] = make_tuple(a, b, c);
    max_edge = max(max_edge, c);
  }

  double lo = 0.0, hi = max_edge, ans = -1;
  for (int _ = 0; _ < 200; ++_) {
    memset(adj, -1, sizeof adj); inde = 0;

    double mid = (lo + hi) / 2;
    for (int i = 0; i < m; ++i) {
      int a, b, c;
      tie(a, b, c) = edges[i];

      c = (long long) min((double) x, round(floor(c / mid)));

      add_edge(a, b, c);
    }

    if (maxflow(1, n) >= x) {
      lo = mid;
      ans = mid;
    } else {
      hi = mid;
    }
  }

  printf("%.10lf\n", ans * x);
  return 0;
}
// 1472173667517


#include<bits/stdc++.h>

using namespace std;

void arquivo(){
    freopen("in","r",stdin);
}

const int oo = 0x3f3f3f3f;
const double EPS = 1e-9;
int n,m,x,u,v,w,res[55][55],mf,f;
vector<int> p;
map<int,int> mapa;

void augment(int v,int minEdge){
    if(v == 1){ f = minEdge; return ;}
    else if(p[v] != -1){
        augment(p[v],min(minEdge,res[p[v]][v]));
        res[p[v]][v] -= f;
        res[v][p[v]] += f;
    }
}

int main(){
    scanf("%d%d%d",&n,&m,&x);
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&u,&v,&w);
        res[u][v] = w;
    }
    mf = 0;
    while(1){
        f = 0;
        vector<int> dist(55,oo);
        dist[1] = 0;
        queue<int> q;
        q.push(1);
        p.assign(55,-1);
        while(!q.empty()){
            int u = q.front(); q.pop();
            if(u == n) break;
            for(int v=1;v<=n;v++){
                if(res[u][v] > 0 && dist[v] == oo){
                    dist[v] = dist[u] + 1, q.push(v) , p[v] = u;
                }
            }
        }
        augment(n,oo);
        if(f == 0){

            break;
        }
        mapa[f]++;
        mf += f;
    }
    double l = 0, r = (double)mf/(double)x, mid ;
    int s = 0;
    for(int i=0;i<100;i++){
        mid = (l+r)/2;

        if(mid <= EPS){
            mid = 0;
            break;
        }
        s = 0;
        for(map<int,int>::iterator it = mapa.begin();it!=mapa.end();it++){
            s += ((int) ((double)it->first / (double)mid) ) * it->second;
        }
        if(s < x){
            r = mid;
        }
        else if (s >= x){
            l = mid;
        }

    }
    printf("%.8lf\n",x*mid);
    return 0;
}

// 1469559107369


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<iomanip>
using namespace std;

typedef long long ll;
int c[55][55];
int d[55][55];
int f[55][55];
int back[55];
vector<int> g[55];
int n, m, x;
bool bfs(){
	queue<int> q;
	for (int i = 1; i <= n; i++) back[i] = 0;
	back[1] = 1;
	q.push(1);
	while (!q.empty()){
		int here = q.front(); q.pop();
		for (int v : g[here]){
			if (!back[v] && d[here][v] - f[here][v] > 0){
				q.push(v);
				back[v] = here;
			}
		}
	}
	return back[n] > 0;
}
int max_flow(){
	int ret = 0;
	while (bfs()){
		int flow = 1000000;
		int it = n;
		while (it != 1){
			int b = back[it];
			flow = min(flow, d[b][it] - f[b][it]);
			it = b;
		}
		it = n;
		while (it != 1){
			int b = back[it];
			f[b][it] += flow;
			f[it][b] -= flow;
			it = b;
		}
		ret += flow;
		if (ret >= x) return ret;
	}
	return ret;
}
int find_cap(int cap, double flow){
	int lo = 0, hi = 1000000;
	while (lo<hi){
		int mid = (lo + hi + 1) / 2;
		if (mid * flow > cap){
			hi = mid - 1;
		}
		else lo = mid;
	}
	return lo;
}
bool chk(double flow){
	for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) f[i][j] = 0;
	for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) d[i][j] = find_cap(c[i][j], flow);

	return max_flow() >= x;
}

int main(){
	cin.sync_with_stdio(0), cout.sync_with_stdio(0);
	cin >> n >> m >> x;
	for (int i = 0; i < m; i++){
		int p, q, r;
		cin >> p >> q >> r;
		g[p].push_back(q), g[q].push_back(p);
		c[p][q] = r;
	}

	double lo = 0, hi = 1000000 * 50;
	for (int i = 0; i < 200; i++){
		double mid = (lo + hi) / 2;
		if (chk(mid)) lo = mid;
		else hi = mid;
	}
	cout << setprecision(15) << lo*x;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<iomanip>
using namespace std;

typedef long long ll;
int c[55][55];
int d[55][55];
int f[55][55];
int back[55];
vector<int> g[55];
int n, m, x;
bool bfs(){
	queue<int> q;
	for (int i = 1; i <= n; i++) back[i] = 0;
	back[1] = 1;
	q.push(1);
	while (!q.empty()){
		int here = q.front(); q.pop();
		for (int v : g[here]){
			if (!back[v] && d[here][v] - f[here][v] > 0){
				q.push(v);
				back[v] = here;
			}
		}
	}
	return back[n] > 0;
}
int max_flow(){
	int ret = 0;
	while (bfs()){
		int flow = 1000000;
		int it = n;
		while (it != 1){
			int b = back[it];
			flow = min(flow, d[b][it] - f[b][it]);
			it = b;
		}
		it = n;
		while (it != 1){
			int b = back[it];
			f[b][it] += flow;
			f[it][b] -= flow;
			it = b;
		}
		ret += flow;
		if (ret >= x) return ret;
	}
	return ret;
}
int find_cap(int cap, long double flow){
	int lo = 1, hi = 1000000;
	while (lo<hi){
		int mid = (lo + hi + 1) / 2;
		if (mid * flow > cap){
			hi = mid - 1;
		}
		else lo = mid;
	}
	return lo;
}
bool chk(long double flow){
	for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) f[i][j] = 0;
	for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) d[i][j] = find_cap(c[i][j], flow);

	return max_flow() >= x;
}

int main(){
	cin.sync_with_stdio(0), cout.sync_with_stdio(0);
	cin >> n >> m >> x;
	for (int i = 0; i < m; i++){
		int p, q, r;
		cin >> p >> q >> r;
		g[p].push_back(q), g[q].push_back(p);
		c[p][q] = r;
	}

	long double lo = 0, hi = 1000000 * 50;
	for (int i = 0; i < 200; i++){
		long double mid = (lo + hi) / 2;
		if (chk(mid)) lo = mid;
		else hi = mid;
	}
	cout << setprecision(15) << lo*x;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<iomanip>
using namespace std;

int c[55][55];
double f[55][55];
int back[55];
vector<int> g[55];
int n, m, x;
bool push(double flow){
	queue<int> q;
	for (int i = 1; i <= n; i++) back[i] = 0;
	back[1] = 1;
	q.push(1);
	while (!q.empty()){
		int here = q.front(); q.pop();
		for (int v : g[here]){
			if (!back[v] && c[here][v] - f[here][v] >= flow){
				q.push(v);
				back[v] = here;
			}
		}
	}
	if (!back[n]) return false;
	int it = n;
	while (it != 1){
		int b = back[it];
		f[b][it] += flow;
		f[it][b] -= flow;
		it = b;
	}
	return true;
}
bool chk(double flow){
	for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) f[i][j] = 0;
	for (int i = 1; i <= x; i++){
		if (!push(flow)) return false;
	}
	return true;
}

int main(){
	cin.sync_with_stdio(0), cout.sync_with_stdio(0);
	cin >> n >> m >> x;
	for (int i = 0; i < m; i++){
		int p, q, r;
		cin >> p >> q >> r;
		g[p].push_back(q), g[q].push_back(p);
		c[p][q] = r;
	}

	double lo = 0, hi = 1000000;
	for (int i = 0; i < 75; i++){
		double mid = (lo + hi) / 2;
		if (chk(mid)) lo = mid;
		else hi = mid;
	}
	cout << setprecision(10) << lo*x;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<iomanip>
using namespace std;

typedef long long ll;
int c[55][55];
int d[55][55];
int f[55][55];
int back[55];
vector<int> g[55];
int n, m, x;
struct MaxFlowDinic
{
	typedef long long flow_t;

	struct Edge
	{
		int next;
		int inv; /* inverse edge index */
		flow_t res; /* residual */
	};

	int n;
	vector<vector<Edge>> graph;

	vector<unsigned> q, l, start;

	void Init(int _n){
		n = _n;
		graph.resize(n);
		for (int i = 0; i < n; i++) graph[i].clear();
	}
	void AddNodes(int count) {
		n += count;
		graph.resize(n);
	}
	void AddEdge(int s, int e, flow_t cap, flow_t caprev = 0) {
		Edge forward = { e, graph[e].size(), cap };
		Edge reverse = { s, graph[s].size(), caprev };
		graph[s].push_back(forward);
		graph[e].push_back(reverse);
	}

	bool AssignLevel(int source, int sink) {
		int t = 0;
		memset(&l[0], 0, sizeof(l[0]) * l.size());
		l[source] = 1;
		q[t++] = source;
		for (int h = 0; h < t && !l[sink]; h++) {
			int cur = q[h];
			for (unsigned i = 0; i < graph[cur].size(); i++) {
				int next = graph[cur][i].next;
				if (l[next]) continue;
				if (graph[cur][i].res > 0) {
					l[next] = l[cur] + 1;
					q[t++] = next;
				}
			}
		}
		return l[sink] != 0;
	}

	flow_t BlockFlow(int cur, int sink, flow_t currentFlow) {
		if (cur == sink) return currentFlow;
		for (unsigned &i = start[cur]; i < graph[cur].size(); i++) {
			int next = graph[cur][i].next;
			if (graph[cur][i].res == 0 || l[next] != l[cur] + 1)
				continue;
			if (flow_t res = BlockFlow(next, sink, min(graph[cur][i].res, currentFlow))) {
				int inv = graph[cur][i].inv;
				graph[cur][i].res -= res;
				graph[next][inv].res += res;
				return res;
			}
		}
		return 0;
	}

	flow_t Solve(int source, int sink)
	{
		q.resize(n);
		l.resize(n);
		start.resize(n);
		flow_t ans = 0;
		while (AssignLevel(source, sink)) {
			memset(&start[0], 0, sizeof(start[0])*n);
			while (flow_t flow = BlockFlow(source, sink, numeric_limits<flow_t>::max())) {
				ans += flow;
			}
		}
		return ans;
	}
};
int find_cap(int cap, long double flow){
	int lo = 1, hi = 1000000;
	while (lo<hi){
		int mid = (lo + hi + 1) / 2;
		if (mid * flow > cap){
			hi = mid - 1;
		}
		else lo = mid;
	}
	return lo;
}
bool chk(long double flow){
	MaxFlowDinic mf; mf.Init(n);
	for (int i = 1; i <= n; i++)for (int j : g[i]) mf.AddEdge(i - 1, j - 1, find_cap(c[i][j], flow));

	return mf.Solve(0, n - 1) >= x;
}

int main(){
	cin.sync_with_stdio(0), cout.sync_with_stdio(0);
	cin >> n >> m >> x;
	for (int i = 0; i < m; i++){
		int p, q, r;
		cin >> p >> q >> r;
		g[p].push_back(q), g[q].push_back(p);
		c[p][q] = r;
	}

	long double lo = 0, hi = 1000000 * 50;
	for (int i = 0; i < 200; i++){
		long double mid = (lo + hi) / 2;
		if (chk(mid)) lo = mid;
		else hi = mid;
	}
	cout << setprecision(15) << lo*x;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<iomanip>
using namespace std;

typedef long long ll;
int c[55][55];
int d[55][55];
int f[55][55];
int back[55];
vector<int> g[55];
int n, m, x;
bool bfs(){
	queue<int> q;
	for (int i = 1; i <= n; i++) back[i] = 0;
	back[1] = 1;
	q.push(1);
	while (!q.empty()){
		int here = q.front(); q.pop();
		for (int v : g[here]){
			if (!back[v] && d[here][v] - f[here][v] > 0){
				q.push(v);
				back[v] = here;
			}
		}
	}
	return back[n] > 0;
}
int max_flow(){
	int ret = 0;
	while (bfs()){
		int flow = 1000000;
		int it = n;
		while (it != 1){
			int b = back[it];
			flow = min(flow, d[b][it] - f[b][it]);
			it = b;
		}
		it = n;
		while (it != 1){
			int b = back[it];
			f[b][it] += flow;
			f[it][b] -= flow;
			it = b;
		}
		ret += flow;
		if (ret >= x) return ret;
	}
	return ret;
}
int find_cap(int cap, double flow){
	int lo = 1, hi = 100000;
	while (lo<hi){
		int mid = (lo + hi + 1) / 2;
		if (mid * flow > cap){
			hi = mid - 1;
		}
		else lo = mid;
	}
	return lo;
}
bool chk(double flow){
	for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) f[i][j] = 0;
	for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) d[i][j] = find_cap(c[i][j], flow);

	return max_flow() >= x;
}

int main(){
	cin.sync_with_stdio(0), cout.sync_with_stdio(0);
	cin >> n >> m >> x;
	for (int i = 0; i < m; i++){
		int p, q, r;
		cin >> p >> q >> r;
		g[p].push_back(q), g[q].push_back(p);
		c[p][q] = r;
	}

	double lo = 0, hi = 1000000;
	for (int i = 0; i < 100; i++){
		double mid = (lo + hi) / 2;
		if (chk(mid)) lo = mid;
		else hi = mid;
	}
	cout << setprecision(10) << lo*x;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<iomanip>
using namespace std;

typedef long long ll;
int c[55][55];
int d[55][55];
int f[55][55];
int back[55];
vector<int> g[55];
int n, m, x;
struct MaxFlowDinic
{
	typedef long long flow_t;

	struct Edge
	{
		int next;
		int inv; /* inverse edge index */
		flow_t res; /* residual */
	};

	int n;
	vector<vector<Edge>> graph;

	vector<unsigned> q, l, start;

	void Init(int _n){
		n = _n;
		graph.resize(n);
		for (int i = 0; i < n; i++) graph[i].clear();
	}
	void AddNodes(int count) {
		n += count;
		graph.resize(n);
	}
	void AddEdge(int s, int e, flow_t cap, flow_t caprev = 0) {
		Edge forward = { e, graph[e].size(), cap };
		Edge reverse = { s, graph[s].size(), caprev };
		graph[s].push_back(forward);
		graph[e].push_back(reverse);
	}

	bool AssignLevel(int source, int sink) {
		int t = 0;
		memset(&l[0], 0, sizeof(l[0]) * l.size());
		l[source] = 1;
		q[t++] = source;
		for (int h = 0; h < t && !l[sink]; h++) {
			int cur = q[h];
			for (unsigned i = 0; i < graph[cur].size(); i++) {
				int next = graph[cur][i].next;
				if (l[next]) continue;
				if (graph[cur][i].res > 0) {
					l[next] = l[cur] + 1;
					q[t++] = next;
				}
			}
		}
		return l[sink] != 0;
	}

	flow_t BlockFlow(int cur, int sink, flow_t currentFlow) {
		if (cur == sink) return currentFlow;
		for (unsigned &i = start[cur]; i < graph[cur].size(); i++) {
			int next = graph[cur][i].next;
			if (graph[cur][i].res == 0 || l[next] != l[cur] + 1)
				continue;
			if (flow_t res = BlockFlow(next, sink, min(graph[cur][i].res, currentFlow))) {
				int inv = graph[cur][i].inv;
				graph[cur][i].res -= res;
				graph[next][inv].res += res;
				return res;
			}
		}
		return 0;
	}

	flow_t Solve(int source, int sink)
	{
		q.resize(n);
		l.resize(n);
		start.resize(n);
		flow_t ans = 0;
		while (AssignLevel(source, sink)) {
			memset(&start[0], 0, sizeof(start[0])*n);
			while (flow_t flow = BlockFlow(source, sink, numeric_limits<flow_t>::max())) {
				ans += flow;
			}
		}
		return ans;
	}
};
int find_cap(int cap, long double flow){
	int lo = 1, hi = 1000000;
	while (lo<hi){
		int mid = (lo + hi + 1) / 2;
		if (mid * flow > cap){
			hi = mid - 1;
		}
		else lo = mid;
	}
	return lo;
}
bool chk(long double flow){
	MaxFlowDinic mf; mf.Init(n);
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) mf.AddEdge(i, j, find_cap(c[i + 1][j + 1], flow));

	return mf.Solve(0, n - 1) >= x;
}

int main(){
	cin.sync_with_stdio(0), cout.sync_with_stdio(0);
	cin >> n >> m >> x;
	for (int i = 0; i < m; i++){
		int p, q, r;
		cin >> p >> q >> r;
		g[p].push_back(q), g[q].push_back(p);
		c[p][q] = r;
	}

	long double lo = 0, hi = 1000000 * 50;
	for (int i = 0; i < 200; i++){
		long double mid = (lo + hi) / 2;
		if (chk(mid)) lo = mid;
		else hi = mid;
	}
	cout << setprecision(15) << lo*x;
}


#include<bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793238462
typedef long long ll;
typedef double db;
int c[200005],d[200005],l[200005],r[200005];
vector<int> edge[200005];
void dfs(int u,int fa){
    int sum=c[u];
    for(auto v:edge[u]){
        if(v==fa) continue;
        dfs(v,u);sum+=c[v];
    }
    c[u]=min(d[u],sum);
}
int main(){
    int n,k,ans=0;cin>>n>>k;
    for(int i=1;i<=2*k;i++){
        int x;cin>>x;c[x]=1;
    }
    for(int i=1;i<n;i++){
        cin>>l[i]>>r[i];
        edge[l[i]].push_back(r[i]);
        edge[r[i]].push_back(l[i]);
        d[l[i]]++;d[r[i]]++;
    }
    dfs(1,0);
    for(int i=1;i<n;i++){
        ans+=min(c[l[i]],c[r[i]]);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793238462
typedef long long ll;
typedef double db;
int c[200005],n,k,ans;
vector<int> edge[200005];
void dfs(int u,int fa){
    for(auto v:edge[u]){
        if(v==fa) continue;
        dfs(v,u);c[u]+=c[v];
    }
}
void solve(int u,int fa){
    for(auto v:edge[u]){
        if(v==fa) continue;
        ans+=min(c[1]-c[v],c[v]);
        solve(v,u);
    }
}
int main(){
    cin>>n>>k;
    for(int i=1;i<=2*k;i++){
        int x;cin>>x;c[x]=1;
    }
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    dfs(1,0);solve(1,0);
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793238462
typedef long long ll;
typedef double db;
ll c[200005],n,k,ans;
vector<ll> edge[200005];
void dfs(ll u,ll fa){
    for(auto v:edge[u]){
        if(v==fa) continue;
        dfs(v,u);c[u]+=c[v];
    }
}
void solve(ll u,ll fa){
    for(auto v:edge[u]){
        if(v==fa) continue;
        ans+=min(c[1]-c[v],c[v]);
        solve(v,u);
    }
}
int main(){
    cin>>n>>k;
    for(ll i=1;i<=2*k;i++){
        ll x;cin>>x;c[x]=1;
    }
    for(ll i=1;i<n;i++){
        ll u,v;cin>>u>>v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    dfs(1,0);solve(1,0);
    cout<<ans<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
vector<int> G[200000 + 5];
int n, k, ai[200000 + 5], ans;
inline void ins(int x, int y) {
	G[x].push_back(y), G[y].push_back(x);
}
int dfs(int u, int pa) {
	int siz = ai[u];
	for (int i = 0; i < (int)G[u].size(); i++) {
		int v = G[u][i];
		if (v != pa) {
			int tmp = dfs(v, u);
			siz += tmp;
			ans += min(tmp, 2 * k - tmp);
		}
	}
	return siz;
}
void clean() {
	ans = 0, ms(ai, 0);
}
int solve() {
	clean();
	for (int u, i = 1; i <= 2 * k; i++) scanf("%d", &u), ai[u] = 1;
	for (int x, y, i = 1; i < n; i++) scanf("%d%d", &x, &y), ins(x, y);
	dfs(1, 0);
	printf("%d\n", ans);
	return 0;
}
int main() {
	scanf("%d%d", &n, &k), solve();
	return 0;
}
//
//¿ªÊ¼-´úÂëÍê³É-¾²Ì¬µ÷ÊÔÍê³É-¹ýÑùÀý


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
vector<LL> G[200000 + 5];
LL n, k, ai[200000 + 5], ans;
inline void ins(LL x, LL y) {
	G[x].push_back(y), G[y].push_back(x);
}
int dfs(LL u, LL pa) {
	LL siz = ai[u];
	for (LL i = 0; i < (LL)G[u].size(); i++) {
		LL v = G[u][i];
		if (v != pa) {
			LL tmp = dfs(v, u);
			siz += tmp;
			ans += min(tmp, 2ll * k - tmp);
		}
	}
	return siz;
}
void clean() {
	ans = 0, ms(ai, 0);
}
int solve() {
	clean();
	for (LL u, i = 1; i <= 2 * k; i++) scanf("%lld", &u), ai[u] = 1;
	for (LL x, y, i = 1; i < n; i++) scanf("%lld%lld", &x, &y), ins(x, y);
	dfs(1, 0);
	printf("%lld\n", ans);
	return 0;
}
int main() {
	scanf("%lld%lld", &n, &k), solve();
	return 0;
}
//
//¿ªÊ¼-´úÂëÍê³É-¾²Ì¬µ÷ÊÔÍê³É-¹ýÑùÀý


#include<bits/stdc++.h>
//#include<unordered_map>
#define ll          long long
#define pb          push_back
#define mp          make_pair
#define pii         pair<int,int>
#define pll         pair<ll,ll>
#define vii         vector<pair<ll,ll> >
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define endl        '\n'
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define rep1(i,a,b) for(ll i=a;i<= b;i++)
#define PI          3.14159
#define MAX         1000001
#define fast_io      ios_base::sync_with_stdio(false);  cin.tie(0); cout.tie(0);
#define scan(var)    scanf("%lld",&var)
#define print(var)   printf("%lld\n", var);
#define BLOCK 317
#define double       long double
#define mod          998244353
#define levelo        31
#define hell1        1000000009
using namespace std;
vector<ll> adj[200001];
bool status[200001];
ll dp[200001];
void dfs(ll child,ll parent,ll& ans,ll k)
{
  for(auto v : adj[child])
  {
    if(v == parent)
    {
      continue;
    }
    dfs(v,child,ans,k);
    ans += min(dp[v],2*k - dp[v]);
    dp[child] += dp[v];
  }
  if(status[child])
  {
    dp[child]++;
  }
  return ;
}
int main()
{
  fast_io
  ll t;
  t = 1;
  while(t--)
  {
    ll n,k;
    cin >> n >> k;
    rep(i,0,2*k)
    {
      ll x;
      cin >> x;
      status[x] = true;
    }
    rep(i,0,n - 1)
    {
      ll a,b;
      cin >> a >> b;
      adj[a].pb(b);
      adj[b].pb(a);
    }
    ll ans = 0;
    dfs(1,-1,ans,k);
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 200005
typedef pair<int,int> pii;
typedef long long ll;

int N,M,C;
vector<int> G[MAX];
int P[MAX][20],H[MAX],S[MAX];
bool V[MAX];

int dfs(int v,int p = -1){
    S[v] = 1;
    if(!V[v]) S[v] = 0;
	P[v][0] = p;
	if(p + 1)
		H[v] = H[p] + 1;
	for(int j = 1; 1<<j < N;j ++)
		if(P[v][j-1] + 1)
			P[v][j] = P[P[v][j-1]][j-1];
	for(auto u : G[v])	if(p - u)
		S[v] += dfs(u,v);
    if(C == -1 && S[v] >= M) C = v;
    return S[v];
}

int lca(int v,int u){
	if(H[v] < H[u])
		swap(v,u);
	for(int i = 16;i >= 0;i --)
		if(P[v][i] + 1 && H[P[v][i]] >= H[u])
			v = P[v][i];
	if(v == u)
		return v;
	for(int i = 16;i >= 0;i --)
		if(P[v][i] - P[u][i])
			v = P[v][i], u = P[u][i];
	return P[v][0];
}

int main()
{
    scanf("%d %d",&N,&M);
    int u,v,w;
    for(int i=0; i<M; i++)
    {
        scanf("%d %d",&u,&v);
        V[u] = V[v] = 1;
    }

    for(int i=1; i<N; i++)
    {
        scanf("%d %d",&u,&v);
        G[u].pb(v);
        G[v].pb(u);
    }

    C = -1;
    dfs(1);

    ll s = 0;
    for(int i=1; i<=N; i++)
    if(V[i])
    {
        w = lca(i,C);
        s += (H[i] - H[w]) + (H[C] - H[w]);
    }

    printf("%lld",s);

}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 200005
typedef pair<int,int> pii;
typedef long long ll;

int N,M;
vector<int> G[MAX];
vector<pii> R;
int P[MAX][20],H[MAX];
bool V[MAX];

void dfs(int v,int p = -1){
	P[v][0] = p;
	if(p + 1)
		H[v] = H[p] + 1;
		if(V[v]) R.pb(mp(H[v],v));
	for(int j = 1; 1<<j < N;j ++)
		if(P[v][j-1] + 1)
			P[v][j] = P[P[v][j-1]][j-1];
	for(auto u : G[v])	if(p - u)
		dfs(u,v);
}

int lca(int v,int u){
	if(H[v] < H[u])
		swap(v,u);
	for(int i = 16;i >= 0;i --)
		if(P[v][i] + 1 && H[P[v][i]] >= H[u])
			v = P[v][i];
	if(v == u)
		return v;
	for(int i = 16;i >= 0;i --)
		if(P[v][i] - P[u][i])
			v = P[v][i], u = P[u][i];
	return P[v][0];
}

int main()
{
    scanf("%d %d",&N,&M);
    int u,v,w;
    for(int i=0; i<M; i++)
    {
        scanf("%d %d",&u,&v);
        V[u] = V[v] = 1;
    }

    for(int i=1; i<N; i++)
    {
        scanf("%d %d",&u,&v);
        G[u].pb(v);
        G[v].pb(u);
    }

    dfs(1);

    sort(all(R));

    int s = 0;
    for(int i=0; i<M; i++)
    {
        u = R[i].y;
        v = R[M*2-i-1].y;
        w = lca(u,v);
        s += (H[u] - H[w]) + (H[v] - H[w]);
    }

    printf("%d",s);

}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 200005
typedef pair<int,int> pii;
typedef long long ll;

int N,M,C;
vector<int> G[MAX];
int P[MAX][20],H[MAX],S[MAX];
bool V[MAX];

int dfs(int v,int p = -1){
    S[v] = 1;
    if(!V[v]) S[v] = 0;
	P[v][0] = p;
	if(p + 1)
		H[v] = H[p] + 1;
	for(int j = 1; 1<<j < N;j ++)
		if(P[v][j-1] + 1)
			P[v][j] = P[P[v][j-1]][j-1];
	for(auto u : G[v])	if(p - u)
		S[v] += dfs(u,v);
    if(C == -1 && S[v] >= M) C = v;
    return S[v];
}

int lca(int v,int u){
	if(H[v] < H[u])
		swap(v,u);
	for(int i = 18;i >= 0;i --)
		if(P[v][i] + 1 && H[P[v][i]] >= H[u])
			v = P[v][i];
	if(v == u)
		return v;
	for(int i = 18;i >= 0;i --)
		if(P[v][i] - P[u][i])
			v = P[v][i], u = P[u][i];
	return P[v][0];
}

int main()
{
    scanf("%d %d",&N,&M);
    int u,v,w;
    for(int i=0; i<M; i++)
    {
        scanf("%d %d",&u,&v);
        V[u] = V[v] = 1;
    }

    for(int i=1; i<N; i++)
    {
        scanf("%d %d",&u,&v);
        G[u].pb(v);
        G[v].pb(u);
    }

    C = -1;
    dfs(1);

    ll s = 0;
    for(int i=1; i<=N; i++)
    if(V[i])
    {
        w = lca(i,C);
        s += (H[i] - H[w]) + (H[C] - H[w]);
    }

    printf("%lld",s);

}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 200005
typedef pair<int,int> pii;
typedef long long ll;

int N,M;
vector<int> G[MAX];
vector<pii> R;
int P[MAX][20],H[MAX];
bool V[MAX];

void dfs(int v,int p = -1){
	P[v][0] = p;
	if(p + 1)
		H[v] = H[p] + 1;
		if(V[v]) R.pb(mp(H[v],v));
	for(int j = 1; 1<<j < N;j ++)
		if(P[v][j-1] + 1)
			P[v][j] = P[P[v][j-1]][j-1];
	for(auto u : G[v])	if(p - u)
		dfs(u,v);
}

int lca(int v,int u){
	if(H[v] < H[u])
		swap(v,u);
	for(int i = 16;i >= 0;i --)
		if(P[v][i] + 1 && H[P[v][i]] >= H[u])
			v = P[v][i];
	if(v == u)
		return v;
	for(int i = 16;i >= 0;i --)
		if(P[v][i] - P[u][i])
			v = P[v][i], u = P[u][i];
	return P[v][0];
}

int main()
{
    scanf("%d %d",&N,&M);
    int u,v,w;
    for(int i=0; i<M; i++)
    {
        scanf("%d %d",&u,&v);
        V[u] = V[v] = 1;
    }

    for(int i=1; i<N; i++)
    {
        scanf("%d %d",&u,&v);
        G[u].pb(v);
        G[v].pb(u);
    }

    dfs(1);

    sort(all(R));

    ll s = 0;
    for(int i=0; i<M; i++)
    {
        u = R[i].y;
        v = R[M*2-i-1].y;
        w = lca(u,v);
        s += (H[u] - H[w]) + (H[v] - H[w]);
    }

    printf("%lld",s);

}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 100005
typedef pair<int,int> pii;
typedef long long ll;

int N,M;
vector<int> G[MAX];
vector<pii> R;
int P[MAX][20],H[MAX];
bool V[MAX];

void dfs(int v,int p = -1){
	P[v][0] = p;
	if(p + 1)
		H[v] = H[p] + 1;
		if(V[v]) R.pb(mp(H[v],v));
	for(int j = 1; 1<<j < N;j ++)
		if(P[v][j-1] + 1)
			P[v][j] = P[P[v][j-1]][j-1];
	for(auto u : G[v])	if(p - u)
		dfs(u,v);
}

int lca(int v,int u){
	if(H[v] < H[u])
		swap(v,u);
	for(int i = 16;i >= 0;i --)
		if(P[v][i] + 1 && H[P[v][i]] >= H[u])
			v = P[v][i];
	if(v == u)
		return v;
	for(int i = 16;i >= 0;i --)
		if(P[v][i] - P[u][i])
			v = P[v][i], u = P[u][i];
	return P[v][0];
}

int main()
{
    scanf("%d %d",&N,&M);
    int u,v,w;
    for(int i=0; i<M; i++)
    {
        scanf("%d %d",&u,&v);
        V[u] = V[v] = 1;
    }

    for(int i=1; i<N; i++)
    {
        scanf("%d %d",&u,&v);
        G[u].pb(v);
        G[v].pb(u);
    }

    dfs(1);

    sort(all(R));

    int s = 0;
    for(int i=0; i<M; i++)
    {
        u = R[i].y;
        v = R[M*2-i-1].y;
        w = lca(u,v);
        s += (H[u] - H[w]) + (H[v] - H[w]);
    }

    printf("%d",s);

}


#include<cstdio>
#include<algorithm>
using namespace std;
#define N 200010
int n,k,cnt,h[N],size[N];
long long ans;
struct data
{
	int to,next;
}edge[N<<1];
bool vis[N],mark[N];

void read(int &x,char ch=getchar())
{
	for (;ch<'0' || ch>'9';ch=getchar());
	for (x=0;ch>='0' && ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
}

void add(int u,int v)
{
	edge[++cnt]=(data){v,h[u]},h[u]=cnt,edge[++cnt]=(data){u,h[v]},h[v]=cnt;
}

void dfs(int x)
{
	int i;
	vis[x]=true;
	if (mark[x]) size[x]=1;
	for (i=h[x];i;i=edge[i].next)
	  if (!vis[edge[i].to]) dfs(edge[i].to),size[x]+=size[edge[i].to];
	ans+=min(size[x],2*k-size[x]);
}

int main()
{
	int i,x,y;
	read(n),read(k);
	for (i=1;i<=2*k;++i) read(x),mark[x]=true;
	for (i=1;i<n;++i) read(x),read(y),add(x,y);
	dfs(1);
	return printf("%I64d",ans),0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define N 200010
int n,k,cnt,h[N],size[N],ans;
struct data
{
	int to,next;
}edge[N<<1];
bool vis[N],mark[N];

void read(int &x,char ch=getchar())
{
	for (;ch<'0' || ch>'9';ch=getchar());
	for (x=0;ch>='0' && ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
}

void add(int u,int v)
{
	edge[++cnt]=(data){v,h[u]},h[u]=cnt,edge[++cnt]=(data){u,h[v]},h[v]=cnt;
}

void dfs(int x)
{
	int i;
	vis[x]=true;
	if (mark[x]) size[x]=1;
	for (i=h[x];i;i=edge[i].next)
	  if (!vis[edge[i].to]) dfs(edge[i].to),size[x]+=size[edge[i].to];
	ans+=min(size[x],2*k-size[x]);
}

int main()
{
	int i,x,y;
	read(n),read(k);
	for (i=1;i<=2*k;++i) read(x),mark[x]=true;
	for (i=1;i<n;++i) read(x),read(y),add(x,y);
	dfs(1);
	return printf("%d",ans),0;
}


#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;
bool br[(ll)2e5+5];
vector<ll> adj[(ll)2e5+5];
ll subt[(ll)2e5+5];
ll ans;
void dfs(ll i,ll par)
{
    if(br[i])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;
    memset(br,0,sizeof(br));
    rep(i,0,2*k){cin>>j;br[j]=1;}
    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 <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <ctime>
using namespace std;
typedef long long ll;
const int N = 100000 + 5;
const int P = 17;
int n, K, A, last[N], cnt, U[N], sz[N];
struct Edge{ int to, next; }e[N<<1];
void insert( int u, int v ){
	e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt;
	e[++cnt].to = u; e[cnt].next = last[v]; last[v] = cnt;
}
bool mark[N]; ll ans = 0;
void dfs( int x, int f ){
	for( int i = last[x]; i; i = e[i].next )
		if( e[i].to ^ f ){
			dfs( e[i].to, x );
			sz[x] += sz[e[i].to];
		}
	ans += min( sz[x], K - sz[x] );
}
int main(){
	scanf( "%d%d", &n, &K ); K *= 2;
	for( int i = 1; i <= K; i++ ) scanf( "%d", &U[i] ), sz[U[i]]++;
	for( int i = 1, u, v; i < n; i++ ) scanf( "%d%d", &u, &v ), insert( u, v );
	dfs( 1, 0 );
	cout<<ans<<endl;
	return 0;
}
/*
7 2 0
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6

4 2 1
1 2 3 4
1 2
2 3
3 4
*/


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <ctime>
using namespace std;
typedef long long ll;
const int N = 200000 + 5;
const int P = 17;
int n, K, A, last[N], cnt, U[N], sz[N];
struct Edge{ int to, next; }e[N<<1];
void insert( int u, int v ){
	e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt;
	e[++cnt].to = u; e[cnt].next = last[v]; last[v] = cnt;
}
bool mark[N]; ll ans = 0;
void dfs( int x, int f ){
	for( int i = last[x]; i; i = e[i].next )
		if( e[i].to ^ f ){
			dfs( e[i].to, x );
			sz[x] += sz[e[i].to];
		}
	ans += min( sz[x], K - sz[x] );
}
int main(){
	scanf( "%d%d", &n, &K ); K *= 2;
	for( int i = 1; i <= K; i++ ) scanf( "%d", &U[i] ), sz[U[i]]++;
	for( int i = 1, u, v; i < n; i++ ) scanf( "%d%d", &u, &v ), insert( u, v );
	dfs( 1, 0 );
	cout<<ans<<endl;
	return 0;
}
/*
7 2 0
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6

4 2 1
1 2 3 4
1 2
2 3
3 4
*/


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <iomanip>
#include <stack>
#include <sstream>
#include <queue>
#include <set>
#include <functional>

#define endl '\n'
#define eps 1e-9
#define ll long long int

using namespace std;

const int N = 200*1000+50;

vector<int> graph[N];
vector<int> children[N];
int subtree[N];
bool vis[N];
bool uni[N];

int dfs(int v) {
  vis[v] = true;
  int sub = 0;
  if (uni[v]) sub++;
  for (int i = 0; i < graph[v].size(); i++) {
    int child = graph[v].at(i);
    if (!vis[child]) {
      // children[v].push_back(child);
      sub += dfs(child);
    }
  }
  return subtree[v] = sub;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
  int n, k;
  cin >> n >> k;
  k *= 2;
  for (int i = 0; i < k; i++) {
    int t;
    cin >> t;
    uni[t] = true;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dfs(1);
  ll ret = 0;
  for (int i = 2; i <= n; i++) {
    ret += min(subtree[i], k-subtree[i]);
  }
  cout << ret << '\n';
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <iomanip>
#include <stack>
#include <sstream>
#include <queue>
#include <set>
#include <functional>

#define endl '\n'
#define eps 1e-9
#define ll long long int

using namespace std;

const int N = 200*1000+50;

vector<int> graph[N];
vector<int> children[N];
int subtree[N];
bool vis[N];
bool uni[N];

int dfs(int v) {
  vis[v] = true;
  int sub = 0;
  if (uni[v]) sub++;
  for (int child : graph[v]) {
    if (!vis[child]) {
      // children[v].push_back(child);
      sub += dfs(child);
    }
  }
  return subtree[v] = sub;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
  int n, k;
  cin >> n >> k;
  k *= 2;
  for (int i = 0; i < k; i++) {
    int t;
    cin >> t;
    uni[t] = true;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dfs(1);
  int ret = 0;
  for (int i = 2; i <= n; i++) {
    ret += min(subtree[i], k-subtree[i]);
  }
  cout << ret << '\n';
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <iomanip>
#include <stack>
#include <sstream>
#include <queue>
#include <set>
#include <functional>

#define endl '\n'
#define eps 1e-9
#define ll long long int

using namespace std;

const int N = 200*1000+50;

vector<int> graph[N];
vector<int> children[N];
int subtree[N];
bool vis[N];
bool uni[N];

int dfs(int v) {
  vis[v] = true;
  int sub = 0;
  if (uni[v]) sub++;
  for (int child : graph[v]) {
    if (!vis[child]) {
      // children[v].push_back(child);
      sub += dfs(child);
    }
  }
  return subtree[v] = sub;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
  int n, k;
  cin >> n >> k;
  k *= 2;
  for (int i = 0; i < k; i++) {
    int t;
    cin >> t;
    uni[t] = true;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dfs(1);
  ll ret = 0;
  for (int i = 2; i <= n; i++) {
    ret += min(subtree[i], k-subtree[i]);
  }
  cout << ret << '\n';
}


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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

const int V=200000;
vector<int> G[V];

int n,k;
int u[V]={};

int s[V];
int dfs(int now, int p)
{
    if(s[now]>=0) return s[now];

    int ret=u[now];

    rep(i,G[now].size())
    {
        int nx=G[now][i];
        if(nx == p) continue;
        ret += dfs(nx,now);
    }

    return s[now]=ret;
}

int main()
{
    scanf(" %d %d", &n, &k);
    rep(i,2*k)
    {
        int a;
        scanf(" %d", &a);
        --a;
        u[a]=1;
    }
    rep(i,n-1)
    {
        int x,y;
        scanf(" %d %d", &x, &y);
        --x;
        --y;
        G[x].pb(y);
        G[y].pb(x);
    }

    memset(s,-1,sizeof(s));
    dfs(0,0);

    ll ans=0;
    rep(i,n) ans += min(s[i],2*k-s[i]);
    cout << ans << endl;
    return 0;
}


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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

const int V=200000;
vector<int> G[V];

int n,k;
set<int> u;

int s[V];
int dfs(int now, int p)
{
    if(s[now]>=0) return s[now];

    int ret=0;
    if(u.count(now)) ++ret;

    rep(i,G[now].size())
    {
        int nx=G[now][i];
        if(nx == p) continue;
        ret += dfs(nx,now);
    }

    return s[now]=ret;
}

int main()
{
    scanf(" %d %d", &n, &k);
    rep(i,2*k)
    {
        int a;
        scanf(" %d", &a);
        --a;
        u.insert(a);
    }
    rep(i,n-1)
    {
        int x,y;
        scanf(" %d %d", &x, &y);
        --x;
        --y;
        G[x].pb(y);
        G[y].pb(x);
    }

    memset(s,-1,sizeof(s));
    dfs(0,0);

    ll ans=0;
    rep(i,n) ans += min(s[i],2*k-s[i]);
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int n, k, size[maxn];
bool f[maxn];
ll dp[maxn];
vector<int>g[maxn];
void dfs(int u, int fa){
    if(f[u]) size[u]=1;
    else size[u]=0;
    for(int i=0;i<g[u].size();i++){
        int v=g[u][i]; if(v==fa) continue;
        dfs(v,u); size[u]+=size[v];
        dp[u]+=dp[v]+size[v];
    }
}
ll ans=0;
int solve(int u, int fa){
    int sum=(f[u]?1:0);
    for(int i=0;i<g[u].size();i++){
        int v=g[u][i];
        if(v==fa) continue;
        sum+=solve(v,u);
    }
    ans+=min(sum,2*k-size[u]);
    return min(sum,2*k-size[u]);
}
int main(){
    scanf("%d%d",&n,&k); int x;
    for(int i=1;i<=2*k;i++) scanf("%d",&x), f[x]=1; int u, v;
    for(int i=1;i<=n-1;i++) scanf("%d%d",&u,&v), g[u].push_back(v), g[v].push_back(u);
    dfs(1,-1);solve(1,-1);
    printf("%lld\n",ans);
    return 0;
}


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

#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define ll long long
#define pii pair < ll , ll >

const int N = 2e5 + 100;
int n, k, sub[N], mark[N], uni[N];
ll ans;
vector < int > adj[N];

void DFS(int v = 0){
	mark[v] = 1;
	sub[v] += uni[v];
	for(int u : adj[v])
		if(!mark[u])
			DFS(u), sub[v] += sub[u], ans += min(sub[u], 2*k-sub[u]);
}

int main(){

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

	DFS();

	cout << ans << endl;
}


#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

#define eb emplace_back
//#define mp make_pair
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)
#define rep(i,j,n) for (int i = j; i < n; ++i)
#define repn(i,j,n) for (int i = j; i <= n; ++i)
#define sz(i) i.size()
#define all(v) v.begin(), v.end()
//#define endl '\n'
#define int long long
#define INF 1e18
#define ss second
#define ff first

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

//const int mod=1e9+7;
const int mod=998244353;
const int up=2e5+10;
const int MAXN=1e6+10;

int u[up],siz[up],vis[up],ans=0,k,a,b;
vector<int>adj[up];

void dfs(int u)
{
    vis[u]=1;
    for(auto &v:adj[u])
    {
        if(!vis[v])
        {
            dfs(v);
            siz[u]+=siz[v];
        }
    }
    ans+=min(siz[u],2*k-siz[u]);
}

void dfs2(int u)
{
    vis[u]=1;
    ans+=min(2*k-siz[u],siz[u]);
    //cout<<min(2*k-siz[u],siz[u])<<endl;
    for(auto &v:adj[u])
    {
        if(!vis[v])
        {
            dfs2(v);
        }
    }
}

void solve()
{
    memset(vis,0,sizeof(vis));
    int n;
    cin>>n>>k;
    repn(i,1,2*k)
    {
        cin>>u[i];
        siz[u[i]]++;
    }
    repn(i,1,n-1)
    {
        cin>>a>>b;
        adj[a].eb(b);
        adj[b].eb(a);
    }
    dfs(1);
    //cout<<a<<endl;
    // memset(vis,0,sizeof(vis));
    // dfs2(1);
    cout<<ans<<endl;
}

int32_t main()
{
    fast_cin();
    int T;
    //cin>>T;
    T=1;
    cout << fixed << setprecision(25);
    //cout<<prime[2]<<" "<<prime[6]<<endl;
    int t=1;
    while(t<=T)
    {
        solve();
        t++;
    }
}


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

#define int long long int
#define ld long double
#define mod 1000000007
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define forn(i,s, e) for (int i = s; i <= e; i++)
#define fornd(i,s, e) for (int i = s ; i >= e; i--)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define print(x) for(auto el: x) cout<<el<<" ";cout<<"\n"
#define PSET(x,y) fixed<<setprecision(y)<<x
// #define bits(x) __builtin_popcount(x)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
void fileio()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

template <typename T>
void swap(T& a, T& b){
    T temp = a;
    a = b;
    b = temp;
}

const int N = 1000001;

vector<int> * edges;
vector<int> unis;
int ans;

void dfs(int a, int p){
    for(int to: edges[a]){
        if(to==p) continue;
        dfs(to,a);
        unis[a]+= unis[to];
    }
}

void dfs2(int a, int p, int k){
    for(int to: edges[a]){
        if(to==p) continue;
        dfs2(to,a,k);
        ans += min(unis[to], 2*k - unis[to]);
    }
}

void solve(){
    int n,k;
    cin>>n>>k;

    unis.assign(n,0);
    edges =new  vector<int>[n];

    for(int i=0;i<2*k;i++){
        int a;
        cin>>a;
        unis[a-1]++;
    }
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        edges[u-1].push_back(v-1);
        edges[v-1].push_back(u-1);
    }
    dfs(0,-1);

    ans =0;

    dfs2(0,-1,k);

    cout<<ans<<endl;

}

int32_t main()
{
    fileio();
    fast;
    // int t;
    // cin>>t;
    // for(int y=1;y<=t;y++)
    {
        // cout<<"Case #"<<y<<": ";
        solve();
    }
    // int y = 0;
    // cout<<(y&&(y=3))<<endl;
    return 0;
}


/*
 * File:   main.cpp
 * Author: Nurlan Sofiyev
 * OS : Ubuntu 16
 *
 * Created 2017
 */
#include <bits/stdc++.h>
#define RLP(x,b,n) for( int x = b; x >= n; --x )
#define rlp(x,b) RLP(x,b,0)
#define LP(x,b,n) for( int x = b; x < n; ++x )
#define LPT(x,b,n) for( int x = b; x <= n; ++x )
#define lpt(x,n) LPT(x,0,n)
#define lp(x,n) LP(x,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define ff first
#define ss second
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<int, int> ii;
typedef pair<int, ii > iii;
typedef pair<ii, ii > iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector <iii> viii;
typedef vector <iiii> viiii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

int dr[] = {0, -1, 1, 0, -1, 1, -1, 1};
int dc[] = {-1, 0, 0, 1, 1, -1, -1, 1};

//void get_ints(vector<int>& v);
vi cities;
vvi graph;
vi used;

int stared_thread;
ii dfs(int v, int d){

    used[v] = stared_thread;
    int limit = graph[v].size();
    ii temp = mp(0,0);
    if( cities[v] ) temp.ff = d, temp.ss = v;
    lp(i,limit){
        int to = graph[v][i];
        if( used[to] != stared_thread  ){
            ii temp2 = dfs(to,d+1);
            if(!temp2.ss) continue;
            if( temp2.ff > temp.ff ){
                temp.ff = temp2.ff;
                temp.ss = temp2.ss;
            }
        }
    }
    return temp;

}
ll calc(int v){
    stared_thread = v;
    used[v] = v;
    vii answers;
    answers.pb(mp(0,v) );
    answers.pb( mp(0,v) );
    int limit = graph[v].size();
    lp(i,limit){
        int to = graph[v][i];
        ii temp2 = dfs(to,1);
        if( temp2.ss ){
            answers.pb(temp2);
        }
    }
    limit = answers.size();
    sort(answers.begin(),answers.end());
    cities[ answers[limit-1].ss ] = cities[ answers[limit-2].ss ] = 0;

    return ( answers[limit-1].ff  +  answers[limit-2].ff );

}
int main() {
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    int n, k;
    cin >> n >> k;
    cities.resize(n+1);
    graph.resize(n+1);
    used.resize(n+1,0);
    int limit = 2*k;
    lp(i,limit){
        int temp;
        cin >> temp;
        cities[temp] = 1;
    }
    LP(i,1,n){
        int a, b;
        cin >> a >> b;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    ll cavab = 0;
    lp(i,n){
        while( cities[i]  ){
            cavab += calc( i );
        }
    }
    cout << cavab << endl;

    return 0;

}

/*
4
2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2

 */

/*
void get_ints(vector<int>& v)
{
        string s;
        int temp;
        getline(cin,s);
    stringstream ss(s);
        while(ss>>temp) v.push_back(temp);

}
 */
//freopen ("out.in","w",stdout);
//freopen ("in.out","r",stdin);


/*
 * File:   main.cpp
 * Author: Nurlan Sofiyev
 * OS : Ubuntu 16
 *
 * Created 2017
 */
#include <bits/stdc++.h>
#define RLP(x,b,n) for( int x = b; x >= n; --x )
#define rlp(x,b) RLP(x,b,0)
#define LP(x,b,n) for( int x = b; x < n; ++x )
#define LPT(x,b,n) for( int x = b; x <= n; ++x )
#define lpt(x,n) LPT(x,0,n)
#define lp(x,n) LP(x,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define ff first
#define ss second
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<int, int> ii;
typedef pair<int, ii > iii;
typedef pair<ii, ii > iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector <iii> viii;
typedef vector <iiii> viiii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

int dr[] = {0, -1, 1, 0, -1, 1, -1, 1};
int dc[] = {-1, 0, 0, 1, 1, -1, -1, 1};

//void get_ints(vector<int>& v);
int n, k;
vvi tree;
vi is_university;
ll answer = 0;
int bfs_s(int v, int from){
    int temp = 0;

    if( is_university[v] ) ++temp;
    int limit = tree[v].size();
    lp(i,limit){
        int to = tree[v][i];
        if( to != from  ){
            ll temp2 = bfs_s(to,v);
            temp += temp2;
            answer += ll( min(temp2, k - temp2  )*1ll*1 );
        }
    }
    return temp;
}
int main() {
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    cin >> n >> k;
    k = k << 1;
    tree.resize(n+1);
    is_university.resize(n+1,0);
    lp(i,k){
        int a;
        cin >> a;
        is_university[a] = 1;

    }
    LP(i,1,n){
        int a, b;
        cin >> a >> b;
        tree[a].pb(b);
        tree[b].pb(a);
    }
    bfs_s(1,0);
    cout << answer << endl;

    return 0;

}

/*
4
2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2

 */

/*
void get_ints(vector<int>& v)
{
        string s;
        int temp;
        getline(cin,s);
    stringstream ss(s);
        while(ss>>temp) v.push_back(temp);

}
 */
//freopen ("out.in","w",stdout);
//freopen ("in.out","r",stdin);


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long LL;
typedef pair <int,int> II;
const int maxn = 2e5+10;
const int maxint = 0x3f3f3f3f;
const int mod = 1e9+7;

int n , k , f[maxn] , sp[maxn] , mi = mod , p;
vector <int> v[maxn];
LL ans;

void dfs1(int x , int y)
{
	int i;
	if (f[x]) sp[x]++;
	for (i = 0 ; i < v[x].size() ; i++)
	{
		if (v[x][i] != y)
		{
			dfs1(v[x][i],x);
			sp[x] += sp[v[x][i]];
		}
	}
}

void dfs2(int x , int y , int s)
{
	int i , ma = s;
	if (f[x]) sp[x]++;
	for (i = 0 ; i < v[x].size() ; i++)
	{
		if (v[x][i] != y)
		{
			dfs2(v[x][i],x,s+sp[x]-sp[v[x][i]]);
			ma = max(ma,sp[v[x][i]]);
		}
	}
	if (ma < mi)
	{
		mi = ma;
		p = x;
	}
}

void dfs3(int x , int y , int deep)
{
	int i;
	if (f[x]) ans += (LL)deep;
	for (i = 0 ; i < v[x].size() ; i++)
	{
		if (v[x][i] != y)
		{
			dfs3(v[x][i],x,deep+1);
		}
	}
}

int main()
{
	int i , x , y;
	cin >> n >> k;
	k *= 2;
	for (i = 1 ; i <= k ; i++)
	{
		scanf("%d",&x);
		f[x] = 1;
	}
	for (i = 1 ; i < n ; i++)
	{
		scanf("%d%d",&x,&y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs1(1,0);
	dfs2(1,0,0);
	dfs3(p,0,0);
	cout << ans << "\n";
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long LL;
typedef pair <int,int> II;
const int maxn = 2e5+10;
const int maxint = 0x3f3f3f3f;
const int mod = 1e9+7;

int n , k , f[maxn] , sp[maxn] , mi = mod , p;
vector <int> v[maxn];
LL ans;

void dfs1(int x , int y)
{
	int i;
	if (f[x]) sp[x]++;
	for (i = 0 ; i < v[x].size() ; i++)
	{
		if (v[x][i] != y)
		{
			dfs1(v[x][i],x);
			sp[x] += sp[v[x][i]];
		}
	}
}

void dfs2(int x , int y , int s)
{
	int i , ma = s;
	for (i = 0 ; i < v[x].size() ; i++)
	{
		if (v[x][i] != y)
		{
			dfs2(v[x][i],x,s+sp[x]-sp[v[x][i]]);
			ma = max(ma,sp[v[x][i]]);
		}
	}
	if (ma < mi)
	{
		mi = ma;
		p = x;
	}
}

void dfs3(int x , int y , int deep)
{
	int i;
	if (f[x]) ans += (LL)deep;
	for (i = 0 ; i < v[x].size() ; i++)
	{
		if (v[x][i] != y)
		{
			dfs3(v[x][i],x,deep+1);
		}
	}
}

int main()
{
	int i , x , y;
	cin >> n >> k;
	k *= 2;
	for (i = 1 ; i <= k ; i++)
	{
		scanf("%d",&x);
		f[x] = 1;
	}
	for (i = 1 ; i < n ; i++)
	{
		scanf("%d%d",&x,&y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs1(1,0);
	dfs2(1,0,0);
	dfs3(p,0,0);
	cout << ans << "\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define allof(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5 + 5;
int N, K;
vector<int> adj[MAXN];
int cnt[MAXN];
bool uni[MAXN];
ll ans = 0;
void dfs2(int pos, int par, int len){
    if(uni[pos]){
        ans += len;
    }
    for(int i : adj[pos]){
        if(i == par){
            continue;
        }
        dfs2(i, pos, len + 1);
    }
}
pii best = pii(1e9, 0);
void dfs1(int pos, int par){
    int mx = 0;
    for(int i : adj[pos]){
        if(i == par){
            continue;
        }
        dfs1(i, pos);

        mx = max(mx, cnt[i]);
        cnt[pos] += cnt[i];
    }

    best = min(best, pii(max(mx, 2 * K - cnt[pos]), pos));
}
int main(){
    cin.tie(0);
    cin.sync_with_stdio(0);

    cin >> N >> K;
    for(int a = 1; a <= K * 2; a++){
        int x;
        cin >> x;
        cnt[x] = 1;
        uni[x] = true;
    }
    for(int a = 1; a <= N - 1; a++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs1(1, -1);
    dfs2(best.second, -1, 0);
    cout << ans;
}