#include <bits/stdc++.h>
using namespace std;
stringstream ss;
long long mod = 1000000007LL;
int t[100010];
int in[100010];
vector<int> adj[100010];
int main() {
  int n, x;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> t[i];
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    in[x]++;
    if (x) adj[i].push_back(x);
  }
  int mx = 0;
  vector<int> v;
  for (int i = 1; i <= n; i++)
    if (t[i]) {
      stack<int> st;
      st.push(i);
      int now = i;
      while (adj[now].size()) {
        now = adj[now][0];
        if (in[now] != 1) break;
        st.push(now);
      }
      if (st.size() > mx) {
        mx = st.size();
        v.clear();
        while (st.size()) v.push_back(st.top()), st.pop();
      }
    }
  cout << mx << endl;
  for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
  return 0;
}
