#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cassert>
#include<ctime>
#include<cstdlib>
#define cmax(a,b) (a<(b)?a=(b),1:0)
#define cmin(a,b) (a>(b)?a=(b),1:0)
#define dmin(a,b) ((a)<(b)?(a):(b))
#define dmax(a,b) ((a)>(b)?(a):(b))
#define CL fclose(stdin),fclose(stdout)
namespace io
{
	int F()
	{
		int n=0,F=1;
		char ch;
		while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
		ch=='-'?F=0:n=ch-'0';
		while((ch=getchar())>='0'&&ch<='9')n=(n<<1)+(n<<3)+ch-'0';
		return F?n:-n;
	}
	long long G()
	{
		long long n=0,F=1;
		char ch;
		while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
		ch=='-'?F=0:n=ch-'0';
		while((ch=getchar())>='0'&&ch<='9')n=(n<<1)+(n<<3)+ch-'0';
		return F?n:-n;
	}
}
int fa[2222],a[2222];
int lb[2222],dep[2222];
int mx[2222],se[2222],son[2222];
int vis[2222];
struct edge
{
	int to;
	int next;
}e[4444];
int pe=2222;
void insert(int a,int to)
{
	e[pe]=(edge){to,e[a].next};
	e[a].next=pe++;
}
int op[33333],pp;
void dd(int o)
{
	op[++pp]=o;
	int now=o,la=a[0],tmp;
	while(1)
	{
		tmp=la,la=a[now],a[now]=tmp;
		if(!now)return;
		now=fa[now];
	}
}
int main()
{
	int n=io::F();
	for(register int i=1;i<n;++i)fa[i]=io::F(),insert(fa[i],i),dep[i]=dep[fa[i]]+1;
	for(register int i=0;i<n;++i)a[i]=io::F();
	for(register int i=n-1;i>=0;--i)
	{
		for(register int p=e[i].next;p;p=e[p].next)
			if(lb[e[p].to]>=mx[i])se[i]=mx[i],mx[i]=lb[e[p].to],son[i]=e[p].to;
				else cmax(se[i],lb[e[p].to]);
		if(mx[i]==se[i])lb[i]=mx[i]+1,son[i]=-1;
			else lb[i]=mx[i];
	}
	for(register int T=1;T<=lb[0];++T)
	{
		int cnt=0;
		for(register int i=0;i<n;++i)if(lb[i]==T)++cnt;
		while(cnt)
		{
			if(lb[a[0]]==T)
			{
				int now=a[0];
				while(son[now]!=-1&&vis[son[now]]==0)now=son[now];
				while(vis[now])now=fa[now];
				vis[now]=1;
				while(son[now]!=-1&&dep[a[son[now]]]<dep[a[0]])now=son[now];
				dd(now);
				--cnt;
			}
			else
			{
				int pl,dp=0;
				for(register int i=0;i<n;++i)
					if(vis[i]==0&&lb[a[i]]==T)
						if(cmax(dp,dep[i]))pl=i;
				dd(pl);
			}
		}
	}
	for(register int i=0;i<n;++i)assert(a[i]==i);
	assert(pp<=25000);
	printf("%d\n",pp);
	for(register int i=1;i<=pp;++i)printf("%d\n",op[i]);
	return 0;
}

