import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class Main {
  private static BufferedReader br;
  private static StringTokenizer st;
  private static PrintWriter pw;

  public static void main(String[] args) throws IOException {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    //int qq = 1;
    int qq = Integer.MAX_VALUE;
    //int qq = readInt();
    for(int casenum = 1; casenum <= qq; casenum++)  {
      int n = readInt();
      int[] type = new int[n];
      for(int i = 0; i < n; i++) {
        type[i] = readInt();
      }
      int[] parent = new int[n];
      int[] numChild = new int[n];
      for(int i = 0; i < n; i++) {
        parent[i] = readInt()-1;
        if(parent[i] >= 0) {
          numChild[parent[i]]++;
        }
      }
      LinkedList<Integer> ret = new LinkedList<Integer>();
      for(int i = 0; i < n; i++) {
        if(type[i] == 0) continue;
        LinkedList<Integer> curr = new LinkedList<Integer>();
        curr.addFirst(i);
        int now = i;
        while(parent[now] >= 0 && type[parent[now]] == 0 && numChild[parent[now]] == 1) {
          curr.addFirst(parent[now]);
          parent[now] = now;
        }
        if(curr.size() > ret.size()) {
          ret = curr;
        }
      }
      pw.println(ret.size());
      StringBuilder sb = new StringBuilder();
      for(int out: ret) {
        sb.append((1+out) + " ");
      }
      pw.println(sb.toString().trim());
    }
    pw.close();
  }

  public static String parse(long curr) {
    String ret = "";
    for(int i = 0; i < 4; i++) {
      ret = (curr&255) + ret;
      ret = "." + ret;
      curr >>= 8;
    }
    return ret.substring(1);
  }

  public static long parse(String str) {
    long ret = 0;
    StringTokenizer su = new StringTokenizer(str, ".");
    while(su.hasMoreTokens()) {
      ret <<= 8;
      ret |= Integer.parseInt(su.nextToken());
    }
    return ret;
  }

  private static void exitImmediately() {
    pw.close();
    System.exit(0);
  }

  private static long readLong() throws IOException {
    return Long.parseLong(nextToken());
  }

  private static double readDouble() throws IOException {
    return Double.parseDouble(nextToken());
  }

  private static int readInt() throws IOException {
    return Integer.parseInt(nextToken());
  }

  private static String nextToken() throws IOException  {
    while(st == null || !st.hasMoreTokens())  {
      if(!br.ready()) {
        exitImmediately();
      }
      st = new StringTokenizer(br.readLine().trim());
    }
    return st.nextToken();
  }
}


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class Main {
  private static BufferedReader br;
  private static StringTokenizer st;
  private static PrintWriter pw;

  public static void main(String[] args) throws IOException {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    //int qq = 1;
    int qq = Integer.MAX_VALUE;
    //int qq = readInt();
    for(int casenum = 1; casenum <= qq; casenum++)  {
      int n = readInt();
      int[] type = new int[n];
      for(int i = 0; i < n; i++) {
        type[i] = readInt();
      }
      int[] parent = new int[n];
      int[] numChild = new int[n];
      for(int i = 0; i < n; i++) {
        parent[i] = readInt()-1;
        if(parent[i] >= 0) {
          numChild[parent[i]]++;
        }
      }
      LinkedList<Integer> ret = new LinkedList<Integer>();
      for(int i = 0; i < n; i++) {
        if(type[i] == 0) continue;
        LinkedList<Integer> curr = new LinkedList<Integer>();
        curr.addFirst(i);
        int now = i;
        while(parent[now] >= 0 && type[parent[now]] == 0 && numChild[parent[now]] == 0) {
          curr.addFirst(parent[now]);
          parent[now] = now;
        }
        if(curr.size() > ret.size()) {
          ret = curr;
        }
      }
      pw.println(ret.size());
      StringBuilder sb = new StringBuilder();
      for(int out: ret) {
        sb.append((1+out) + " ");
      }
      pw.println(sb.toString().trim());
    }
    pw.close();
  }

  public static String parse(long curr) {
    String ret = "";
    for(int i = 0; i < 4; i++) {
      ret = (curr&255) + ret;
      ret = "." + ret;
      curr >>= 8;
    }
    return ret.substring(1);
  }

  public static long parse(String str) {
    long ret = 0;
    StringTokenizer su = new StringTokenizer(str, ".");
    while(su.hasMoreTokens()) {
      ret <<= 8;
      ret |= Integer.parseInt(su.nextToken());
    }
    return ret;
  }

  private static void exitImmediately() {
    pw.close();
    System.exit(0);
  }

  private static long readLong() throws IOException {
    return Long.parseLong(nextToken());
  }

  private static double readDouble() throws IOException {
    return Double.parseDouble(nextToken());
  }

  private static int readInt() throws IOException {
    return Integer.parseInt(nextToken());
  }

  private static String nextToken() throws IOException  {
    while(st == null || !st.hasMoreTokens())  {
      if(!br.ready()) {
        exitImmediately();
      }
      st = new StringTokenizer(br.readLine().trim());
    }
    return st.nextToken();
  }
}


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class Main {
  private static BufferedReader br;
  private static StringTokenizer st;
  private static PrintWriter pw;

  public static void main(String[] args) throws IOException {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    //int qq = 1;
    int qq = Integer.MAX_VALUE;
    //int qq = readInt();
    for(int casenum = 1; casenum <= qq; casenum++)  {
      int n = readInt();
      int[] type = new int[n];
      for(int i = 0; i < n; i++) {
        type[i] = readInt();
      }
      int[] parent = new int[n];
      int[] numChild = new int[n];
      for(int i = 0; i < n; i++) {
        parent[i] = readInt()-1;
        if(parent[i] >= 0) {
          numChild[parent[i]]++;
        }
      }
      LinkedList<Integer> ret = new LinkedList<Integer>();
      for(int i = 0; i < n; i++) {
        if(type[i] == 0) continue;
        LinkedList<Integer> curr = new LinkedList<Integer>();
        curr.addFirst(i);
        int now = i;
        while(parent[now] >= 0 && type[parent[now]] == 0 && numChild[parent[now]] == 1) {
          curr.addFirst(parent[now]);
          now = parent[now];
        }
        if(curr.size() > ret.size()) {
          ret = curr;
        }
      }
      pw.println(ret.size());
      StringBuilder sb = new StringBuilder();
      for(int out: ret) {
        sb.append((1+out) + " ");
      }
      pw.println(sb.toString().trim());
    }
    pw.close();
  }

  public static String parse(long curr) {
    String ret = "";
    for(int i = 0; i < 4; i++) {
      ret = (curr&255) + ret;
      ret = "." + ret;
      curr >>= 8;
    }
    return ret.substring(1);
  }

  public static long parse(String str) {
    long ret = 0;
    StringTokenizer su = new StringTokenizer(str, ".");
    while(su.hasMoreTokens()) {
      ret <<= 8;
      ret |= Integer.parseInt(su.nextToken());
    }
    return ret;
  }

  private static void exitImmediately() {
    pw.close();
    System.exit(0);
  }

  private static long readLong() throws IOException {
    return Long.parseLong(nextToken());
  }

  private static double readDouble() throws IOException {
    return Double.parseDouble(nextToken());
  }

  private static int readInt() throws IOException {
    return Integer.parseInt(nextToken());
  }

  private static String nextToken() throws IOException  {
    while(st == null || !st.hasMoreTokens())  {
      if(!br.ready()) {
        exitImmediately();
      }
      st = new StringTokenizer(br.readLine().trim());
    }
    return st.nextToken();
  }
}


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class Main {
  private static BufferedReader br;
  private static StringTokenizer st;
  private static PrintWriter pw;

  public static void main(String[] args) throws IOException {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    //int qq = 1;
    int qq = Integer.MAX_VALUE;
    //int qq = readInt();
    for(int casenum = 1; casenum <= qq; casenum++)  {
      int n = readInt();
      int[] type = new int[n];
      for(int i = 0; i < n; i++) {
        type[i] = readInt();
      }
      int[] parent = new int[n];
      int[] numChild = new int[n];
      for(int i = 0; i < n; i++) {
        parent[i] = readInt()-1;
        if(parent[i] >= 0) {
          numChild[parent[i]]++;
        }
      }
      LinkedList<Integer> ret = new LinkedList<Integer>();
      for(int i = 0; i < n; i++) {
        if(type[i] == 0) continue;
        LinkedList<Integer> curr = new LinkedList<Integer>();
        curr.addFirst(i);
        int now = i;
        while(parent[now] >= 0 && type[parent[now]] == 0 && numChild[parent[now]] == 0) {
          curr.addFirst(parent[now]);
          parent[now] = now;
        }
        if(curr.size() > ret.size()) {
          ret = curr;
        }
      }
      pw.println(ret.size());
      StringBuilder sb = new StringBuilder();
      for(int out: ret) {
        sb.append(out + " ");
      }
      pw.println(sb.toString().trim());
    }
    pw.close();
  }

  public static String parse(long curr) {
    String ret = "";
    for(int i = 0; i < 4; i++) {
      ret = (curr&255) + ret;
      ret = "." + ret;
      curr >>= 8;
    }
    return ret.substring(1);
  }

  public static long parse(String str) {
    long ret = 0;
    StringTokenizer su = new StringTokenizer(str, ".");
    while(su.hasMoreTokens()) {
      ret <<= 8;
      ret |= Integer.parseInt(su.nextToken());
    }
    return ret;
  }

  private static void exitImmediately() {
    pw.close();
    System.exit(0);
  }

  private static long readLong() throws IOException {
    return Long.parseLong(nextToken());
  }

  private static double readDouble() throws IOException {
    return Double.parseDouble(nextToken());
  }

  private static int readInt() throws IOException {
    return Integer.parseInt(nextToken());
  }

  private static String nextToken() throws IOException  {
    while(st == null || !st.hasMoreTokens())  {
      if(!br.ready()) {
        exitImmediately();
      }
      st = new StringTokenizer(br.readLine().trim());
    }
    return st.nextToken();
  }
}


