#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ub upper_bound
#define lb upper_bound
#define bs binary_search
#define fi first
#define se second
using pii = pair<int,int>;
using ll = long long;
const int maxn = 4e5;
const int inf = 1e9;
const int mod = 1e9+7;

void open_io() { freopen("in","r",stdin); freopen("out","w",stdout); }
void fast_io() { ios::sync_with_stdio(0); cout.tie(0);  cin.tie(0);  }

int n,ans;
vector<int> adj[maxn+5];
set<int> num;
set<int> s;

int dfs(int u)
{
	int ret = adj[u].size();
	s.insert(u);
	for(int v : adj[u])
		if(not s.count(v))
			ret += dfs(v);
	return ret;
}

int32_t main () 
{
	//open_io();
	fast_io();
	cin >> n;
	for(int i=1;i<=n;i++)
	{
		int x,y;
		cin >> x >> y;
		adj[x].pb(y);
		adj[y].pb(x);
		num.insert(x);
		num.insert(y);
	}
	for(int x : num)
		if(not s.count(x))
		{
			int tmp = s.size();
			int edge = dfs(x)/2;
			int sze = s.size() - tmp;
			ans += min(sze,edge);
		}
	cout << ans << endl;
}	
	

#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double
using pii = pair<int, int>;
template<typename T>
using Prior = std::priority_queue<T>;
template<typename T>
using prior = std::priority_queue<T, vector<T>, greater<T>>;

#define X first
#define Y second
#define eb emplace_back
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline int getRand(int L, int R) {
    if (L > R) swap(L, R);
    return (int)(rng() % ((uint64_t)R - L + 1) + L);
}

template<typename T1, typename T2>
ostream& operator << (ostream &os, pair<T1, T2> &p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template<typename T>
ostream& operator << (ostream &os, vector<T> &vec) {
    for (int i = 0; i < vec.size(); ++i) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}

const int maxn = 2E5 + 5;

vector<int> adj[maxn], dep(maxn);
vector<pii> timestamp(maxn);
vector<int> st[maxn]; /// record timestamp in st[depth]

void dfs(int now) {
    static int tok = 0;
    st[dep[now]].eb(timestamp[now].X = ++tok);
    for (auto x : adj[now]) dfs(x);
    timestamp[now].Y = ++tok;
}

void solve() {
    int N; cin >> N;
    for (int i = 2, par; i <= N; ++i) cin >> par, adj[par].eb(i), dep[i] = dep[par] + 1;
    
    dfs(1);
    
    int Q; cin >> Q;
    while (Q--) {
        int u, d; cin >> u >> d;
        /// query how many x \in st[d] \cap [in_u, out_u] ///
        cout << upper_bound(ALL(st[d]), timestamp[u].Y)
              - lower_bound(ALL(st[d]), timestamp[u].X) << "\n";
    }
}

int32_t main() {
    fastIO();
    
    int t = 1; // cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case #" << _ << ": ";
        solve();
    }
    
    return 0;
}