#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

int IT_MAX = 1 << 19;
int MOD = 10000000;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
#define szz(x) (int)(x).size()
#define rep(i, n) for(int i=0;i<n;i++)
#define Se second
#define Fi first

vector <int> son[2050];
int par[2050];
int val[2050];
bool isdec[2050][2050];
int deccnt[2050];
int cnt[2050];
bool chk[2050];

vector <int> Vd[2050];
vector <int> Vrd[2050];
void DFS(int n, int on) {
	Vd[on].push_back(n);
	Vrd[n].push_back(on);
	isdec[on][n] = true;
	deccnt[on]++;
	for (auto it : son[n]) DFS(it, on);
}

vector <int> Vl;
int findpos(int n, int t) {
	if (son[n].empty()) return n;
	int mn = son[n][0];
	for (auto it : son[n]) if (val[mn] > val[it]) mn = it;
	if (val[mn] > t) return n;
	return findpos(mn, t);
}

vector <int> Va;
vector <int> Vur;
void myrotate(int x) {
	Va.push_back(x);
	
	Vur.clear();
	Vur.push_back(x);
	while (Vur.back() != 0) Vur.push_back(par[Vur.back()]);

	int v = val[0];
	for (int i = (int)Vur.size() - 2; i >= 0; i--) val[Vur[i + 1]] = val[Vur[i]];
	val[Vur[0]] = v;
}

int DEBUG = 0;
int main() {
	int N, i, j;

	if(!DEBUG) scanf("%d", &N);
	else {
		int seed;
		scanf("%d %d", &N, &seed);
		srand(seed);
	}
	for (i = 1; i < N; i++) {
		if(!DEBUG) scanf("%d", &par[i]);
		else par[i] = i - 1;
		son[par[i]].push_back(i);
	}

	if(!DEBUG) for (i = 0; i < N; i++) scanf("%d", &val[i]);
	else {
		for (i = 0; i < N; i++) val[i] = N - 1 - i;
		random_shuffle(val, val + N);
	}
	for (i = 0; i < N; i++) DFS(i, i);
	while (!chk[0]) {
		int t = val[0];
		Vl.clear();
		for (i = 0; i < N; i++) if (isdec[t][i] && cnt[i] == deccnt[i] - 1) Vl.push_back(i);

		bool cu = false;
		if (Vl.empty()) {
			cu = true;
			Vl.push_back(t);
		}

		random_shuffle(all(Vl));
		int x = Vl[0];

		if (cnt[x] == deccnt[x] - 1) {
			chk[x] = true;
			for (i = 0; i < N; i++) if (isdec[i][x]) cnt[i]++;
		}
		x = findpos(x, t);
		myrotate(x);
		if (cu) {
			while (chk[x]) {
				if (!isdec[val[x]][x]) {
					for (auto it : Vrd[x]) {
						if (!chk[it]) continue;
						chk[it] = false;
						for (auto it2 : Vrd[it]) cnt[it2]--;
					}
					//printf("Broke at %d\n", x);
					break;
					
				}
				if (x == 0) break;
				x = par[x];
			}
		}

		if (DEBUG && chk[0]) {
			printf("val: ");
			for (i = 0; i < N; i++) printf("%d ", val[i]);
			printf("\n");
			printf("chk: ");
			for (i = 0; i < N; i++) printf("%d ", (int)chk[i]);
			printf("\n");
			printf("-------------");
			printf("\n");

			for (i = 0; i < N; i++) if (val[i] != i) break;
			if (i < N) printf("ERROR OCCURED!! at %d\n", i);
		}
	}
	printf("%d\n", (int)Va.size());
	if(!DEBUG) for (auto it : Va) printf("%d\n", it);
	return 0;
}
//*/

Measured memory_bytes: 25728