#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; 
typedef pair<int, int> P; 
const int mod = 1e9 + 7;
const int N = 2e6 + 5;
struct ed{
	int to, nxt;
}e[N << 1];
int head[N], tot = 1;
void add(int u, int v){
	e[tot].to = v, e[tot].nxt = head[u], head[u] = tot++;
}

int n, s, t, dep[N], mx[N], fa[N], ans, tp;
void dfs(int x, int last){
	fa[x] = last;
	dep[x] = dep[last] + 1;
	mx[x] = dep[x];
	for (int i = head[x]; i; i = e[i].nxt){
		int v = e[i].to;
		if (v == last)	continue;
		dfs(v, x);
		mx[x] = max(mx[x], mx[v]);
	}
}
int  main()
{
	scanf("%d%d%d", &n, &s, &t);
	for (int i = 0; i < n - 1; i++){
		int x, y;
		scanf("%d%d", &x, &y);
		add(x, y);
		add(y, x);
	}
	dfs(t, 0);
	t = 1;
	ans = (dep[s] - dep[t]) >> 1;
	while (true){
		if (dep[s] <= t)	break;
		if ((dep[s] - dep[t]) & 1)
			ans = max(ans, mx[s] - t - 1 + tp);
		else
			ans = max(ans, mx[s] - t - 1 + tp);
		s = fa[s];
		t++;
		tp++;
	}
	printf("%d\n", ans);
}
#pragma GCC optimize("O3")
//#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")
#define io_init cin.tie(0);ios::sync_with_stdio(0);cout<<setprecision(20)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int main() {
	int N;
	cin >> N;
	vector<vector<pair<int, int>>> person(N, vector<pair<int, int>>());
	for (int i = 0; i < N; i++) {
		int A;
		cin >> A;
		for (int j = 0; j < A; j++) {
			int x, y;
			cin >> x >> y;
			x--;
			person[i].push_back({ x,y });
		}
	}
	int ans = 0;
	//bitが立っている人が正直
	for (int i = 0; i < (1 << N); i++) {
		bool flag = true;
		int cnt = 0;
		for (int j = 0; j < N; j++) {
			if ((i & (1 << j)) == 0)continue;
			cnt++;
			for (int k = 0; k < person[j].size(); k++) {
				if (((i & (1 << person[j][k].first)) != 0) != person[j][k].second)flag = false;
			}
		}
		if (flag)ans = max(ans, cnt);
	}
	cout << ans << endl;
}