#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> adj[100003];
bool vis[100003] = {false};
bool hotel[100003] = {false};
int dp[100003] = {0};
int DFS(int node) {
  if (vis[node]) return dp[node];
  vis[node] = true;
  if (hotel[node]) {
    return dp[node] = 1;
  }
  if (int(adj[node].size()) > 1 || int(adj[node].size()) == 0)
    return dp[node] = -1;
  int temp = DFS(adj[node][0]);
  if (temp == -1) return dp[node] = -1;
  return dp[node] = temp + 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, a;
  cin >> n;
  for (__typeof(n) i = 1; i <= n; i++) {
    cin >> a;
    if (a) hotel[i] = true;
  }
  for (__typeof(n) i = 1; i <= n; i++) {
    cin >> a;
    if (a) adj[a].push_back(i);
  }
  int result = 0, start = 0;
  for (__typeof(n) i = 1; i <= n; i++) {
    if (vis[i]) continue;
    int temp = DFS(i);
    if (temp > result) {
      start = i;
    }
    result = max(result, temp);
  }
  cout << result << '\n';
  while (start != 0) {
    cout << start << ' ';
    if (adj[start].empty()) break;
    start = adj[start][0];
  }
  return 0;
}
