#include "september.h"
#include <bits/stdc++.h>
using namespace std;
int solve(int N, int M, vector<int> F, vector<vector<int>> S) {
vector<vector<int>> child(N);
for (int i = 1; i < N; i++) {
child[F[i]].push_back(i);
}
vector<int> tin(N), tout(N);
int timer = 0;
vector<pair<int, int>> st_dfs;
st_dfs.push_back({0, 0});
while (!st_dfs.empty()) {
int u = st_dfs.back().first;
int &idx = st_dfs.back().second;
if (idx == 0) {
tin[u] = timer++;
}
if (idx < (int)child[u].size()) {
int v = child[u][idx++];
st_dfs.push_back({v, 0});
} else {
tout[u] = timer - 1;
st_dfs.pop_back();
}
}
set<pair<int, int>> alive;
for (int i = 1; i < N; i++) {
alive.insert({tin[i], i});
}
int ans = 0;
for (int node : S[0]) {
auto cur = alive.find({tin[node], node});
if (cur == alive.end()) {
continue;
}
ans++;
while (true) {
auto it = alive.lower_bound({tin[node], -1});
if (it == alive.end() || it->first > tout[node]) {
break;
}
alive.erase(it);
}
}
return ans;
}