import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

public class CFB {

	private static final int IN_BUFFER_SIZE = 1 << 16;
	private static final int OUT_BUFFER_SIZE = 1 << 16;
	private byte[] input = new byte[IN_BUFFER_SIZE];
	private int ix = IN_BUFFER_SIZE;
	private int bytesRead = ix;
	private byte[] output = new byte[OUT_BUFFER_SIZE];
	private int ox = 0;

	private void readMore() {
		try {
			bytesRead = System.in.read(input, 0, IN_BUFFER_SIZE);
			if (bytesRead <= 0)
				throw new RuntimeException();
			ix = 0;
		} catch (IOException e) {
			throw new RuntimeException();
		}
	}

	private void flushOut() {
		System.out.write(output, 0, ox);
		ox = 0;
	}

	private void append(char c) {
		if (ox == OUT_BUFFER_SIZE)
			flushOut();
		output[ox++] = (byte) c;
	}

	private int nextInt() {
		skipSpaces();
		int ret = 0;
		if (ix == bytesRead) {
			readMore();
		}
		int sign = 1;
		if (input[ix] == '-') {
			sign = -1;
			ix++;
		}
		while (true) {
			if (ix == bytesRead) {
				try {
					readMore();
				} catch (RuntimeException e) {
					return ret;
				}
			}
			if (input[ix] < '0') {
				break;
			}
			ret *= 10;
			ret += input[ix++] - '0';
		}
		return sign * ret;
	}

	int cntSize;

	private void skipSpaces() {
		while (true) {
			if (ix == bytesRead) {
				readMore();
			}
			if (input[ix] > ' ')
				break;
			ix++;
		}
	}

	private char[] nn = new char[32];

	private void printInt(int n) {
		if (n == 0) {
			append('0');
		} else {
			if (n < 0) {
				append('-');
				n = -n;
			}
			int kk = 0;
			while (n > 0) {
				nn[kk++] = (char) (n % 10 + '0');
				n /= 10;
			}
			for (int i = kk - 1; i >= 0; i--) {
				append(nn[i]);
			}
		}
	}

	public static void main(String[] args) throws IOException {
		new CFB().work();
	}

	class State {
		int k, d;
		State prev;

		public State(int k, int d, State prev) {
			super();
			this.k = k;
			this.d = d;
			this.prev = prev;
		}
	}

	private void work() {
		int n = nextInt();
		Queue<State> q = new LinkedList<State>();
		for (int i = 1; i <= n; i++) {
			if (nextInt() != 0)
				q.add(new State(i, 1, null));
		}
		int[] a = new int[n + 1];
		int[] count = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			a[i] = nextInt();
			count[a[i]]++;
		}

		State res = null;
		while (!q.isEmpty()) {
			State s = q.poll();
			if (res == null || res.d < s.d) {
				res = s;
			}
			if (a[s.k] > 0 && count[a[s.k]] == 1) {
				q.add(new State(a[s.k], s.d + 1, s));
			}
		}

		printInt(res.d);
		append('\n');

		print(res);
		append('\n');

