#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& p) {
  os << "[ ";
  for (T x : p) os << x << " ";
  os << "]" << endl;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& p) {
  os << "{ ";
  for (T x : p) os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& p) {
  os << "{ ";
  for (T x : p) os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream& operator<<(ostream& os, const map<Tk, Tv>& p) {
  os << "{ ";
  for (pair<Tk, Tv> x : p) os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream& operator<<(ostream& os, const pair<Tk, Tv>& p) {
  os << "{" << p.first << ',' << p.second << "}";
  return os;
}
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
long long power(long long a, long long n) {
  long long p = 1;
  while (n > 0) {
    if (n & 1) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
long long power(long long a, long long n, long long mod) {
  long long p = 1;
  while (n > 0) {
    if (n & 1) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
const int N = 100005;
vector<int> v[N], rev[N];
vector<int> aux, ma;
int a[N];
int ans = 0;
int vi[N];
void dfs(int s, int dis, int root) {
  if (rev[s].size() == 0 || v[s].size() > 1 || (s != root && a[s])) {
    if (rev[s].size() == 0 || (s != root && a[s])) aux.push_back(s);
    if (ans < aux.size()) {
      ma.clear();
      for (auto i : aux) ma.push_back(i);
      ans = aux.size();
    }
    if (rev[s].size() == 0 || (s != root && a[s])) aux.pop_back();
    return;
  }
  vi[s] = 1;
  aux.push_back(s);
  for (auto i : rev[s]) dfs(i, dis + 1, root);
  aux.pop_back();
}
int main() {
  int i, j;
  int n;
  scanf("%d", &n);
  for (i = 1; i < n + 1; i++) scanf("%d", &a[i]);
  for (i = 1; i < n + 1; i++) {
    int x;
    scanf("%d", &x);
    if (x == 0) continue;
    v[x].push_back(i);
    rev[i].push_back(x);
  }
  for (i = 1; i < n + 1; i++)
    if (a[i] == 1 && vi[i] == 0) dfs(i, 1, i);
  printf("%d\n", ans);
  for (i = (int)ma.size() - 1; i >= 0; i--) printf("%d ", ma[i]);
  puts("");
  return 0;
}
