#include <bits/stdc++.h>
using namespace std;
int type[100005];
int lead[100005], degree[100005];
vector<int> go(int n) {
  vector<int> v;
  while (true) {
    if (degree[n] > 1) {
      return v;
    } else if (lead[n] == -1) {
      v.push_back(n);
      return v;
    }
    v.push_back(n);
    n = lead[n];
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> type[i];
  for (int i = 0; i < n; ++i) {
    cin >> lead[i];
    --lead[i];
    if (lead[i] >= 0) ++degree[lead[i]];
  }
  vector<int> best;
  for (int i = 0; i < n; ++i) {
    vector<int> cur;
    if (type[i]) cur = go(i);
    if (cur.size() > best.size()) best = cur;
  }
  reverse(best.begin(), best.end());
  cout << best.size() << '\n';
  for (int i = 0; i < best.size(); ++i) {
    cout << best[i] + 1 << ' ';
  }
  cout << endl;
}
