#include <bits/stdc++.h>
using namespace std;
struct Vertex {
  bool hotel;
  int deg;
  int from;
};
int n;
vector<Vertex> graph;
vector<int> curPath;
void dfs(int pos) {
  const Vertex& v = graph[pos];
  if (v.deg > 1) return;
  curPath.push_back(pos);
  if (v.from != -1) dfs(v.from);
}
int main() {
  cin >> n;
  graph.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> graph[i].hotel;
  }
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (x) graph[x - 1].deg++;
    graph[i].from = x - 1;
  }
  vector<int> maxPath;
  for (int i = 0; i < n; ++i) {
    if (graph[i].hotel) {
      curPath.clear();
      curPath.push_back(i);
      if (graph[i].from != -1) dfs(graph[i].from);
      maxPath = curPath.size() > maxPath.size() ? curPath : maxPath;
    }
  }
  reverse(maxPath.begin(), maxPath.end());
  cout << maxPath.size() << '\n';
  for (int i = 0; i < maxPath.size(); ++i) {
    if (i) cout << " ";
    cout << maxPath[i] + 1;
  }
  return 0;
}
