#include <bits/stdc++.h>
using namespace std;
int toInt(string s) {
  int r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
long long toInt64(string s) {
  long long r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
double toDouble(string s) {
  double r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
string toString(long long n) {
  string s, s1;
  while (n / 10 > 0) {
    s += (char)((n % 10) + 48);
    n /= 10;
  }
  s += (char)((n % 10) + 48);
  n /= 10;
  s1 = s;
  for (long long i = 0; i < s.length(); i++) s1[(s.length() - 1) - i] = s[i];
  return s1;
}
bool isUpperCase(char c) { return c >= 'A' && c <= 'Z'; }
bool isLowerCase(char c) { return c >= 'a' && c <= 'z'; }
bool isLetter(char c) { return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'; }
bool isDigit(char c) { return c >= '0' && c <= '9'; }
char toLowerCase(char c) { return (isUpperCase(c)) ? (c + 32) : c; }
char toUpperCase(char c) { return (isLowerCase(c)) ? (c - 32) : c; }
long long gcd(long long a, long long b) {
  if (!a) return b;
  return gcd(b % a, a);
}
bool grid[100001];
int masir[100001];
int yek[100001], Yek = 0;
short int how[100001];
vector<int> BEST;
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a == 1)
      grid[i] = true;
    else
      grid[i] = false;
    if (grid[i]) yek[Yek++] = i;
  }
  for (long long i = 0; i < n; i++) {
    cin >> masir[i];
    masir[i]--;
    if (how[masir[i]] != 2) how[masir[i]]++;
  }
  int Max = 0;
  for (long long i = 0; i < Yek; i++) {
    int Masir[100001], count = 0, dest = yek[i];
    Masir[count++] = dest;
    while (masir[dest] != -1) {
      dest = masir[dest];
      if (grid[dest] || how[dest] >= 2) break;
      Masir[count++] = dest;
    }
    if (Max < count) {
      BEST.clear();
      for (int j = count - 1; j >= 0; j--) BEST.push_back(Masir[j]);
      Max = count;
    }
  }
  int SIZE = BEST.size();
  cout << SIZE << endl;
  for (long long i = 0; i < SIZE - 1; i++) cout << BEST[i] + 1 << ' ';
  cout << BEST[SIZE - 1] + 1 << endl;
}
