#include <bits/stdc++.h>
using namespace std;
int pathlead[100010], mx, pmx, n, sn;
vector<int> adj[100010];
vector<int> res;
int type[100010];
bool col[100010];
void dfs(int u, int depth) {
  col[u] = true;
  if (depth > mx) mx = depth;
  for (int i = 0; i < ((int)adj[u].size()); i++) {
    int v = adj[u][i];
    if (col[v] == false && type[v] == 0 && pathlead[v] < 2) {
      dfs(v, depth + 1);
    }
  }
  return;
}
void find_res(int u) {
  col[u] = true;
  res.push_back(u);
  for (int i = 0; i < ((int)adj[u].size()); i++) {
    int v = adj[u][i];
    if (col[v] == false && type[v] == 0 && pathlead[v] < 2) {
      find_res(v);
    }
  }
  return;
}
int main() {
  int a;
  pmx = mx = -1 << 30;
  cin >> n;
  memset(pathlead, 0, sizeof(pathlead));
  for (int i = 1; i <= n; i++) cin >> type[i];
  for (int i = 1; i <= n; i++) {
    cin >> a;
    if (a == 0) continue;
    pathlead[a]++;
    adj[i].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (type[i] == 1 && pathlead[i] == 0) {
      mx = -1 << 30;
      memset(col, false, sizeof(col));
      dfs(i, 1);
      if (mx > pmx) {
        pmx = mx;
        sn = i;
      }
    }
  }
  cout << pmx << endl;
  memset(col, false, sizeof(col));
  find_res(sn);
  for (int i = ((int)res.size()) - 1; i >= 0; i--) {
    cout << res[i] << ' ';
  }
  return 0;
}
