#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, y, num, max = 0;
  cin >> n;
  vector<int> type(n + 1, 0);
  vector<int> deg(n + 1, 0);
  vector<int> prev(n + 1, 0);
  for (int i = 0; i < (int)n; i++) {
    cin >> type[i + 1];
  }
  for (int i = 0; i < (int)n; i++) {
    cin >> num;
    deg[num]++;
    prev[i + 1] = num;
  }
  vector<int> M, ans;
  for (int i = (int)1; i <= (int)n; i++) {
    if (type[i] == 1) {
      y = i;
      M.clear();
      M.push_back(i);
      while (1) {
        x = prev[y];
        if (deg[x] > 1 || type[x] || !x) break;
        M.push_back(x);
        y = x;
      }
      if (M.size() > max) {
        max = M.size();
        ans = M;
      }
    }
  }
  cout << ans.size() << '\n';
  reverse((ans).begin(), (ans).end());
  for (int i = 0; i < (int)ans.size(); i++) {
    if (i) cout << " ";
    cout << ans[i];
  }
  cout << '\n';
  return 0;
}