		if (ox > 0)
			flushOut();
		System.out.close();
	}

	private void print(State s) {
		if (s == null)
			return;
		printInt(s.k);
		append(' ');
		print(s.prev);
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class A {
    public static void main(String[] args)throws Exception{
        // TODO Auto-generated method stub
        new A().run();
    }

    int[] getArray(String line){
        String[] array=line.split(" ");
        int[] res=new int[array.length];
        for(int i=0;i<res.length;i++)
            res[i]=Integer.parseInt(array[i]);
        return res;
    }

    void run() throws Exception{
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

        String line=reader.readLine();
        int[] resorts=getArray(reader.readLine());
        int[] ways=getArray(reader.readLine());

        makeGraph(resorts, ways);
        List<Integer> way=getWay();
        StringBuilder s=new StringBuilder();
        for(int i:way)
            s.append(i+" ");
        s.deleteCharAt(s.length()-1);
        System.out.println(way.size());
        System.out.println(s.toString());
        System.out.flush();
        reader.close();
    }

    int n;
    List<Integer>[] graph;
    List<Integer> startPoints;
    int[] max;
    int[] vorgaenger;
    boolean[] verboten;

    int getMax(int number){

        if(max[number]==-2){
            Stack<Integer> s=new Stack<Integer>();
            s.add(number);
            while(!s.isEmpty()){
                int akt=s.pop();
                if(max[akt]==-2){
                    max[akt]=-1;
                    s.add(akt);
                    for(int next: graph[akt])
                        if(! verboten[next] && max[next]==-2)
                            s.push(next);
                }
                else{//max=-1;
                    max[akt]=0;
                    vorgaenger[akt]=-1;
                    for(int next: graph[akt])
                        if(! verboten[next]) {
                            //if(max[next]<0) System.out.println("Problem!!!");
                            int nVal=max[next]+1;
                            if(max[akt]<nVal){
                                max[akt]=nVal;
                                vorgaenger[akt]=next;
                            }
                        }
                }
            }
        }
        //System.out.println(Arrays.toString(max));
        return max[number];
    }

    List<Integer> getWay(){
        int max=-1;
        int index=-1;
        for(int start: startPoints){
            int val=getMax(start);
            if(val>max){
                max=val;
                index=start;
            }
        }
        List<Integer> way=new LinkedList<Integer>();
        //System.out.println("MAX: "+max);
        while(vorgaenger[index]!=-1){
            way.add(0,index);
            index=vorgaenger[index];
        }
        way.add(0, index);
        return way;
    }

    void printGraph(){
        int cnt=0;
        for(List<Integer> li:graph)
            System.out.println((cnt++)+":"+li);
    }
    void makeGraph(int[] resorts, int[] ways){
        n=resorts.length+1;
        startPoints=new LinkedList<Integer>();
        graph=new List[n];
        for(int i=0;i<n;graph[i++]=new LinkedList<Integer>());
        int[] howMany=new int[n];
        max=new int[n];
        vorgaenger=new int[n];
        verboten=new boolean[n];
        Arrays.fill(max, -2);
        Arrays.fill(vorgaenger, -1);

        for(int i=0;i<ways.length;i++)
            if(ways[i]!=0){
                graph[i+1].add(ways[i]);
                howMany[ways[i]]++;
            }
        //printGraph();
        for(int i=0;i<resorts.length;i++){
            if(resorts[i]==1) {
                startPoints.add(i+1);
                verboten[i+1]=true;
            }
            else if(howMany[i+1]>1) verboten[i+1]=true;
        }
        //printGraph();
        //System.out.println(Arrays.toString(verboten));

    }

}


import java.io.*;
import java.util.*;

public class B implements Runnable {

	private void solve() throws IOException {
		int n = nextInt();
		int[] type = new int[n];
		for (int i = 0; i < n; i++)
			type[i] = nextInt();
		int[] p = new int[n], e = new int[n];
		for (int i = 0; i < n; i++) {
			p[i] = nextInt() - 1;
			if (p[i] != -1) e[p[i]]++;
		}
		int len = 0, num = 0;
		for (int i = 0; i < n; i++)
			if (type[i] == 1) {
				int cur = i, cnt = 0;
				while (true) {
					cur = p[cur];
					cnt++;
					if (cur == -1 || e[cur] > 1) break;
				}
				if (cnt > len) {
					len = cnt;
					num = i;
				}
			}
		int cur = num, pt = len - 1;
		int[] ans = new int[len];
		while (true) {
			ans[pt--] = cur;
			cur = p[cur];
			if (cur == -1 || e[cur] > 1) break;
		}
		println(len);
		for (int i = 0; i < len; i++) {
			if (i != 0) print(' ');
			print(ans[i] + 1);
		}
	}

	public static void main(String[] args) {
		new B().run();
	}

	BufferedReader reader;
	StringTokenizer tokenizer;
	PrintWriter writer;

	public void run() {
		try {
			reader = new BufferedReader(new InputStreamReader(System.in));
			writer = new PrintWriter(System.out);
			solve();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(261);
		}
	}

	void halt() {
		writer.close();
		System.exit(0);
	}

	void print(Object... objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	void println(Object... objects) {
		print(objects);
		writer.println();
	}

	String nextLine() throws IOException {
		return reader.readLine();
	}

	String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}

	int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Vector;

public class CF_Solution_B {
    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok = new StringTokenizer("");

    static void init() throws FileNotFoundException{
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);

    }

    static String readString() throws IOException{
        while(!tok.hasMoreTokens()){
            try{
                tok = new StringTokenizer(in.readLine());
            }catch (Exception e){
                return null;
            }
        }
        return tok.nextToken();
    }

    static int readInt() throws IOException{
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException{
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException{
        return Double.parseDouble(readString());
    }

    public static void main(String[] args){
     run();
    }

    static long timeBegin;

	static long timeEnd;

    static void time(){
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    static long memoryTotal;

	static long memoryFree;

    static void memory(){
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB");
    }

    static void run(){
        try{
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            time();
            memory();
        }catch (Exception e){
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    static void solve() throws IOException{

        int n = readInt();
        int[] tag = new int[n];
        int [] v = new int[n];
        for (int i=0; i<n; i++) tag[i] = readInt();
        for (int j=0; j<n; j++) v[j] = readInt();

        Vector<Integer> last = new Vector<Integer>();
        for(int k=0;k<n;k++){
        	for(int p=k+1;p<n;p++){
        		if(v[k]==v[p]){
        			if(!last.contains(k)){
        				last.add(k);
        			}
        			if(!last.contains(p)){
        				last.add(p);
        			}
        		}
        	}
        }
        Vector<Integer> path = new Vector<Integer>();
        for(int i=0;i<tag.length;i++){
        	if(tag[i]==1){
        		Vector<Integer> tmppath = new Vector<Integer>();
        		int pointer = i;
        		tmppath.add(pointer+1);
        		while(!last.contains(pointer)){
        			pointer = v[pointer]-1;
        			if(pointer<0)
        				break;
        			tmppath.add(pointer+1);
        		}
        		if(tmppath.size()>path.size()){
        			path = (Vector<Integer>) tmppath.clone();
        		}
        	}
        }

        out.println(path.size());
        for(int l=path.size()-1;l>=0;l--){
        	out.print(path.get(l)+" ");
        }

    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

public class CF_Solution_B {
    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok = new StringTokenizer("");

    static void init() throws FileNotFoundException{
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);

    }

    static String readString() throws IOException{
        while(!tok.hasMoreTokens()){
            try{
                tok = new StringTokenizer(in.readLine());
            }catch (Exception e){
                return null;
            }
        }
        return tok.nextToken();
    }

    static int readInt() throws IOException{
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException{
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException{
        return Double.parseDouble(readString());
    }

    public static void main(String[] args){
     run();
    }

    static long timeBegin;

	static long timeEnd;

    static void time(){
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    static long memoryTotal;

	static long memoryFree;

    static void memory(){
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB");
    }

    static void run(){
        try{
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            time();
            memory();
        }catch (Exception e){
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    static void solve() throws IOException{

        int n = readInt();
        int[] tag = new int[n+1];
        int [] v = new int[n+1];
        for (int i=1; i<=n; i++) tag[i] = readInt();
        for (int j=1; j<=n; j++) v[j] = readInt();

        int[] num = new int[n+1];
        for(int k=1;k<=n;k++){
        	num[v[k]]++;
        }

        List<Integer> path = new ArrayList<Integer>();
        for(int q=1;q<=n;q++){
        	if(tag[q]==1){
        		List<Integer> tmp = new ArrayList<Integer>();
        		int index=q;
        		tmp.add(index);
        		index = v[index];
        		while(num[index]<=1){
        			if(index==0)
        				break;
        			tmp.add(index);
        			index = v[index];

        		}
        		if(tmp.size()>path.size()){
        			path = tmp;
        		}
        	}
        }
        out.println(path.size());
        for(int i=path.size()-1;i>=0;i--){
        	out.print(path.get(i)+" ");
        }

    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

public class CF_Solution_B {
    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok = new StringTokenizer("");

    static void init() throws FileNotFoundException{
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);

    }

    static String readString() throws IOException{
        while(!tok.hasMoreTokens()){
            try{
                tok = new StringTokenizer(in.readLine());
            }catch (Exception e){
                return null;
            }
        }
        return tok.nextToken();
    }

    static int readInt() throws IOException{
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException{
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException{
        return Double.parseDouble(readString());
    }

    public static void main(String[] args){
     run();
    }

    static long timeBegin;

	static long timeEnd;

    static void time(){
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    static long memoryTotal;

	static long memoryFree;

    static void memory(){
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB");
    }

    static void run(){
        try{
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            time();
            memory();
        }catch (Exception e){
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    static void solve() throws IOException{

        int n = readInt();
        int[] tag = new int[n+1];
        int [] v = new int[n+1];
        for (int i=1; i<=n; i++) tag[i] = readInt();
        for (int j=1; j<=n; j++) v[j] = readInt();

        int[] num = new int[n+1];
        for(int k=1;k<=n;k++){
        	num[v[k]]++;
        }

        List<Integer> path = new ArrayList<Integer>();
        for(int q=1;q<=n;q++){
        	if(tag[q]==1){
        		List<Integer> tmp = new ArrayList<Integer>();
        		int index=q;
        		tmp.add(index);
        		index = v[index];
        		while(num[index]<=1){
        			tmp.add(index);
        			index = v[index];
        			if(index==0)
        				break;
        		}
        		if(tmp.size()>path.size()){
        			path = tmp;
        		}
        	}
        }
        out.println(path.size());
        for(int i=path.size()-1;i>=0;i--){
        	out.print(path.get(i)+" ");
        }

    }
}


import java.util.*;
import java.io.*;

public class ProblemB203 implements Runnable {

	public static void main(String args[]) {
		new Thread(new ProblemB203()).run();
	}

	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;

	public void solve() throws IOException {
		int n = nextInt();
		ArrayList<Integer> ar = new ArrayList<Integer>();
		for (int i = 0; i < n; i++) {
			if (nextInt() == 1)
				ar.add(i + 1);
		}
		int[] a = new int[n + 1];
		int[] c = new int[n + 1];
		Arrays.fill(c, 0);
		c[0] = 2;
		for (int i = 0; i < n; i++) {
			a[i + 1] = nextInt();
			c[a[i + 1]]++;
		}
		int ansk = 0;
		int ans = 0;
		for (int x: ar) {
			int i = x;
			int k = 1;
			while (c[a[i]] < 2) {
				i = a[i];
				k++;
			}
			if (ansk < k) {
				ansk = k;
				ans = x;
			}
		}
		out.println(ansk);
		ArrayList<Integer> ansa = new ArrayList<Integer>();
		int i = ans;
		ansa.add(i);
		while (c[a[i]] < 2) {
			ansa.add(a[i]);
			i = a[i];
		}
		int v = ansa.size() - 1;
		while (!ansa.isEmpty()) {
			out.print(ansa.get(v) + " ");
			ansa.remove(v);
			v--;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	String nextToken() throws IOException {
		while ((in == null) || (!in.hasMoreTokens())) {
			in = new StringTokenizer(br.readLine());
		}
		return in.nextToken();
	}

	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			// br = new BufferedReader(new FileReader("input.txt"));
			// out = new PrintWriter("output.txt");
			solve();
			out.close();
		} catch (IOException e) {
		}
	}
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

/**
 * Works good for CF
 */
public class B {

	//some local config
	static boolean test = false;
	static String testDataFile = "test.txt";
	static String feedFile = "feed.txt";
	CompetitionType type = CompetitionType.CF;
	private static String ENDL = "\n";

	// solutions

	private void solve() throws Throwable {
		int n = iread();
		boolean[] hotel = new boolean[n+1];
		int[] arr = new int[n+1];

		for(int i = 1; i <= n; i++){
			if(iread() == 1){
				hotel[i] = true;
			}
		}

		for(int i = 1; i<=n; i++){
			arr[i] = iread();
		}

		int max = 0;
		int index = 0;
		for(int i = 1; i<=n; i++){
			if(hotel[i] == true){
				int x = 0;
				int p = i;
				int count = 0;
				while(x == 0 && p != 0){
					p = arr[p];
					if(hotel[p] == true){
						System.out.println("hotelbreak");
						break;
					}
					else if(count > n){
						count = 0;
						break;
					}
					else{
						count++;
					}
				}
				if(count > max){
					max = count;
					index = i;
				}
			}
		}
		if(max != 0){
			ArrayList<Integer> way = new ArrayList<Integer>();

			System.out.println(max);
			while(index != 0){
				way.add(index);
				index = arr[index];
				if(hotel[index] == true){
					break;
				}
			}

			for(int i = way.size()-1; i > 0; i--){
				System.out.print(way.get(i) + " ");
			}
			System.out.println(way.get(0));
		}
		else{
			for(int i = 1; i <= n; i++){
				if(hotel[i]){
					System.out.println(1);
					System.out.println(i);
					break;
				}
			}
		}

	}

	public int iread() throws Exception {
		return Integer.parseInt(wread());
	}

	public double dread() throws Exception {
		return Double.parseDouble(wread());
	}

	public long lread() throws Exception {
		return Long.parseLong(wread());
	}

	public String wread() throws IOException {
		StringBuilder b = new StringBuilder();
		int c;
		c = in.read();
		while (c >= 0 && c <= ' ')
			c = in.read();
		if (c < 0)
			return "";
		while (c > ' ') {
			b.append((char) c);
			c = in.read();
		}
		return b.toString();
	}

	public static void main(String[] args) throws Throwable {

		if(test){ //run all cases from testfile:
			BufferedReader testdataReader = new BufferedReader(new FileReader(testDataFile));
			String readLine = testdataReader.readLine();
			int casenr = 0;
			out: while (true) {
				BufferedWriter w = new BufferedWriter(new FileWriter(feedFile));
				if(!readLine.equals("input")){
					break;
				}
				while (true) {
					readLine = testdataReader.readLine();
					if(readLine.equals("output")){
						break;
					}
					w.write(readLine + "\n");
				}
				w.close();
				System.out.println("Answer on case "+(++casenr)+": ");

				new B().solve();
				System.out.println("Expected answer: ");

				while (true) {
					readLine = testdataReader.readLine();

					if(readLine == null){
						break out;
					}
					if(readLine.equals("input")){
						break;
					}
					System.out.println(readLine);
				}
				System.out.println("----------------");
			}
			testdataReader.close();
		} else { // run on server
			new B().solve();
		}
		out.close();
	}

	public B() throws Throwable {
		if (test) {
			in = new BufferedReader(new FileReader(new File(feedFile)));
		}
	}

	InputStreamReader inp = new InputStreamReader(System.in);
	BufferedReader in = new BufferedReader(inp);
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

	enum CompetitionType {CF, OTHER};
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

/**
 * Works good for CF
 */
public class B {

	//some local config
	static boolean test = false;
	static String testDataFile = "test.txt";
	static String feedFile = "feed.txt";
	CompetitionType type = CompetitionType.CF;
	private static String ENDL = "\n";

	// solutions

	private void solve() throws Throwable {
		int n = iread();
		boolean[] hotel = new boolean[n+1];
		int[] arr = new int[n+1];
		int[] arr2 = new int[n+1];

		for(int i = 1; i <= n; i++){
			if(iread() == 1){
				hotel[i] = true;
			}
		}

		for(int i = 1; i<=n; i++){
			arr[i] = iread();
			arr2[arr[i]]++;
		}

		for(int i = 1; i<=n; i++){
			if(arr2[i] > 1){
				arr[i] = -1;
			}
		}

		int max = 0;
		int index = 0;
		for(int i = 1; i<=n; i++){
			if(hotel[i] == true){
				int x = 0;
				int p = i;
				int count = 0;
				while(x == 0 && p != 0){
					p = arr[p];
					if(p == -1){
						break;
					}
					else if(hotel[p] == true){
						System.out.println("hotelbreak");
						break;
					}
					else if(count > n){
						count = 0;
						break;
					}
					else{
						count++;
					}
				}
				if(count > max){
					max = count;
					index = i;
				}
			}
		}
		if(max != 0){
			ArrayList<Integer> way = new ArrayList<Integer>();

			System.out.println(max);
			while(max > 0){
				max--;
				way.add(index);
				index = arr[index];
//				if(hotel[index] == true){
//					break;
//				}
			}

			for(int i = way.size()-1; i > 0; i--){
				System.out.print(way.get(i) + " ");
			}
			System.out.println(way.get(0));
		}
		else{
			for(int i = 1; i <= n; i++){
				if(hotel[i]){
					System.out.println(1);
					System.out.println(i);
					break;
				}
			}
		}

	}

	public int iread() throws Exception {
		return Integer.parseInt(wread());
	}

	public double dread() throws Exception {
		return Double.parseDouble(wread());
	}

	public long lread() throws Exception {
		return Long.parseLong(wread());
	}

	public String wread() throws IOException {
		StringBuilder b = new StringBuilder();
		int c;
		c = in.read();
		while (c >= 0 && c <= ' ')
			c = in.read();
		if (c < 0)
			return "";
		while (c > ' ') {
			b.append((char) c);
			c = in.read();
		}
		return b.toString();
	}

	public static void main(String[] args) throws Throwable {

		if(test){ //run all cases from testfile:
			BufferedReader testdataReader = new BufferedReader(new FileReader(testDataFile));
			String readLine = testdataReader.readLine();
			int casenr = 0;
			out: while (true) {
				BufferedWriter w = new BufferedWriter(new FileWriter(feedFile));
				if(!readLine.equals("input")){
					break;
				}
				while (true) {
					readLine = testdataReader.readLine();
					if(readLine.equals("output")){
						break;
					}
					w.write(readLine + "\n");
				}
				w.close();
				System.out.println("Answer on case "+(++casenr)+": ");

				new B().solve();
				System.out.println("Expected answer: ");

				while (true) {
					readLine = testdataReader.readLine();

					if(readLine == null){
						break out;
					}
					if(readLine.equals("input")){
						break;
					}
					System.out.println(readLine);
				}
				System.out.println("----------------");
			}
			testdataReader.close();
		} else { // run on server
			new B().solve();
		}
		out.close();
	}

	public B() throws Throwable {
		if (test) {
			in = new BufferedReader(new FileReader(new File(feedFile)));
		}
	}

	InputStreamReader inp = new InputStreamReader(System.in);
	BufferedReader in = new BufferedReader(inp);
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

	enum CompetitionType {CF, OTHER};
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

/**
 * Works good for CF
 */
public class B {

	//some local config
	static boolean test = false;
	static String testDataFile = "test.txt";
	static String feedFile = "feed.txt";
	CompetitionType type = CompetitionType.CF;
	private static String ENDL = "\n";

	// solutions

	private void solve() throws Throwable {
		int n = iread();
		boolean[] hotel = new boolean[n+1];

		for(int i = 1; i <= n; i++){
			if(iread() == 1){
				hotel[i] = true;
			}
		}

		for(int i = 1; i<=n; i++){
			if(hotel[i] == true){
				System.out.println(1);
				System.out.println(i);
				break;
			}
		}
	}

	public int iread() throws Exception {
		return Integer.parseInt(wread());
	}

	public double dread() throws Exception {
		return Double.parseDouble(wread());
	}

	public long lread() throws Exception {
		return Long.parseLong(wread());
	}

	public String wread() throws IOException {
		StringBuilder b = new StringBuilder();
		int c;
		c = in.read();
		while (c >= 0 && c <= ' ')
			c = in.read();
		if (c < 0)
			return "";
		while (c > ' ') {
			b.append((char) c);
			c = in.read();
		}
		return b.toString();
	}

	public static void main(String[] args) throws Throwable {

		if(test){ //run all cases from testfile:
			BufferedReader testdataReader = new BufferedReader(new FileReader(testDataFile));
			String readLine = testdataReader.readLine();
			int casenr = 0;
			out: while (true) {
				BufferedWriter w = new BufferedWriter(new FileWriter(feedFile));
				if(!readLine.equals("input")){
					break;
				}
				while (true) {
					readLine = testdataReader.readLine();
					if(readLine.equals("output")){
						break;
					}
					w.write(readLine + "\n");
				}
				w.close();
				System.out.println("Answer on case "+(++casenr)+": ");

				new B().solve();
				System.out.println("Expected answer: ");

				while (true) {
					readLine = testdataReader.readLine();

					if(readLine == null){
						break out;
					}
					if(readLine.equals("input")){
						break;
					}
					System.out.println(readLine);
				}
				System.out.println("----------------");
			}
			testdataReader.close();
		} else { // run on server
			new B().solve();
		}
		out.close();
	}

	public B() throws Throwable {
		if (test) {
			in = new BufferedReader(new FileReader(new File(feedFile)));
		}
	}

	InputStreamReader inp = new InputStreamReader(System.in);
	BufferedReader in = new BufferedReader(inp);
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

	enum CompetitionType {CF, OTHER};
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * <b> Algorithm on Codeforces. Problem C div 2 </b> </br>
 * @author Huynh Quang Thao
 *
 */
public class Main {

        public static void main(String[] args) throws Exception {
                Main main = new Main();
                main.run();
        }

        public void run() throws Exception {
                 Scanner sc = null;
             PrintWriter pr = null;

             pr=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
             sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
           //  sc = new Scanner(new File("input.txt"));

             int n = sc.nextInt();

             int[] revpath = new int[n+1];
             int[] type = new int[n+1];
             int[] count = new int[n+1];
             int[] path = new int[n+1];

             for (int i = 1; i <= n; i++) type[i] = sc.nextInt();
             for (int i = 1; i <= n; i++) {
                int a = sc.nextInt();
                // exist path from a to i
                path[a] = i;
                count[a]++;
             }

             // building rev-path
             for (int i = 1; i <= n; i++) {
                 int to = path[i];
                 if (count[i] == 1) revpath[to] = i;
             }

            /* for (int i = 1; i <= n; i++) {
                 System.out.println(path[i]);
             }
             System.out.println("---------");

             for (int i = 1; i <= n; i++) {
                 System.out.println(revpath[i]);
             }*/

             // dfs from hotel
             List<Integer> resList = new ArrayList<Integer>();
             for (int i = 1; i <= n; i++) {
                 if (type[i] == 1) {
                         int v = i;
                         // dfs from v - vertex
                         List<Integer> list = new ArrayList<Integer>();
                         list.add(v);
                         while (revpath[v] != 0) {
                                 v = revpath[v];
                                 list.add(v);
                         }
                         if (list.size() > resList.size()) resList = list;
                 }
             }

             System.out.printf("%d\n", resList.size());
             for (int i = resList.size()-1; i >= 0; i--) System.out.printf("%d ", resList.get(i));

             pr.close();
             sc.close();
        }

        public void dfs(int v) {

        }

        static class Team {
                int baloon;
                int rA;
                int rB;
                public Team(int baloon, int rA, int rB) {
                        this.baloon = baloon;
                        this.rA = rA;
                        this.rB = rB;
                }
        }

        static class InputReader {
                public BufferedReader reader;

                public StringTokenizer tokenizer;

                public InputReader(InputStream stream) {
                        reader = new BufferedReader(new InputStreamReader(stream));
                        tokenizer = null;
                }

                public String next() {
                        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                                try {
                                        tokenizer = new StringTokenizer(reader.readLine());
                                }
                                catch (IOException e) {
                                        throw new RuntimeException(e);
                                }
                        }
                        return tokenizer.nextToken();
                }

                public int nextInt() {
                        return Integer.parseInt(next());
                }

                public double nextDouble() {
                        return Double.parseDouble(next());
                }

                public float nextFloat() {
                        return Float.parseFloat(next());
                }

                public long nextLong() {
                        return Long.parseLong(next());
                }

                public BigInteger nextBigInteger() {
                        return new BigInteger(next());
                }

        }
}


import java.util.*;
public class b {
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    int n = input.nextInt();
    int[] types = new int[n];
    for(int i = 0; i<n; i++) types[i] = input.nextInt();
    int[] prev = new int[n], count = new int[n];
    for(int i = 0; i<n; i++)
    {
        int p = input.nextInt()-1;
        prev[i] = p;
        if(p==-1) continue;
        count[p]++;
    }
    int max = 0, maxdex = 0;
    for(int i = 0; i<n; i++)
    {
        if(types[i] == 0) continue;
        int x = 1;
        int at = i;
        while(true)
        {
            int next = prev[at];
            if(next == -1 || types[next] == 1 || count[next] > 1) break;
            at = next;
            x++;
        }
        if(x>max)
        {
            max = x;
            maxdex = i;
        }
    }
    int[] data = new int[max];
    data[max-1] = maxdex;
    for(int i = max-2; i>=0; i--) data[i] = prev[data[i+1]];
    System.out.println(max);
    for(int i = 0; i<max; i++) System.out.print((1+data[i])+" ");
}
}


 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class B implements Runnable {

    LinkedList<Integer>[] g;
    int[] which, deg;
    ArrayList<Integer> path;

    void dfs(int i) {
    	if (i == 0) return;
        path.add(i);
        if (g[i].size() == 1 && deg[g[i].get(0)] == 1) {
        	dfs(g[i].get(0));
        } else {
        	return;
        }
    }

    private void solve() throws IOException {
        int n = nextInt();
        which = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            which[i] = nextInt();
        }
        g = new LinkedList[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new LinkedList<Integer>();
        }
        deg = new int[n + 1];
        for (int i = 0; i < n; i++) {
            int from = nextInt();
            if (from == 0) continue;
            g[i + 1].add(from);
            deg[from]++;
        }
        path = new ArrayList<Integer>();
        ArrayList<Integer> pathFinal = new ArrayList<Integer>();
        int max = 0;

        for (int i = 1; i <= n; i++) {
            if (which[i] == 1) {
                path.clear();
                dfs(i);
                if (path.size() > max) {
                    max = path.size();
                    pathFinal = (ArrayList<Integer>) path.clone();
                }
            }
        }
        Collections.reverse(pathFinal);
        writer.println(max);
        for (int res : pathFinal) writer.print(res + " ");
    }

    public static void main(String[] args) {
        new B().run();
    }

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter writer;

    public void run() {
        try {
            reader = new BufferedReader(new BufferedReader(
                    new InputStreamReader(System.in)));
            writer = new PrintWriter(System.out);
            tokenizer = null;
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    BigInteger nextBigInteger() throws IOException {
        return new BigInteger(nextToken());
    }

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    void p(Object... objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    void pl(Object... objects) {
        p(objects);
        writer.println();
    }

    int cc;

    void pf() {
        writer.printf("Case #%d: ", ++cc);
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Resort
{
	public static void main (String[] args) throws IOException
	{
		InputStreamReader a = new InputStreamReader(System.in);
		BufferedReader buf = new BufferedReader(a);
		int N = Integer.parseInt(buf.readLine());
		String[] tmp = buf.readLine().split(" ");
		boolean[] hotels = new boolean[N];
		for(int i =0; i<N; i++)
			if(Integer.parseInt(tmp[i])==0)
				hotels[i] = false;
			else
				hotels[i] = true;
		tmp = buf.readLine().split(" ");
		int[] fromhere = new int[N];
		int[] tohere = new int[N];
		for(int i =0; i<N; i++)
		{
			int x = Integer.parseInt(tmp[i]);
			tohere[i] = x;
			if (x!=0)
				fromhere[x-1]++;
		}
		int max=0;
		ArrayList<Integer> stack = new ArrayList<Integer>();
		for(int i=0; i<N; i++)
		{
			if(hotels[i])
			{
				ArrayList<Integer> stack1 = new ArrayList<Integer>();
				int len = 1;
				int curr=i;
				stack1.add(curr+1);
				while(tohere[curr]!=0)
				{
					curr = tohere[curr]-1;
					if(hotels[curr] || fromhere[curr]>1)
						break;
					len++;
					stack1.add(curr+1);
				}
				if(max<len)
				{
					max=len;
					stack=stack1;
				}
			}
		}
		System.out.println(max);
		for(int i=max-1; i>=0; i--)
		{
			System.out.print(stack.get(i));
			System.out.print(" ");
		}
	}
}


import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st = new StringTokenizer("");

    private void run() throws IOException {
        if (new File("input.txt").exists())
            in = new BufferedReader(new FileReader("input.txt"));
        else
            in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        in.close();
        out.close();
    }

    private void solve() throws IOException {

        int n = nextInt();
        boolean hot[] = new boolean[n + 1];
        for (int i = 0; i < n; i++)
            hot[i + 1] = (nextInt() == 1);
        int from[] = new int[n + 1];
        int deg[] = new int[n + 1];
        for (int i = 0; i < n; i++) {
            from[i + 1] = nextInt();
            deg[from[i + 1]]++;
        }
        int ans = 0;
        int end = -1;
        for (int i = 1; i <= n; i++)
            if (hot[i]) {
                int cans = 1;
                int cur = i;
                int prev = from[cur];
                while (prev > 0 && deg[prev] == 1) {
                    cans++;
                    cur = prev;
                    prev = from[cur];
                }
                if (cans > ans) {
                    ans = cans;
                    end = i;
                }
            }
        int a[] = new int[n];
        out.println(ans);
        for (int i = 0; i < ans; i++) {
            a[i] = end;
            end = from[end];
        }
        for (int i = ans - 1; i > -1; i--) {
            if (i != ans - 1)
                out.print(" ");
            out.print(a[i]);
        }
    }

//  class Bomb implements Comparable<Bomb> {
//
//      int x;
//      int y;
//      Bomb(int x, int y) {
//          this.x = x;
//          this.y = y;
//      }
//      @Override
//      public int compareTo(Bomb o) {
//          // TODO Auto-generated method stub
//          return ((long)abs(this.x) + abs(this.y) < (long)abs(o.x) + abs(o.y)) ? -1 : 1;
//      }
//
//  }

    void chk(boolean b) {
        if (b)
            return;
        System.out.println(new Error().getStackTrace()[1]);
        exit(999);
    }
    void deb(String fmt, Object... args) {
        System.out.printf(Locale.US, fmt + "%n", args);
    }
    String nextToken() throws IOException {
        while (!st.hasMoreTokens())
            st = new StringTokenizer(in.readLine());
        return st.nextToken();
    }
    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }
    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
    String nextLine() throws IOException {
        st = new StringTokenizer("");
        return in.readLine();
    }
    boolean EOF() throws IOException {
        while (!st.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null)
                return true;
            st = new StringTokenizer(s);
        }
        return false;
    }
}


import java.util.*;
import java.io.*;

public class WriteCodeHere {
	public static void main(String[] args) throws IOException {
		st = new StringTokenizer(rd.readLine());
		int n = Integer.parseInt(st.nextToken());
		isHotel = new boolean[n+1];
		st = new StringTokenizer(rd.readLine());
		for(int i=1; i<=n; i++)
			isHotel[i] = (Integer.parseInt(st.nextToken())==0? false: true);
		G = new ArrayList[n+1];
		for(int i=0; i<=n; i++) G[i] = new ArrayList<Integer>();
		st  = new StringTokenizer(rd.readLine());
		for(int i=1; i<=n; i++){
			int cur = Integer.parseInt(st.nextToken());
			if(cur==0) continue;
			G[cur].add(i);
		}
		for(int i=0; i<n; i++){
			if(isHotel[i+1]){
				start = i+1;
				break;
			}
		}
		boolean[] been = new boolean[n+1];
		for(int i=1; i<=n; i++){
			if(isHotel[i]) continue;
			if(!been[i]){
				doJob(i, been);
			}
		}
		pw.println(size);
		int cur = start;
		if(size==1){
			pw.println(cur);
			pw.flush();
			return;
		}
		while(true){
			pw.print(cur+" ");
			if(G[cur].size()==0) break;
			cur = G[cur].get(0);
		}
		pw.println();
		pw.flush();
	}

	static int doJob(int v, boolean[] been){
		if(been[v]) return -1; // cycle
		been[v] = true;
		if(isHotel[v]) return 1;
		if(G[v].size()!=1) return -1;
		int ret = 1 + doJob(G[v].get(0), been);
		if(ret==0) return -1;
		if(ret > size){
			size = ret;
			start = v;
		}
		return ret;
	}

	static boolean[] isHotel;

	static ArrayList<Integer> [] G;

	static int size = 1, start = -1;

	static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter pw = new PrintWriter(System.out);
	static StringTokenizer st;
}


import java.util.*;
import java.io.*;

public class WriteCodeHere {
	public static void main(String[] args) throws IOException {
		st = new StringTokenizer(rd.readLine());
		int n = Integer.parseInt(st.nextToken());
		isHotel = new boolean[n+1];
		st = new StringTokenizer(rd.readLine());
		for(int i=1; i<=n; i++)
			isHotel[i] = (Integer.parseInt(st.nextToken())==0? false: true);
		G = new ArrayList[n+1];
		for(int i=0; i<=n; i++) G[i] = new ArrayList<Integer>();
		st  = new StringTokenizer(rd.readLine());
		for(int i=1; i<=n; i++){
			int cur = Integer.parseInt(st.nextToken());
			if(cur==0) continue;
			G[cur].add(i);
		}
		for(int i=0; i<n; i++){
			if(isHotel[i+1]){
				start = i+1;
				break;
			}
		}
		boolean[] been = new boolean[n+1];
		for(int i=1; i<=n; i++){
			if(!been[i]){
				doJob(i, been);
			}
		}
		pw.println(size);
		int cur = start;
		while(true){
			pw.print(cur+" ");
			if(G[cur].size()==0) break;
			cur = G[cur].get(0);
		}
		pw.println();
		pw.flush();
	}

	static int doJob(int v, boolean[] been){
		if(been[v]) return -1; // cycle
		been[v] = true;
		if(isHotel[v]) return 1;
		if(G[v].size()!=1) return -1;
		int ret = 1 + doJob(G[v].get(0), been);
		if(ret > size){
			size = ret;
			start = v;
		}
		return ret;
	}

	static boolean[] isHotel;

	static ArrayList<Integer> [] G;

	static int size = 1, start = -1;

	static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter pw = new PrintWriter(System.out);
	static StringTokenizer st;
}


import java.util.*;
import java.io.*;

public class WriteCodeHere {
	public static void main(String[] args) throws IOException {
		st = new StringTokenizer(rd.readLine());
		int n = Integer.parseInt(st.nextToken());
		isHotel = new boolean[n+1];
		st = new StringTokenizer(rd.readLine());
		for(int i=1; i<=n; i++)
			isHotel[i] = (Integer.parseInt(st.nextToken())==0? false: true);
		G = new ArrayList[n+1];
		for(int i=0; i<=n; i++) G[i] = new ArrayList<Integer>();
		st  = new StringTokenizer(rd.readLine());
		for(int i=1; i<=n; i++){
			int cur = Integer.parseInt(st.nextToken());
			if(cur==0) continue;
			G[cur].add(i);
		}
		for(int i=0; i<n; i++){
			if(isHotel[i+1]){
				start = i+1;
				break;
			}
		}
		boolean[] been = new boolean[n+1];
		for(int i=1; i<=n; i++){
			if(isHotel[i]) continue;
			if(!been[i]){
				doJob(i, been);
			}
		}
		pw.println(size);
		int cur = start;
		while(true){
			pw.print(cur+" ");
			if(G[cur].size()==0) break;
			cur = G[cur].get(0);
		}
		pw.println();
		pw.flush();
	}

	static int doJob(int v, boolean[] been){
		if(been[v]) return -1; // cycle
		been[v] = true;
		if(isHotel[v]) return 1;
		if(G[v].size()!=1) return -1;
		int ret = 1 + doJob(G[v].get(0), been);
		if(ret==0) return -1;
		if(ret > size){
			size = ret;
			start = v;
		}
		return ret;
	}

	static boolean[] isHotel;

	static ArrayList<Integer> [] G;

	static int size = 1, start = -1;

	static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter pw = new PrintWriter(System.out);
	static StringTokenizer st;
}


import java.util.*;
import java.io.*;

public class WriteCodeHere {
	public static void main(String[] args) throws IOException {
		st = new StringTokenizer(rd.readLine());
		int n = Integer.parseInt(st.nextToken());
		isHotel = new boolean[n+1];
		st = new StringTokenizer(rd.readLine());
		for(int i=1; i<=n; i++)
			isHotel[i] = (Integer.parseInt(st.nextToken())==0? false: true);
		G = new ArrayList[n+1];
		for(int i=0; i<=n; i++) G[i] = new ArrayList<Integer>();
		st  = new StringTokenizer(rd.readLine());
		for(int i=1; i<=n; i++){
			int cur = Integer.parseInt(st.nextToken());
			if(cur==0) continue;
			G[cur].add(i);
		}
		for(int i=0; i<n; i++){
			if(isHotel[i+1]){
				start = i+1;
				break;
			}
		}
		boolean[] been = new boolean[n+1];
		for(int i=1; i<=n; i++){
			if(!been[i]){
				doJob(i, been);
			}
		}
		pw.println(size);
		int cur = start;
		while(true){
			pw.print(cur+" ");
			if(G[cur].size()==0) break;
			cur = G[cur].get(0);
		}
		pw.println();
		pw.flush();
	}

	static int doJob(int v, boolean[] been){
		if(been[v]) return -1; // cycle
		been[v] = true;
		if(isHotel[v]) return 1;
		if(G[v].size()!=1) return -1;
		int ret = 1 + doJob(G[v].get(0), been);
		if(ret==0) return -1;
		if(ret > size){
			size = ret;
			start = v;
		}
		return ret;
	}

	static boolean[] isHotel;

	static ArrayList<Integer> [] G;

	static int size = 1, start = -1;

	static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter pw = new PrintWriter(System.out);
	static StringTokenizer st;
}


import java.util.*;
import java.io.*;

public class WriteCodeHere {
	public static void main(String[] args) throws IOException {
		st = new StringTokenizer(rd.readLine());
		int n = Integer.parseInt(st.nextToken());
		isHotel = new boolean[n+1];
		st = new StringTokenizer(rd.readLine());
		for(int i=1; i<=n; i++)
			isHotel[i] = (Integer.parseInt(st.nextToken())==0? false: true);
		G = new ArrayList[n+1];
		for(int i=0; i<=n; i++) G[i] = new ArrayList<Integer>();
		st  = new StringTokenizer(rd.readLine());
		int[] IN = new int[n+1];
		for(int i=1; i<=n; i++){
			int cur = Integer.parseInt(st.nextToken());
			if(cur==0) continue;
			G[cur].add(i);
			IN[i] = cur;
		}
		for(int i=0; i<n; i++){
			if(isHotel[i+1]){
				start = i+1;
				break;
			}
		}
		boolean[] been = new boolean[n+1];
		for(int i=1; i<=n; i++){
			if(isHotel[i]) continue;
			if(!been[i] && !(IN[i]!=0 && G[IN[i]].size()==1)){
				doJob(i, been);
			}
		}
		pw.println(size);
		int cur = start;
		if(size==1){
			pw.println(cur);
			pw.flush();
			return;
		}
		while(true){
			pw.print(cur+" ");
			if(G[cur].size()==0) break;
			cur = G[cur].get(0);
		}
		pw.println();
		pw.flush();
	}

	static int doJob(int v, boolean[] been){
		if(been[v]) return -1; // cycle
		been[v] = true;
		if(isHotel[v]) return 1;
		if(G[v].size()!=1) return -1;
		int ret = 1 + doJob(G[v].get(0), been);
		if(ret==0) return -1;
		if(ret > size){
			size = ret;
			start = v;
		}
		return ret;
	}

	static boolean[] isHotel;

	static ArrayList<Integer> [] G;

	static int size = 1, start = -1;

	static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter pw = new PrintWriter(System.out);
	static StringTokenizer st;
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * <b> Algorithm on Codeforces. Problem C div 2 </b> </br>
 * @author Huynh Quang Thao
 *
 */
public class Main {

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.run();
    }

    public void run() throws Exception {
         Scanner sc = null;
         PrintWriter pr = null;

         pr=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
         sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
       //  sc = new Scanner(new File("input.txt"));

         int n = sc.nextInt();

         int[] revpath = new int[n+1];
         int[] type = new int[n+1];
         int[] count = new int[n+1];
         int[] path = new int[n+1];

         for (int i = 1; i <= n; i++) type[i] = sc.nextInt();
         for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            // exist path from a to i
            path[a] = i;
            count[a]++;
         }

         // building rev-path
         for (int i = 1; i <= n; i++) {
             int to = path[i];
             if (count[i] == 1) revpath[to] = i;
         }

        /* for (int i = 1; i <= n; i++) {
             System.out.println(path[i]);
         }
         System.out.println("---------");

         for (int i = 1; i <= n; i++) {
             System.out.println(revpath[i]);
         }*/

         // dfs from hotel
         List<Integer> resList = new ArrayList<Integer>();
         for (int i = 1; i <= n; i++) {
             if (type[i] == 1) {
                 int v = i;
                 // dfs from v - vertex
                 List<Integer> list = new ArrayList<Integer>();
                 list.add(v);
                 while (revpath[v] != 0) {
                     v = revpath[v];
                     list.add(v);
                 }
                 if (list.size() > resList.size()) resList = list;
             }
         }

         System.out.printf("%d\n", resList.size());
         for (int i = resList.size()-1; i >= 0; i--) System.out.printf("%d ", resList.get(i));

         pr.close();
         sc.close();
    }

    public void dfs(int v) {

    }

    static class Team {
        int baloon;
        int rA;
        int rB;
        public Team(int baloon, int rA, int rB) {
            this.baloon = baloon;
            this.rA = rA;
            this.rB = rB;
        }
    }

    static class InputReader {
        public BufferedReader reader;

        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
                catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

    }
}


import java.util.*;
import java.io.*;

public class Solution{

     public static void main(String []args){
        Solution ob=new Solution();
        ob.go();
     }

     void go() {
         Scanner sc=new Scanner(System.in);
         int n=sc.nextInt();
         boolean[] typem=new boolean[n+1];
         back=new int[n+1];
         ban=new int[n+1];
         int t;
         for(int i=1;i<=n;i++) {
             t=sc.nextInt();
             if (t==0)typem[i]=true;
             else typem[i]=false;
             ban[i]=0;
         }
         for(int i=1;i<=n;i++) {
             t=sc.nextInt();
             back[i]=t;
             ban[t]++;
//             if (ban[t]>2)//
         }

         //Stack<Integer> p=new Stack<Integer>();
         int sum,max=0,maxs=0;
         pl=new int[n+1];
         for(int i=0;i<=n;i++)pl[i]=-1;

         for(int i=1;i<=n;i++) {
             if (!typem[i]) {
                 sum=cal(i);
                 if (sum>maxs) {
                     max=i;
                     maxs=sum;
                 }
             }
         }
         pr(maxs+"\n");
         int cur=max;
         Stack<Integer> p=new Stack<Integer>();
         while(cur!=0){
             p.push(cur);
             cur=back[cur];
             if (ban[cur]>=2) break;
         }
         pr(p.pop()+"");
         while(!p.empty()) {
             pr(" "+p.pop());
         }

     }
     int[] pl;
     int[] back,ban;
     int cal(int i) {
         if (pl[i]!=-1) return pl[i];
         if (ban[i]>=2) {pl[i]=0;return 0;}
         if (back[i]==0) {pl[i]=1;return 1;}
         pl[i]=1+cal(back[i]);
         return pl[i];
     }

     void pr(String a) {
         System.out.print(a);
     }
}


import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;

import org.omg.CORBA.UNKNOWN;

public class HelloWorld {

    InputReader input;
    PrintWriter output;
    BufferedReader inp;

    void run(){
        output = new PrintWriter(new OutputStreamWriter(System.out));
        input = new InputReader(System.in);
        inp = new BufferedReader(new InputStreamReader(System.in));
        solve();
        output.flush();
    }

    public static void main(String[] args){
        new HelloWorld().run();
    }

    int n;
    boolean[] isHotel;
    int[] next;
    HashSet<Integer> seen;

    void solve() {
        n = input.readInt();
        isHotel = new boolean[n+1];
        next = new int[n+1];
        seen = new HashSet<Integer>();
        for(int i = 1; i <= n; i++) {
            int t = input.readInt();
            isHotel[i] = t == 1;
        }
        for(int i = 1; i <= n; i++) {
            int t = input.readInt();
            if(seen.contains(t)) {
                next[t] = 0;
            }
            else {
                next[t] = i;
                seen.add(t);
            }
        }
        for(int i = 1; i <= n; i++) {
            findPath(i);
        }
        output.println(maxLen);
        for(int i = 0; i < maxLen; i++) {
            output.print(maxi+" ");
            maxi = next[maxi];
        }
    }

    int maxLen = 0;
    int maxi = 0;

    void findPath(int i) {
        int len = 0;
        int start = i;
        if(isHotel[i]) {
            checkMax(start, 1);
            return;
        }
        while(i <= n && next[i] != 0 && !isHotel[i]) {
            len++;
            if(i == next[i]) {
                break;
            }
            i = next[i];
        }
        if(isHotel[i]) {
            len++;
            checkMax(start, len);
        }
    }

    void checkMax(int s, int l) {
        if(l > maxLen) {
            maxLen = l;
            maxi = s;
        }
    }

    class node implements Comparable<node>{
        int destination;
        int direction;
        int distance;

        public node(int destination, int distance, int direction) {
            this.direction = direction;
            this.distance = distance;
            this.destination = destination;
        }

        public int compareTo(node b) {
            return this.distance - b.distance;
        }
    }

    class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars)
            {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public Long readLong() {
            return Long.parseLong(readString());
        }

        public Double readDouble() {
            return Double.parseDouble(readString());
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }

}


import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;

import org.omg.CORBA.UNKNOWN;

public class HelloWorld {

    InputReader input;
    PrintWriter output;
    BufferedReader inp;

    void run(){
        output = new PrintWriter(new OutputStreamWriter(System.out));
        input = new InputReader(System.in);
        inp = new BufferedReader(new InputStreamReader(System.in));
        solve();
        output.flush();
    }

    public static void main(String[] args){
        new HelloWorld().run();
    }

    int n;
    boolean[] isHotel;
    int[] next;
    HashSet<Integer> seen;

    void solve() {
        n = input.readInt();
        isHotel = new boolean[n+1];
        next = new int[n+1];
        seen = new HashSet<Integer>();
        for(int i = 1; i <= n; i++) {
            int t = input.readInt();
            isHotel[i] = t == 1;
        }
        for(int i = 1; i <= n; i++) {
            int t = input.readInt();
            if(seen.contains(t)) {
                next[t] = 0;
            }
            else {
                next[t] = i;
                seen.add(t);
            }
        }
        for(int i = 1; i <= n; i++) {
            findPath(i);
        }
        output.println(maxLen);
        for(int i = 0; i < maxLen; i++) {
            output.print(maxi+" ");
            maxi = next[maxi];
        }
    }

    int maxLen = 0;
    int maxi = 0;

    void findPath(int i) {
        int len = 0;
        int start = i;
        if(isHotel[i]) {
            checkMax(start, 1);
            return;
        }
        while(i <= n && next[i] != 0 && !isHotel[i]) {
            len++;
            i = next[i];
        }
        if(isHotel[i]) {
            len++;
            checkMax(start, len);
        }
    }

    void checkMax(int s, int l) {
        if(l > maxLen) {
            maxLen = l;
            maxi = s;
        }
    }

    class node implements Comparable<node>{
        int destination;
        int direction;
        int distance;

        public node(int destination, int distance, int direction) {
            this.direction = direction;
            this.distance = distance;
            this.destination = destination;
        }

        public int compareTo(node b) {
            return this.distance - b.distance;
        }
    }

    class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars)
            {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public Long readLong() {
            return Long.parseLong(readString());
        }

        public Double readDouble() {
            return Double.parseDouble(readString());
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }

}


import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;

import org.omg.CORBA.UNKNOWN;

public class HelloWorld {

    InputReader input;
    PrintWriter output;
    BufferedReader inp;

    void run(){
        output = new PrintWriter(new OutputStreamWriter(System.out));
        input = new InputReader(System.in);
        inp = new BufferedReader(new InputStreamReader(System.in));
        solve();
        output.flush();
    }

    public static void main(String[] args){
        new HelloWorld().run();
    }

    int n;
    boolean[] isHotel;
    int[] prev;
    HashSet<Integer> seen;
    int[] out;

    void solve() {
        n = input.readInt();
        isHotel = new boolean[n+1];
        prev = new int[n+1];
        out = new int[n+1];
        for(int i = 1; i <= n; i++) {
            int t = input.readInt();
            isHotel[i] = t == 1;
        }
        for(int i = 1; i <= n; i++) {
            int t = input.readInt();
            out[t]++;
            prev[i] = t;
        }
        for(int i = 1; i <= n; i++) {
            findPath(i);
        }
        output.println(maxLen);
        //output.println(maxi);
        print(1, maxi);
    }

    void print(int i, int maxi) {
        if(i == maxLen) {
            output.print(maxi+" ");
            return;
        }
        print(i+1, prev[maxi]);
        output.print(maxi+" ");
    }

    int maxLen = 0;
    int maxi = 0;

    void findPath(int i) {
        int len = 1;
        int start = i;
        if(!isHotel[i]) {
            return;
        }
        while(out[prev[i]] <= 1 && prev[i] != 0) {
            //System.out.println("i = "+i);
            len++;
            i = prev[i];
        }
        checkMax(start, len);
    }

    void checkMax(int s, int l) {
        if(l > maxLen) {
            maxLen = l;
            maxi = s;
        }
    }

    class node implements Comparable<node>{
        int destination;
        int direction;
        int distance;

        public node(int destination, int distance, int direction) {
            this.direction = direction;
            this.distance = distance;
            this.destination = destination;
        }

        public int compareTo(node b) {
            return this.distance - b.distance;
        }
    }

    class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars)
            {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public Long readLong() {
            return Long.parseLong(readString());
        }

        public Double readDouble() {
            return Double.parseDouble(readString());
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }

}


import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;

public class Main {
	static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
	static StringTokenizer st = new StringTokenizer("");
	static String next()	throws Exception	{
		while	( !st.hasMoreTokens() )	st = new StringTokenizer( br.readLine() );
		return	st.nextToken();
	}
	public static void main(String [] asda)	throws Exception	{
		PrintWriter out = new PrintWriter( new BufferedOutputStream(System.out) );
		//
		int N = Integer.parseInt( next() );
		// save hotels
		ArrayList<Integer> hotels = new ArrayList<Integer>();
		for	(int i = 1; i <= N; i++)	{
			if	( next().equals("1") )
				hotels.add(0, i);
		}
		// read paths
		int [] graph = new int [1 + N];
		int [] pathsTo = new int [1 + N];	// pathsTo[i] = number of paths that connect with i
		for	(int i = 1; i <= N; i++)	{
			int next = Integer.parseInt( next() );
			// graph[i] = next, i -> next
			graph[i] = next;
			pathsTo[next]++;
		}
		// check for a valid path
		ArrayList<Integer> path = new ArrayList<Integer>();	// best path
		for	(Integer hotel : hotels)	{
			ArrayList<Integer> aux = getPath(graph, pathsTo, hotel);
			if	( aux.size() > path.size() )	path = aux;
			else if	(
				aux.size() > 0	&&
				aux.size() == path.size()	&&
				aux.get( aux.size() - 1 ) > path.get( path.size() - 1)
			)	path = aux;
		}
		out.println( path.size() );
		while	( path.size() > 1 )	{
			out.print( path.remove(0) + " " );
		}
		out.println( path.get(0) );
		out.flush();
	}
	private static ArrayList<Integer> getPath(int [] graph, int [] pathsTo, int id) {
		ArrayList<Integer> path = new ArrayList<Integer>();
	//	System.out.println( "   getting path from: " + id );
		path.add(id);
		while	( true )	{
		//	if	( graph[id] != id - 1 )	break;
			id = graph[id];
			if	( id == 0 || pathsTo[id] != 1 )	break;
			path.add(0, id);
	//		System.out.println( "    " + id );
		}
		return	path;
	}
}


import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;

public class Main {
	static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
	static StringTokenizer st = new StringTokenizer("");
	static String next()	throws Exception	{
		while	( !st.hasMoreTokens() )	st = new StringTokenizer( br.readLine() );
		return	st.nextToken();
	}
	public static void main(String [] asda)	throws Exception	{
		PrintWriter out = new PrintWriter( new BufferedOutputStream(System.out) );
		//
		int N = Integer.parseInt( next() );
		ArrayList<Integer> graph [] = (ArrayList<Integer> []) new ArrayList[N + 1];
		for (int i = 1; i <= N; i++)	graph[i] = new ArrayList<Integer>();
		// save hotels
		HashSet<Integer> hotels = new HashSet<Integer>();
		for	(int i = 1; i <= N; i++)	{
			if	( next().equals("1") )
				hotels.add(i);
		}
		// read paths
		for	(int i = 1; i <= N; i++)	{
			int path = Integer.parseInt( next() );
			if	( path == 0 )	continue;
			graph[path].add(i);
		}
		// print paths
	/*	for	(int i = 1; i <= N; i++)	{
			out.print("  from: " + i + ", paths to: ");
			for (Integer v : graph[i])	out.print( v + " " );
			out.println();
		}
		*/
		// check for a valid path
		ArrayList<Integer> path = new ArrayList<Integer>();	// best path
		for	(int i = 1; i <= N; i++)	{
			ArrayList<Integer> aux = getPath(graph, i, hotels);
			if	( aux.size() > path.size() )	path = aux;
		}
		out.println( path.size() );
		while	( path.size() > 1 )	{
			out.print( path.remove(0) + " " );
		}
		out.println( path.get(0) );
		out.flush();
	}
	private static ArrayList<Integer> getPath(ArrayList<Integer>[] graph, int id, HashSet<Integer> hotels) {
		ArrayList<Integer> path = new ArrayList<Integer>();
	//	System.out.println( "   getting path from: " + id );
		path.add(id);
		while	( true )	{
			if	( hotels.contains(id) )	return	path;
			if	( graph[id].size() != 1 || id == graph[id].get(0) )	break;
			int aux = graph[id].get(0);
		//	if	( aux != id + 1 )	break;
			id = aux;
			path.add(id);
	//		System.out.println( "    " + id );
		}
		path.clear();
		return	path;
	}
}


import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;

public class Main {
	static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
	static StringTokenizer st = new StringTokenizer("");
	static String next()	throws Exception	{
		while	( !st.hasMoreTokens() )	st = new StringTokenizer( br.readLine() );
		return	st.nextToken();
	}
	public static void main(String [] asda)	throws Exception	{
		PrintWriter out = new PrintWriter( new BufferedOutputStream(System.out) );
		//
		int N = Integer.parseInt( next() );
		ArrayList<Integer> graph [] = (ArrayList<Integer> []) new ArrayList[N + 1];
		for (int i = 1; i <= N; i++)	graph[i] = new ArrayList<Integer>();
		// save hotels
		HashSet<Integer> hotels = new HashSet<Integer>();
		for	(int i = 1; i <= N; i++)	{
			if	( next().equals("1") )
				hotels.add(i);
		}
		// read paths
		for	(int i = 1; i <= N; i++)	{
			int path = Integer.parseInt( next() );
			if	( path == 0 )	continue;
			graph[path].add(i);
		}
		// print paths
	/*	for	(int i = 1; i <= N; i++)	{
			out.print("  from: " + i + ", paths to: ");
			for (Integer v : graph[i])	out.print( v + " " );
			out.println();
		}
		*/
		// check for a valid path
		ArrayList<Integer> path = new ArrayList<Integer>();	// best path
		for	(int i = 1; i <= N; i++)	{
			ArrayList<Integer> aux = getPath(graph, i, hotels);
			if	( aux.size() > path.size() )	path = aux;
		}
		out.println( path.size() );
		while	( path.size() > 1 )	{
			out.print( path.remove(0) + " " );
		}
		out.println( path.get(0) );
		out.flush();
	}
	private static ArrayList<Integer> getPath(ArrayList<Integer>[] graph, int id, HashSet<Integer> hotels) {
		ArrayList<Integer> path = new ArrayList<Integer>();
	//	System.out.println( "   getting path from: " + id );
		path.add(id);
		while	( true )	{
			if	( hotels.contains(id) )	return	path;
			if	( graph[id].size() != 1 || id == graph[id].get(0) )	break;
			int aux = graph[id].get(0);
			if	( aux != id + 1 )	break;
			id = aux;
			path.add(id);
	//		System.out.println( "    " + id );
		}
		path.clear();
		return	path;
	}
}


import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;

public class Main {
	static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
	static StringTokenizer st = new StringTokenizer("");
	static String next()	throws Exception	{
		while	( !st.hasMoreTokens() )	st = new StringTokenizer( br.readLine() );
		return	st.nextToken();
	}
	public static void main(String [] asda)	throws Exception	{
		PrintWriter out = new PrintWriter( new BufferedOutputStream(System.out) );
		//
		int N = Integer.parseInt( next() );
		// save hotels
		ArrayList<Integer> hotels = new ArrayList<Integer>();
		for	(int i = 1; i <= N; i++)	{
			if	( next().equals("1") )
				hotels.add(0, i);
		}
		// read paths
		int [] graph = new int [1 + N];
		int [] pathsTo = new int [1 + N];	// pathsTo[i] = number of paths that connect with i
		for	(int i = 1; i <= N; i++)	{
			int next = Integer.parseInt( next() );
			// graph[i] = next, i -> next
			graph[i] = next;
			pathsTo[next]++;
		}
		// check for a valid path
		int max = 0, indexMax = 0;
		for	(Integer hotel : hotels)	{
			int aux = getPath(graph, pathsTo, hotel);
			if	( aux > max )	{
				max = aux;
				indexMax = hotel;
				if	( max == N )	break;
			}
		}
		out.println( max );
		out.print( indexMax );
		while	( --max > 0 )	{
			indexMax = graph[indexMax];
			out.print( " " + indexMax );
		}
		out.println();
		out.flush();
	}
	private static int getPath(int [] graph, int [] pathsTo, int id) {
		int len = 1;
		while	( true )	{
			id = graph[id];
			if	( id == 0 || pathsTo[id] != 1 )	break;
			len++;
		}
		return	len;
	}
}


import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;

public class Main {
	static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
	static StringTokenizer st = new StringTokenizer("");
	static String next()	throws Exception	{
		while	( !st.hasMoreTokens() )	st = new StringTokenizer( br.readLine() );
		return	st.nextToken();
	}
	public static void main(String [] asda)	throws Exception	{
		PrintWriter out = new PrintWriter( new BufferedOutputStream(System.out) );
		//
		int N = Integer.parseInt( next() );
		// save hotels
		ArrayList<Integer> hotels = new ArrayList<Integer>();
		for	(int i = 1; i <= N; i++)	{
			if	( next().equals("1") )
				hotels.add(0, i);
		}
		// read paths
		int [] graph = new int [1 + N];
		int [] pathsTo = new int [1 + N];	// pathsTo[i] = number of paths that connect with i
		for	(int i = 1; i <= N; i++)	{
			int next = Integer.parseInt( next() );
			// graph[i] = next, i -> next
			graph[i] = next;
			pathsTo[next]++;
		}
		// check for a valid path
		ArrayList<Integer> path = new ArrayList<Integer>();	// best path
		for	(Integer hotel : hotels)	{
			ArrayList<Integer> aux = getPath(graph, pathsTo, hotel);
			if	( aux.size() > path.size() )	path = aux;
			else if	(
				aux.size() > 0	&&
				aux.size() == path.size()	&&
				aux.get( aux.size() - 1 ) > path.get( path.size() - 1)
			)	path = aux;
		}
		out.println( path.size() );
		while	( path.size() > 1 )	{
			out.print( path.remove(0) + " " );
		}
		out.println( path.get(0) );
		out.flush();
	}
	private static ArrayList<Integer> getPath(int [] graph, int [] pathsTo, int id) {
		ArrayList<Integer> path = new ArrayList<Integer>();
	//	System.out.println( "   getting path from: " + id );
		path.add(id);
		while	( true )	{
			if	( graph[id] != id - 1 )	break;
			id = graph[id];
			if	( id == 0 || pathsTo[id] != 1 )	break;
			path.add(0, id);
	//		System.out.println( "    " + id );
		}
		return	path;
	}
}


import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;

public class Main {
	static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
	static StringTokenizer st = new StringTokenizer("");
	static String next()	throws Exception	{
		while	( !st.hasMoreTokens() )	st = new StringTokenizer( br.readLine() );
		return	st.nextToken();
	}
	public static void main(String [] asda)	throws Exception	{
		PrintWriter out = new PrintWriter( new BufferedOutputStream(System.out) );
		//
		int N = Integer.parseInt( next() );
		// save hotels
		ArrayList<Integer> hotels = new ArrayList<Integer>();
		for	(int i = 1; i <= N; i++)	{
			if	( next().equals("1") )
				hotels.add(0, i);
		}
		// read paths
		int [] graph = new int [1 + N];
		int [] pathsTo = new int [1 + N];	// pathsTo[i] = number of paths that connect with i
		for	(int i = 1; i <= N; i++)	{
			int next = Integer.parseInt( next() );
			// graph[i] = next, i -> next
			graph[i] = next;
			pathsTo[next]++;
		}
		// check for a valid path
		int max = 0, indexMax = 0;
		for	(Integer hotel : hotels)	{
			int aux = getPath(graph, pathsTo, hotel);
			if	( aux > max )	{
				max = aux;
				indexMax = hotel;
				if	( max == N )	break;
			}
		}
		out.println( max );

		Stack<Integer> stack = new Stack<Integer>();
		stack.push(indexMax);
		while	( --max > 0 )	{
			indexMax = graph[indexMax];
			stack.push(indexMax);
		}
		out.print( stack.pop() );
		while	( ! stack.isEmpty() )	out.print( " " + stack.pop() );
		out.println();
		out.flush();
	}
	private static int getPath(int [] graph, int [] pathsTo, int id) {
		int len = 1;
		while	( true )	{
			id = graph[id];
			if	( id == 0 || pathsTo[id] != 1 )	break;
			len++;
		}
		return	len;
	}
}


import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;

public class Main {
	static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
	static StringTokenizer st = new StringTokenizer("");
	static String next()	throws Exception	{
		while	( !st.hasMoreTokens() )	st = new StringTokenizer( br.readLine() );
		return	st.nextToken();
	}
	public static void main(String [] asda)	throws Exception	{
		PrintWriter out = new PrintWriter( new BufferedOutputStream(System.out) );
		//
		int N = Integer.parseInt( next() );
		ArrayList<Integer> graph [] = (ArrayList<Integer> []) new ArrayList[N + 1];
		for (int i = 1; i <= N; i++)	graph[i] = new ArrayList<Integer>();
		// save hotels
		HashSet<Integer> hotels = new HashSet<Integer>();
		for	(int i = 1; i <= N; i++)	{
			if	( next().equals("1") )
				hotels.add(i);
		}
		// read paths
		for	(int i = 1; i <= N; i++)	{
			int path = Integer.parseInt( next() );
			if	( path == 0 )	continue;
			graph[path].add(i);
		}
		// print paths
	/*	for	(int i = 1; i <= N; i++)	{
			out.print("  from: " + i + ", paths to: ");
			for (Integer v : graph[i])	out.print( v + " " );
			out.println();
		}
		*/
		// check for a valid path
		ArrayList<Integer> path = new ArrayList<Integer>();	// best path
		for	(int i = 1; i <= N; i++)	{
			ArrayList<Integer> aux = getPath(graph, i, hotels);
			if	( aux.size() > path.size() )	path = aux;
			else if	(
				aux.size() > 0	&&
				aux.size() == path.size()	&&
				aux.get( aux.size() - 1 ) > path.get( path.size() - 1)
			)	path = aux;
		}
		out.println( path.size() );
		while	( path.size() > 1 )	{
			out.print( path.remove(0) + " " );
		}
		out.println( path.get(0) );
		out.flush();
	}
	private static ArrayList<Integer> getPath(ArrayList<Integer>[] graph, int id, HashSet<Integer> hotels) {
		ArrayList<Integer> path = new ArrayList<Integer>();
	//	System.out.println( "   getting path from: " + id );
		path.add(id);
		while	( true )	{
			if	( hotels.contains(id) )	return	path;
			if	( graph[id].size() != 1 || id == graph[id].get(0) )	break;
			int aux = graph[id].get(0);
			if	( aux != id + 1 )	break;
			id = aux;
			path.add(id);
	//		System.out.println( "    " + id );
		}
		path.clear();
		return	path;
	}
}