#include <bits/stdc++.h>
using namespace std;


vector<int> par;
vector<bool> exists;

int n;
vector<int> cur;
vector<int> ops;

void make_move(int v){
	ops.push_back(v);
	int x = cur[0];
	while(true){
		swap(x, cur[v]);
		if(v == 0) break;
		v = par[v];
	}
	assert(x == cur[ops.back()]);
}

void solve(){
	while(true){
		vector<int> deg(n, 0);
		vector<int> ch(n, -1);
		for(int i = 0; i < n; i++){
			if(!exists[i]) continue;
			if(i > 0){
				deg[par[i]]++;
				ch[par[i]] = i;
			}
		}
		vector<bool> cur_step(n, false);
		for(int i = 0; i < n; i++){
			if(!exists[i]) continue;
			if(deg[i] == 0){
				int x = i;
				while(x > 0 && deg[x] <= 1){
					cur_step[x] = true;
					x = par[x];
				}
			}
		}

		vector<bool> up_tree(n, false);
		for(int i = 0; i < n; i++){
			if(exists[i]) up_tree[i] = true;
		}
		vector<bool> placed(n, false);
		while(up_tree[0]){
			int v = cur[0];
			if(cur_step[v]){
				int x = v;
				while(ch[x] != -1){
					x = ch[x];
				}
				while(placed[x]) x = par[x];
				assert(cur_step[x]);
				placed[x] = true;
				up_tree[x] = false;
				while(ch[x] != -1 && cur[ch[x]] < v){
					x = ch[x];
				}
				make_move(x);
			} else {
				for(int x = n-1; x >= 0; x--){
					if(up_tree[x]){
						up_tree[x] = false;
						make_move(x);
						break;
					}
				}
			}
		}

		for(int i = 0; i < n; i++){
			if(cur_step[i]){
				exists[i] = false;
				assert(cur[i] == i);
			}
		}
		int nexists = 0;
		for(int a : exists) nexists += a;
		if(nexists == 1) break;
	}
}

int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	par.resize(n);
	par[0] = -1;
	for(int i = 1; i < n; i++) cin >> par[i];

	cur.resize(n);
	for(int i = 0; i < n; i++) cin >> cur[i];
	exists.assign(n, true);
	solve();
	cout << ops.size() << '\n';
	for(int a : ops){
		cout << a << ' ';
	}
	cout << '\n';
}
