#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 1e5 + 10;
vector<int> G[maxn];
int c[maxn], out[maxn], d[maxn], fa[maxn];
int t, ss, tt, mlen;
void bfs(int x) {
  queue<int> q;
  q.push(x);
  memset(d, -1, sizeof(d));
  d[x] = mlen = 0;
  fa[x] = -1;
  t = x;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (d[v] != -1 || c[v] == -1) continue;
      if (out[v] != 1) continue;
      d[v] = d[u] + 1;
      fa[v] = u;
      q.push(v);
      if (d[v] > mlen) {
        mlen = d[v];
        t = v;
      }
    }
  }
}
int main() {
  memset(out, 0, sizeof(out));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t);
    if (t) {
      G[i].push_back(t);
      G[t].push_back(i);
      out[t]++;
    }
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    if (c[i]) {
      bfs(i);
      if (d[t] > ans) {
        ans = d[t];
        ss = i;
        tt = t;
      }
    }
  }
  bfs(ss);
  printf("%d\n", ans + 1);
  while (tt != -1) {
    printf("%d ", tt);
    tt = fa[tt];
  }
  return 0;
}
