#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> type(n);
  for (int i = ((0)); i <= (int)(((n)-1)); i += (1)) cin >> type[i];
  vector<int> a(n);
  for (int i = ((0)); i <= (int)(((n)-1)); i += (1)) cin >> a[i];
  vector<int> c(n + 1);
  for (int i = ((0)); i <= (int)(((n)-1)); i += (1)) ++c[a[i]];
  vector<int> max_path;
  for (int i = ((0)); i <= (int)(((n)-1)); i += (1)) {
    if (type[i] == 1) {
      vector<int> path;
      int j = i;
      path.push_back(j + 1);
      while (a[j] > 0 && c[a[j]] == 1) {
        j = a[j] - 1;
        path.push_back(j + 1);
      }
      if (path.size() > max_path.size()) max_path = path;
    }
  }
  reverse(max_path.begin(), max_path.end());
  cout << max_path.size() << endl;
  for (int i = ((0)); i <= (int)(((max_path.size()) - 1)); i += (1))
    cout << max_path[i] << " ";
  return 0;
}
