#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
T min(T a, U b) {
  if (a < (U)b) return a;
  return b;
}
template <class T, class U>
T max(T a, U b) {
  if (a < (U)b) return b;
  return a;
}
const int INF = 1000000000;
const long long MOD = 1000000009;
const double eps = 1e-9;
vector<vector<int> > g;
vector<char> used;
vector<int> p;
vector<int> h;
vector<int> sz;
void dfs(int v, int curh = 0, int par = -1) {
  used[v] = true;
  h[v] = curh;
  p[v] = par;
  for (int i = 0; i < g[v].size(); ++i) {
    if (!used[g[v][i]] && sz[g[v][i]] <= 1) dfs(g[v][i], curh + 1, v);
  }
}
int main() {
  int N;
  cin >> N;
  g.resize(N);
  used.assign(N, false);
  h.assign(N, 0);
  p.assign(N, -1);
  sz.assign(N, 0);
  int type[N];
  for (int i = 0; i < N; ++i) {
    cin >> type[i];
  }
  int curp[N];
  for (int i = 0; i < N; ++i) {
    cin >> curp[i];
    curp[i]--;
    if (curp[i] >= 0) {
      sz[curp[i]]++;
      g[i].push_back(curp[i]);
    }
  }
  for (int i = 0; i < N; ++i) {
    if (type[i]) {
      dfs(i);
    }
  }
  int maxh = -1, maxid = -1;
  for (int i = 0; i < N; ++i) {
    if (h[i] > maxh && used[i]) {
      maxh = h[i];
      maxid = i;
    }
  }
  vector<int> res;
  for (int i = maxid; i != -1; i = p[i]) {
    res.push_back(i + 1);
  }
  cout << res.size() << endl;
  for (int i = 0; i < res.size(); ++i) cout << res[i] << " ";
  return 0;
}
