#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int type[N];
int pre[N];
int c[N];
long long int n;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> type[i];
  for (int i = 0; i < n; i++) {
    cin >> pre[i];
    pre[i]--;
    if (pre[i] != -1) c[pre[i]]++;
  }
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    if (type[i] == 1) {
      int curv = i;
      vector<int> cur;
      while (pre[curv] != -1 && c[pre[curv]] <= 1) {
        cur.push_back(curv);
        curv = pre[curv];
      }
      cur.push_back(curv);
      if (ans.size() < cur.size()) ans = cur;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] + 1 << " ";
  }
  return 0;
}
