#include <bits/stdc++.h>
using namespace std;
const int maxn = (1e5) + 10;
int type[maxn], go[maxn], from[maxn];
void dfs(int k) {
  if (from[k] != 0 && go[from[k]] == 1) dfs(from[k]);
  printf("%d ", k);
}
int main() {
  int n, i, j, k, a, maxl, mark;
  cin >> n;
  for (i = 1; i <= n; i++) scanf("%d", &type[i]);
  for (i = 1; i <= n; i++) {
    scanf("%d", &from[i]);
    go[from[i]]++;
  }
  maxl = 0;
  for (i = 1; i <= n; i++)
    if (type[i]) {
      int l = 1;
      k = i;
      while (from[k] != 0 && go[from[k]] == 1) {
        k = from[k];
        l++;
      }
      if (l > maxl) {
        maxl = l;
        mark = i;
      }
    }
  cout << maxl << endl;
  dfs(mark);
  cout << endl;
  return 0;
}
