#include <bits/stdc++.h>
using namespace std;
vector<int> b;
int tp[100005], ss[100005], cnt[100005], pre[100005], ans, ret[100005];
void solve(int u) {
  int ls = 0, first = 1;
  while (1) {
    if (u == -1 || u == 0 || cnt[u] > 1 || (tp[u] && !first)) break;
    if (first == 1) first = 0;
    ss[ls++] = u;
    u = pre[u];
  }
  if (ls > ans) {
    ans = ls;
    for (int i = 0; i < ls; i++) ret[i] = ss[i];
  }
}
int main() {
  int n, x;
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) pre[i] = -1;
  for (int i = 1; i <= n; i++) scanf("%d", &tp[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    pre[i] = x;
    cnt[x]++;
    if (tp[i] == 1) b.push_back(i);
  }
  int siz = b.size();
  ans = 0;
  for (int i = 0; i < siz; i++) {
    int v = b[i];
    solve(v);
  }
  printf("%d\n", ans);
  for (int i = ans - 1; i >= 0; i--) {
    printf("%d", ret[i]);
    if (i) printf(" ");
  }
  puts("");
  return 0;
}
