This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector <int> v[200005],v1,v2[200005];
int d1,b1,cnt1,fr[200005],cnt[200005],dep[200005];
void dfs(int x,int d) {
	dep[x]=d;
	for(int i=0;i<v[x].size();i++) {
		dfs(v[x][i],d+1);
	}
}
void dfs1(int x) {
	if(dep[x]>=d1) fr[x]=1;
	cnt1+=fr[x];
	for(int i=0;i<v[x].size();i++) {
		dfs1(v[x][i]);
	}
}
void dfs2(int x) {
	if(b1) return;
	int mx=-1,idx;
	for(int i=0;i<v[x].size();i++) {
		dfs2(v[x][i]);
		if(b1) return;
		fr[x]+=fr[v[x][i]];
		if((int)v2[v[x][i]].size()>mx) {
			mx=v2[v[x][i]].size();
			idx=i;
		}
	}
	if(!v[x].empty()) {
		swap(v2[x],v2[v[x][idx]]);
		for(int i=0;i<v[x].size();i++) {
			for(int j=0;j<v2[v[x][i]].size();j++) v2[x].push_back(v2[v[x][i]][j]);
			v2[v[x][i]].clear();
		}
	}
	sort(v2[x].begin(),v2[x].end());
	if(!v2[x].empty() && v2[x].back()-dep[x]>=dep[x]-1) {
		b1=1;
		return;
	}
	while(!v2[x].empty() && fr[x]>0) {
		v2[x].pop_back();
		fr[x]-=1;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,m,a=0,a1=0;
	cin >> n >> m;
	for(int i=1;i<n;i++) {
		int x;
		cin >> x;
		a+=(x==i);
		v[x-1].push_back(i);
	}
	for(int i=0;i<m;i++) {
		int x;
		cin >> x;
		if(x==1) a1=1;
		v1.push_back(x-1);
	}
	if(a==n-1) {
		if(a1) cout << 1;
		else cout << n-m+1;
		return 0;
	}
	dfs(0,1);
	int lo=2,hi=n;
	while(lo<hi) {
		int mid=(lo+hi+1)/2;
		b1=0;
		cnt1=0;
		d1=mid;
		//cout << "---" << d1 << "\n\n";
		for(int i=0;i<n;i++) {
			fr[i]=0;
			cnt[i]=0;
			v2[i].clear();
		}
		dfs1(0);
		if(cnt1<m) {
			hi=mid-1;
			continue;
		}
		for(int i=0;i<m;i++) {
			cnt[v1[i]]=1;
			v2[v1[i]].push_back(dep[v1[i]]);
		}
		dfs2(0);
		for(int i=0;i<n;i++) {
			if(!v2[i].empty()) b1=1;
		}
		if(!b1) lo=mid;
		else hi=mid-1;
	}
	cout << lo;
	return 0;
}
/*
           __
          /\ \
         /  \ \
        / /\ \ \
       / / /\ \ \
      / / /__\_\ \
     / / /________\
     \/___________/
*/
 
Compilation message (stderr)
mokesciai.cpp: In function 'void dfs(int, int)':
mokesciai.cpp:9:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 |  for(int i=0;i<v[x].size();i++) {
      |              ~^~~~~~~~~~~~
mokesciai.cpp: In function 'void dfs1(int)':
mokesciai.cpp:16:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for(int i=0;i<v[x].size();i++) {
      |              ~^~~~~~~~~~~~
mokesciai.cpp: In function 'void dfs2(int)':
mokesciai.cpp:23:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  for(int i=0;i<v[x].size();i++) {
      |              ~^~~~~~~~~~~~
mokesciai.cpp:34:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |   for(int i=0;i<v[x].size();i++) {
      |               ~^~~~~~~~~~~~
mokesciai.cpp:35:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |    for(int j=0;j<v2[v[x][i]].size();j++) v2[x].push_back(v2[v[x][i]][j]);
      |                ~^~~~~~~~~~~~~~~~~~~
mokesciai.cpp:33:25: warning: 'idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   33 |   swap(v2[x],v2[v[x][idx]]);
      |                         ^| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |