#ifndef __MAKRA_H_
#define __MAKRA_H_

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

int mod = 1e9 + 7;

#define ALL(x) x.begin(), x.end()
#define SZ(x) (int) x.size()
#define MP make_pair
#define EB emplace_back
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FORD(i, a, b) for (int i = a; i >= b; --i)

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("ipa-pta")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template < class C > struct rge { C b, e; };
template < class C > rge<C> range(C i, C j) { return rge<C>{i, j}; }
template < class C > auto dud(C* x) -> decltype(cerr << *x, 0);
template < class C > char dud(...);
template < class C > rge<C*> tblAux(C *p, int i, int j) { return rge<C*>{p + i, p + j + 1}; }

struct debug {
  #ifdef LOCAL 
  ~debug() { cerr << endl; }
  template < class C > typename enable_if<sizeof dud<C>(0) != 1, debug&>::type operator<<(C i) { cerr << i; return * this; }
  template < class C > typename enable_if<sizeof dud<C>(0) == 1, debug&>::type operator<<(C i) { return * this << range(begin(i), end(i)); }

  template < class C, class B > debug & operator <<(pair < C, B > d) {
    return * this << "(" << d.first << ", " << d.second << ")";
  }
  
  template < class C > debug & operator <<(rge<C> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
  #else 
  template < class C > debug & operator <<(const C &x) {
    return *this;
  }
  #endif
};

#define imiet(t, i, j) "[" << #t ": " << tblAux(t, i, j) << "] "
#define imie(x) "[" << #x ": " << x << "] "
#endif // __MAKRA_H_

const int N = 2000;
int par[N + 7];
vector <int> G[N + 7];
int a[N + 7];
int n;
vector <int> wynik;

bool juz[N + 7];
bool biere[N + 7];
bool zaj[N + 7];
int gdzie[N + 7];
int h[N + 7];

void operacja(int v) {
  wynik.EB(v);
  int x = par[v];
  int val = a[v];
  while (x > 0) {
    int nval = a[x];
    a[x] = val;
    val = nval;
    x = par[x];
  }
  a[v] = val;
}

void dfs0(int v) {
  vector <int> ng;
  for (int u : G[v]) if (!juz[u]) ng.EB(u);
  G[v].swap(ng);
  
  h[v] = h[par[v]] + 1;
  
  if (G[v].empty()) {
    biere[v] = true;
    gdzie[v] = v;
  }
  else {
    for (int u : G[v]) dfs0(u);
    if (SZ(G[v]) == 1 && biere[G[v].back()]) {
      biere[v] = true; 
      gdzie[v] = gdzie[G[v].back()];
    }
  } 
}

int znajdzGdzie() {
  int v = 1;
  while (1) {
    int nv = 0;
    for (int u : G[v]) if (!zaj[u]) nv = u;
    if (nv == 0) break;
    v = nv;
  } 
  
  return v;
}

void cisnij() {
  FOR (i, 1, n) {
    biere[i] = false;
    zaj[i] = juz[i];
  }
  
  dfs0(1);
  
  int ile = 0;
  FOR (i, 1, n) if (biere[i]) ile++;
  
  while (ile > 0) {
    if (biere[a[1]]) {
      ile--;
      int v = gdzie[a[1]];
      while (juz[v] && h[a[1]] < h[a[v]]) v = par[v];
      operacja(v);
      while (juz[v]) v = par[v];
      juz[v] = zaj[v] = true;
    }
    else {
      int v = znajdzGdzie();
      operacja(v);
      zaj[v] = true;
    } 
  }
  
  FOR (i, 1, n) {
    if (biere[i]) {
      assert (a[i] == i);
      assert (juz[i]);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  FOR (i, 2, n) {
    cin >> par[i];
    par[i]++;
    G[par[i]].EB(i);
  }
  FOR (i, 1, n) {
    cin >> a[i];
    a[i]++;
  }
  
  while (!juz[1]) cisnij();
  
  FOR (i, 1, n) assert (a[i] == i);
  
  cout << SZ(wynik) << endl;
  for (int a : wynik) cout << a - 1 << '\n';
  return 0;
}

Measured memory_bytes: 768