#include<bits/stdc++.h>

#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-funroll-loops")
#pragma GCC target("avx,sse4")

#define mms(a,n) memset(a,0,sizeof((a)[0])*(n))
#define mmp(a,b,n) memcpy(a,b,sizeof((b)[0])*(n))
#define lowbit(x) ((x)&-(x))
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define fo(i,l,r) for(register int i=l,_lim_=r;i<=_lim_;i++)
#define fd(i,r,l) for(register int i=r,_lim_=l;i>=_lim_;i--)
#define fos(i,l,r,d) for(register int i=l,_lim_=r;i<=r;i+=d)
#define fol(i,l,r) for(register ll i=l,_lim_=r;i<=_lim_;i++)
#define fdl(i,r,l) for(register ll i=r,_lim_=l;i>=_lim_;i--)
#define fosl(i,l,r,d) for(register ll i=l,_lim_=r;i<=r;i+=d)
#define Clear(a) memset(a,0,sizeof(a))
#define Copy(a,b) memcpy(a,b,sizeof(b))
#define ALL(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ldb;
typedef double db;
typedef pair<int,ll> pi;

namespace io{
	const int L=(1<<21)+1;
	char ibuf[L],*iS,*iT,obuf[L],*oS=obuf,*oT=obuf+L-1,c,st[55];int f,tp;
	#ifdef whzzt
		#define gc() getchar()
	#else
		#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	#endif
	inline void flush(){fwrite(obuf,1,oS-obuf,stdout);oS=obuf;}
	inline void putc(char x){*oS++=x;if(oS==oT)flush();}
	template<class I>
	inline void gi(I&x){
		for(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;
		for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
	}
	template<class I>
	inline void print(I x){
		if(!x)putc('0');if(x<0)putc('-'),x=-x;
		while(x)st[++tp]=x%10+'0',x/=10;
		while(tp)putc(st[tp--]);
	}
	inline void gs(char*s,int&l){
		for(c=gc();c<'a'||c>'z';c=gc());
		for(l=0;c<='z'&&c>='a';c=gc())s[l++]=c;
	}
	inline char O(){
		for(c=gc();c!='C'&&c!='Q'&&c!='R';c=gc());
		return c;
	}
	inline void ps(const char*s){
		fo(i,0,strlen(s)-1)putc(s[i]);
	}
};
using io::putc;
using io::gi;
using io::gs;
using io::print;

const int N=2005;
int n,p[N],w[N],sz[N],son[N],bel[N],bot[N],m,pos[N];
bool del[N],red[N],vi[N];
vector<int>e[N],O,li[N];

void dfs(int u){
	sz[u]=1;
	for(int v:e[u]){
		dfs(v),sz[u]+=sz[v];
		if(sz[v]>sz[son[u]])son[u]=v;
	}
}
void dfs(int u,int b){
	bel[u]=b;li[b].pb(u);pos[u]=SZ(li[b]);
	if(u==b)red[u]=true;if(e[u].size()>1)red[b]=false;
	if(son[u])dfs(son[u],b);else bot[b]=u;
	for(int v:e[u])if(v!=son[u])dfs(v,v);
	if(red[u])for(int v:li[u])red[v]=true,m++;
}
void flip(int u){
	O.pb(u);for(int v=p[u];v;v=p[v])swap(w[v],w[u]);
}
int find(int u){
	bool o=true;
	for(int v:e[u]){
		int p=find(v);
		if(p>0)return p;
		o&=p==-1; 
	}
	if(o)return vi[w[u]]?-1:u;
	return 0;
}
void move(){
	int u=w[1];if(!vi[u])vi[u]=true;m-=red[u];
	if(red[u])for(int v=bot[bel[u]];v;v=p[v])if(!red[w[v]]||!vi[w[v]]||pos[w[v]]<=pos[u])return flip(v);
	flip(find(1));
}
bool fix(){
	fo(i,2,n)if(!del[i])e[p[i]].pb(i);
	if(!e[1].size())return false;
	dfs(1);dfs(1,1);
	while(m)move();
	fo(i,1,n)e[i].clear(),li[i].clear(),del[i]|=red[i],red[i]=vi[i]=bot[i]=pos[i]=bel[i]=son[i]=sz[i]=0;m=0;
	return true;
}

int main(){
	gi(n);
	fo(i,2,n)gi(p[i]),p[i]++;
	fo(i,1,n)gi(w[i]),w[i]++;
	while(fix());
	print(O.size());putc('\n');
	for(int v:O)print(v-1),putc('\n');
	io::flush();
	return 0;
}

Measured memory_bytes: 2816