#include <bits/stdc++.h>
using namespace std;
int n, t[100010], a[100010], in[100010];
vector<int> cur, ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", t + i);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    ++in[a[i]];
  }
  for (int i = 1; i <= n; ++i) {
    if (!t[i]) continue;
    int j = i, flag = 1;
    while (flag && j) {
      cur.push_back(j);
      j = a[j];
      if (in[j] > 1) flag = 0;
    }
    if (ans.size() < cur.size()) ans.swap(cur);
    cur.clear();
  }
  reverse(ans.begin(), ans.end());
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]);
  return 0;
}
