#include <bits/stdc++.h>
using namespace std;
int pre[100005];
int deg[100005];
bool flag[100005];
int n;
int deep;
void output(int x) {
  if (deg[pre[x]] == 1) output(pre[x]);
  printf("%d ", x);
}
int main() {
  int x;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> flag[i];
  for (int i = 1; i <= n; i++) {
    cin >> x;
    pre[i] = x;
    deg[x]++;
  }
  deg[0] = 0;
  int len = 0;
  int best = -1;
  for (int i = 1; i <= n; i++)
    if (flag[i]) {
      deep = 1;
      x = i;
      while (deg[pre[x]] == 1) {
        deep++;
        x = pre[x];
      }
      if (deep > len) {
        best = i;
        len = deep;
      }
    }
  printf("%d\n", len);
  output(best);
  return 0;
}
