#include <bits/stdc++.h>
using namespace std;
int ish[100006];
bool vis[100007];
int depp[100007];
vector<int> v[100007];
int dfs(int vt, int ans) {
  if (ish[vt]) return ans;
  if (vis[vt] && depp[vt] > 0)
    return depp[vt] + ans - 1;
  else if (vis[vt] && depp[vt] == 0)
    return 0;
  if (v[vt].size() == 0 || v[vt].size() > 1) return 0;
  vis[vt] = 1;
  return dfs(v[vt][0], ans + 1);
}
int main() {
  int n;
  int i;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> ish[i];
  int tmp;
  for (i = 1; i <= n; i++) {
    cin >> tmp;
    if (tmp == 0)
      continue;
    else
      v[tmp].push_back(i);
  }
  int ans = 0, ver = 0;
  for (i = 1; i <= n; i++) {
    if (!ish[i])
      tmp = dfs(i, 1);
    else
      tmp = 1;
    depp[i] = tmp;
    if (ans < tmp) {
      ver = i;
      ans = tmp;
    }
  }
  cout << ans << endl;
  while (1) {
    cout << ver << " ";
    if (ish[ver]) break;
    ver = v[ver][0];
  }
}
