#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int MAX = 2010;
int wst[MAX],il[MAX],ok[MAX],a[MAX],p[MAX],syn[MAX];
int n;
void dfs(int v){
  ok[v] = 1;
  debug("ok",v);
  if(p[v] != 0 && il[p[v]] == 1){
    dfs(p[v]);
    syn[p[v]] = v;
  }
}
vector<int> out;
void rusz(int x){
   debug("rusz",x);
  out.PB(x);
  int pom = a[0];
  bool cz = 1;
  while(x != 0){
    swap(pom,a[x]);
    if(wst[x] == 0 && cz){
      if(il[x] == 0){
        wst[x] = 1;
        il[p[x]]--;
      }
      cz = 0;
    }
    x = p[x];
  }
  a[0] = pom;
//   R(i,n){
//     cerr << a[i] << " ";
//   }
//   cerr << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n;
  p[0] = -1;
  R(i,n-1){
    cin >> p[i+1];
  }
  R(i,n){
    cin >> a[i];
  }
  vector<int> todo;
  R(i,n)if(i)todo.PB(i);
  while(SZ(todo)){
    debug(todo);
    for(int ak:todo){
      wst[ak] = 0;
      il[ak] = 0;
      ok[ak] = 0;
      syn[ak] = -1;
    }
    for(int ak:todo){
      il[p[ak]]++;
    }
    for(int ak:todo){
      if(il[ak] == 0){
        dfs(ak);
      }
    }
    int gd = SZ(todo)-1;
    while(1){
      if(ok[a[0]]){
        int ak = a[0];
        debug(ak);
        while(syn[ak] != -1 && (wst[syn[ak]] == 0 || !ok[a[syn[ak]]] || a[syn[ak]] < a[0])){
          ak = syn[ak];
          debug(ak);
        }
        rusz(ak);
      }else{
        while(gd >= 0 && wst[todo[gd]]){
          gd--;
        }
        if(gd != -1){
          rusz(todo[gd]);
        }else
          break;
      }
    }
    vector<int> td;
    for(int el:todo){
      if(!ok[el])
        td.PB(el);
      else
        assert(a[el] == el);
    }
    swap(td,todo);
  }
  cout << SZ(out) << "\n";
  for(int el:out){
    cout << el << "\n";
  }
#ifdef LOCAL
  R(i,n){
    assert(i == a[i]);
  }
#endif
}
