import javafx.scene.layout.Priority;

import java.io.*;
import java.lang.reflect.Array;
import java.net.Inet4Address;
import java.util.*;
import java.lang.*;
import java.util.HashMap;
import java.util.PriorityQueue;
public class templ implements Runnable {
    static class pair implements Comparable
    {
        int f,s;
        pair(int fi,int se)
        {
            f=fi;
            s=se;
        }
        public int compareTo(Object o)
        {
            pair pr=(pair)o;
            if(f>pr.f)
                return 1;
            else
                return -1;
        }
        public boolean equals(Object o)
        {
            pair ob=(pair)o;
            int ff;
            int ss;
            if(o!=null)
            {
                ff=ob.f;
                ss=ob.s;
                if((ff==this.f)&&(ss==this.s))
                    return true;
            }
            return false;
        }
        public int hashCode()
        {
            return (this.f+" "+this.s).hashCode();
        }
    }
    public class triplet implements Comparable
    {
        int f,s;
        int t;
        triplet(int f,int s,int t)
        {
            this.f=f;
            this.s=s;
            this.t=t;
        }
        public boolean equals(Object o)
        {
            triplet ob=(triplet)o;
            int ff,ss;
            int tt;
            if(o!=null)
            {
                ff=ob.f;
                ss=ob.s;
                tt=ob.t;
                if((ff==this.f)&&(ss==this.s)&&(tt==this.t))
                    return true;
            }
            return false;
        }
        public int hashCode()
        {
            return (this.f+" "+this.s+" "+this.t).hashCode();
        }
        public int compareTo(Object o)
        {
            triplet tr=(triplet)o;
            if(f>tr.f)
                return 1;
            else if(f==tr.f)
            {
                if(s>tr.s)
                    return -1;
                else
                    return 1;
            }
            else
                return -1;
        }
    }
    ArrayList<Integer>g[];
    int child[],dp[];
    HashSet<Integer>hs=new HashSet<>();
    void dfs(int u ,int pp)
    {
        int count=0;
        int p=0;
        for(int v : g[u])
        {
            if(v!=pp)
            {
                dfs(v,u);
                p=v;
                count++;
            }
        }
        if(count>1)
            child[u]=u;
        else
            child[u]=p;
    }
    int solve(int i)
    {
        if(i==child[i])
        {
            if(hs.contains(i))
            {
                dp[i]=1;
                return 1;
            }
            else
            {
                dp[i]=-100000000;
                return -100000000;
            }
        }
        if(hs.contains(i))
        {
            dp[i]=1;
            return 1;
        }
        if(dp[i]!=-1)
            return dp[i];
        dp[i]=1+solve(child[i]);
        return dp[i];
    }
    public static void main(String args[])throws Exception
    {
        new Thread(null,new templ(),"templ",1<<27).start();
    }
    public void run()
    {
        try
        {
            InputReader in = new InputReader(System.in);
            PrintWriter out = new PrintWriter(System.out);
            int n=in.ni();
            g=new ArrayList[n+1];
            int m[]=new int[n+1];
            child=new int[n+1];
            dp=new int[n+1];
            Arrays.fill(dp,-1);
            for(int i=1;i<=n;i++)
                g[i]=new ArrayList<>();
            for(int i=1;i<=n;i++)
            {
                int x=in.ni();
                if(x==1)
                   hs.add(i);
            }
            //out.println(hs);
            for(int i=1;i<=n;i++)
            {
                int x=in.ni();
                if(x==i)
                    x=0;
                m[i]=x;
                if(x==0)
                    continue;
                g[x].add(i);
                g[i].add(x);
            }
            for(int i=1;i<=n;i++)
            {
                if(m[i]==0)
                {
                    dfs(i,0);
                }
            }
            for(int i=1;i<=n;i++)
            {
                if(dp[i]==-1)
                    solve(i);
            }
            int ans=0;
            int ind=0;
            for(int i=1;i<=n;i++)
            {
                if(dp[i]>ans)
                {
                    ans=dp[i];
                    ind=i;
                }
            }
            out.println(ans);
            while(true)
            {
                out.print(ind+" ");
                if(hs.contains(ind))
                    break;
                ind=child[ind];
            }
            out.close();
        }
        catch(Exception e){
            return;
        }
    }
    static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

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

        public int ni() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }

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

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

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

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }
}


import javafx.scene.layout.Priority;

import java.io.*;
import java.lang.reflect.Array;
import java.net.Inet4Address;
import java.util.*;
import java.lang.*;
import java.util.HashMap;
import java.util.PriorityQueue;
public class templ implements Runnable {
    static class pair implements Comparable
    {
        int f,s;
        pair(int fi,int se)
        {
            f=fi;
            s=se;
        }
        public int compareTo(Object o)
        {
            pair pr=(pair)o;
            if(f>pr.f)
                return 1;
            else
                return -1;
        }
        public boolean equals(Object o)
        {
            pair ob=(pair)o;
            int ff;
            int ss;
            if(o!=null)
            {
                ff=ob.f;
                ss=ob.s;
                if((ff==this.f)&&(ss==this.s))
                    return true;
            }
            return false;
        }
        public int hashCode()
        {
            return (this.f+" "+this.s).hashCode();
        }
    }
    public class triplet implements Comparable
    {
        int f,s;
        int t;
        triplet(int f,int s,int t)
        {
            this.f=f;
            this.s=s;
            this.t=t;
        }
        public boolean equals(Object o)
        {
            triplet ob=(triplet)o;
            int ff,ss;
            int tt;
            if(o!=null)
            {
                ff=ob.f;
                ss=ob.s;
                tt=ob.t;
                if((ff==this.f)&&(ss==this.s)&&(tt==this.t))
                    return true;
            }
            return false;
        }
        public int hashCode()
        {
            return (this.f+" "+this.s+" "+this.t).hashCode();
        }
        public int compareTo(Object o)
        {
            triplet tr=(triplet)o;
            if(f>tr.f)
                return 1;
            else if(f==tr.f)
            {
                if(s>tr.s)
                    return -1;
                else
                    return 1;
            }
            else
                return -1;
        }
    }
    HashSet<Integer>hs=new HashSet<>();
    ArrayList<Integer>g[];
    int dp[],child[];
    int p;
    int m[];
    int dfs(int u,int pp)
    {
        //System.out.println(u+" "+pp);
        if(u!=p&&hs.contains(u))
        {
            child[pp]=pp;
            dp[u]=0;
            return 0;
        }
        if(m[u]>1)
        {
            child[pp]=pp;
            dp[u]=0;
            return 0;
        }
        if(u!=p&&g[u].size()==0)
        {
            child[u]=u;
            dp[u]=1;
            return 1;
        }
        if(dp[u]!=-1)
        {
            return dp[u];
        }
        for(int v : g[u])
        {
            if(v!=pp)
            {
                child[u]=v;
                dp[u]=1+dfs(v,u);
            }
        }
        return dp[u];
    }
    public static void main(String args[])throws Exception
    {
        new Thread(null,new templ(),"templ",1<<27).start();
    }
    public void run()
    {
        try
        {
            InputReader in = new InputReader(System.in);
            PrintWriter out = new PrintWriter(System.out);
            int n=in.ni();
            g=new ArrayList[n+1];
            dp=new int[n+1];
            child=new int[n+1];
            m=new int[n+1];
            Arrays.fill(dp,-1);
            for(int i=1;i<=n;i++)
                g[i]=new ArrayList<>();
            for(int i=1;i<=n;i++)
            {
                int x=in.ni();
                if(x==1)
                    hs.add(i);
            }
            for(int i=1;i<=n;i++)
            {
                int x=in.ni();
                if(x!=0)
                {
                    g[i].add(x);
                }
            }
            for(int i=1;i<=n;i++)
            {
                for(int j : g[i])
                    m[j]++;
            }
            for(int i : hs)
            {
                p=i;
                dfs(p,0);
            }
            int ans=0;
            int ind=0;
            for(int i=1;i<=n;i++)
            {
                if(dp[i]>ans)
                {
                    ans=dp[i];
                    ind=i;
                }
            }
            if(ans!=0)
            {
                out.println(ans);
                Stack<Integer> st = new Stack<>();
                while (true) {
                    st.add(ind);
                    if (ind == child[ind])
                        break;
                    ind = child[ind];
                }
                while (!st.isEmpty())
                    out.print(st.pop() + " ");
            }
            if(ans==0)
            {
                out.println(1);
                out.println(p);
            }
            out.close();
        }
        catch(Exception e){
            return;
        }
    }
    static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

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

        public int ni() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }

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

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

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

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CF_350B solver = new CF_350B();
        solver.solve(1, in, out);
        out.close();
    }

    static class CF_350B {
        public void solve(int testNumber, Scanner input, PrintWriter out) {
            ShortScanner in = new ShortScanner(input);
            int n = in.i();
            boolean[] hotel = new boolean[n];
            for (int i = 0; i < n; ++i) hotel[i] = (in.i() == 1);
            int[] prev = new int[n], deg = new int[n];
            Arrays.fill(prev, -1);
            for (int i = 0; i < n; ++i) {
                int x = in.i() - 1;
                if (x < 0) continue;
                prev[i] = x;
                deg[x]++;
            }

            Object[] rs = {};
            List<Integer> tmp = new ArrayList<>();
            for (int i = 0; i < n; ++i) {
                if (!hotel[i]) continue;
                int cur = i;
                tmp.clear();
                tmp.add(cur + 1);
                while (prev[cur] >= 0 && deg[prev[cur]] == 1 && !hotel[prev[cur]]) {
                    tmp.add(prev[cur] + 1);
                    cur = prev[cur];
                }
                if (tmp.size() > rs.length)
                    rs = tmp.toArray();
            }

            out.println(rs.length);
            for (int i = rs.length - 1; i >= 0; --i) out.print(rs[i] + " ");
        }

        class ShortScanner {
            Scanner in;

            ShortScanner(Scanner in) {
                this.in = in;
            }

            int i() {
                return in.nextInt();
            }

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CF_350B solver = new CF_350B();
        solver.solve(1, in, out);
        out.close();
    }

    static class CF_350B {
        public void solve(int testNumber, Scanner input, PrintWriter out) {
            ShortScanner in = new ShortScanner(input);
            int n = in.i();
            boolean[] hotel = new boolean[n];
            for (int i = 0; i < n; ++i) hotel[i] = (in.i() == 1);
            int[] prev = new int[n], deg = new int[n];
            for (int i = 0; i < n; ++i) {
                int x = in.i() - 1;
                if (x < 0) continue;
                prev[i] = x;
                deg[x]++;
            }

            prev[0] = -1;
            Object[] rs = {};
            List<Integer> tmp = new ArrayList<>();
            for (int i = 0; i < n; ++i) {
                if (!hotel[i]) continue;
                int cur = i;
                tmp.clear();
                tmp.add(cur + 1);
                while (prev[cur] >= 0 && deg[prev[cur]] == 1) {
                    tmp.add(prev[cur] + 1);
                    cur = prev[cur];
                }
                if (tmp.size() > rs.length)
                    rs = tmp.toArray();
            }

            out.println(rs.length);
            for (int i = rs.length - 1; i >= 0; --i) out.print(rs[i] + " ");
        }

        class ShortScanner {
            Scanner in;

            ShortScanner(Scanner in) {
                this.in = in;
            }

            int i() {
                return in.nextInt();
            }

        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Main {

    IIO io;

    Main(IIO io) {
        this.io = io;
    }

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

        Main m = new Main(new ConsoleIO());m.solve();

//        Tester test = new Tester();test.run();
    }

    public void solve() {
        int n = io.readInt();
        int[] nodes = io.readIntArray();
        int[] edges = io.readIntArray();
        int[] deg = new int[n + 1];
        for (int i = 0; i < n; i++)
            if (edges[i] > 0)
                deg[edges[i] - 1]++;
        int max = 0;
        int end = -1;
        for (int i = 0; i < n; i++)
            if (nodes[i] == 1) {
                int k = i;
                int sum = 1;
                for (; ; ) {
                    int p = edges[k];
                    if (p == 0 || deg[p - 1] > 1)
                        break;
                    sum++;
                    k = p - 1;
                }
                if (sum > max) {
                    max = sum;
                    end = i;
                }
            }
        int[] path = new int[max];
        for (int q = end, i = max - 1; i >= 0; i--) {
            path[i] = q + 1;
            q = edges[q] - 1;
        }
        io.writeLine(Integer.toString(max));
        for (int i = 0; i < max; i++)
            io.writeInt(path[i]);
    }
}

class ConsoleIO extends BaseIO {
    BufferedReader br;

    public ConsoleIO(){
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public void writeLine(String s) {
        System.out.println(s);
    }

    public void writeInt(int a) {
        System.out.print(a);
        System.out.print(' ');
    }

    public String readLine() {
        try {
            return br.readLine();
        }
        catch (Exception ex){
            return "";
        }
    }
}
abstract class BaseIO implements IIO {

    public int readInt() {
        return Integer.parseInt(this.readLine());
    }

    public long readLong() {
        return Long.parseLong(this.readLine());
    }

    public int[] readIntArray() {
        String line = this.readLine();
        String[] nums = line.split(" ");
        int[] res = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            res[i] = Integer.parseInt(nums[i]);
        }
        return res;
    }

    public long[] readLongArray() {
        String line = this.readLine();
        String[] nums = line.split(" ");
        long[] res = new long[nums.length];
        for (int i = 0; i < nums.length; i++) {
            res[i] = Long.parseLong(nums[i]);
        }
        return res;
    }
}
interface IIO {
    void writeLine(String s);
    void writeInt(int a);
    String readLine();
    int readInt();
    long readLong();
    int[] readIntArray();
    long[] readLongArray();
}


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

public class Main {

    static InputReader in = new InputReader(System.in);
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        int n = in.nextInt();
        int[] types = new int[n];

        for (int i = 0; i < n; i++) {
            types[i] = in.nextInt();
        }

        int[] prev = new int[n];
        int[] count = new int[n];

        for (int i = 0; i < n; i++) {
            prev[i] = in.nextInt() - 1;
            if (prev[i] != -1) {
                count[prev[i]]++;
            }
        }

        int max = 0;
        int maxindex = 0;

        for (int i = 0; i < n; i++) {
            if (types[i] > 0) {
                int counter = 1;
                int counterindex = i;
                while (true) {
                    int next = prev[counterindex];
                    if (next == -1 || types[next] == 1 || count[next] > 1) {
                        break;
                    }
                    counterindex = next;
                    counter++;
                }
                if (counter > max) {
                    max = counter;
                    maxindex = i;
                }
            }
        }

        int[] data = new int[max];
        data[max - 1] = maxindex;

        for (int i = max - 2; i >= 0; i--) {
            data[i] = prev[data[i + 1]];
        }

        out.println(max);
        for (int i = 0; i < max; i++) {
            out.print((1 + data[i]) + " ");
        }
        out.println();
        out.close();
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            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());
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Stack;

/*
Name: Abdulaziz Waleed Alshememry
ID: 1537869
E-Mail: azzoz_4_ever@outlook.com
Date: *
 */
public class Main {

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader input = new InputReader(inputStream);
        PrintWriter output = new PrintWriter(outputStream);
        int n = input.nextInt();
        int[] link = new int[n + 1];
        int[] occurs = new int[n + 1];
        ArrayList<Integer> indexOfH = new ArrayList<>();

        for (int i = 1; i <= n; i++) {
            if (input.nextInt() == 1) {
                indexOfH.add(i);
            }
        }
        for (int i = 1; i <= n; i++) {
            link[i] = input.nextInt();
            occurs[link[i]]++;
        }
        Stack<Integer>[] allRoads = new Stack[indexOfH.size()];

        int biggestSize = 0;
        for (int i = 0; i < indexOfH.size(); i++) {
            allRoads[i] = new Stack<>();
            allRoads[i].push(indexOfH.get(i));
            while (true) {
                if (link[allRoads[i].peek()] == 0 || occurs[link[allRoads[i].peek()]] > 1) {
                    break;
                } else {
                    allRoads[i].push(link[allRoads[i].peek()]);
                }
            }
            if (allRoads[i].size() > allRoads[biggestSize].size()) {
                biggestSize = i;
            }
        }
        output.println(allRoads[biggestSize].size());
        while (!allRoads[biggestSize].isEmpty()) {
            output.print(allRoads[biggestSize].pop() + " ");
        }
        output.close();
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            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());
        }

    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.Queue;
import java.util.Stack;

/*
Name: Abdulaziz Waleed Alshememry
ID: 1537869
E-Mail: azzoz_4_ever@outlook.com
Date: *
 */
public class Main {

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader input = new InputReader(inputStream);
        PrintWriter output = new PrintWriter(outputStream);
        int n = input.nextInt();
        int[] link = new int[n + 1];
        int[] occurs = new int[n + 1];
        ArrayList<Integer> indexOfH = new ArrayList<>();

        for (int i = 1; i <= n; i++) {
            if (input.nextInt() == 1) {
                indexOfH.add(i);
            }
        }
        for (int i = 1; i <= n; i++) {
            link[i] = input.nextInt();
            occurs[link[i]]++;
        }
        Stack<Integer>[] allRoads = new Stack[indexOfH.size()];

        int biggestSize = 0;
        for (int i = 0; i < indexOfH.size(); i++) {
            allRoads[i] = new Stack<>();
            allRoads[i].push(indexOfH.get(i));
            while (true) {
                if (link[allRoads[i].peek()] == 0 || occurs[link[allRoads[i].peek()]] > 1) {
                    break;
                } else {
                    allRoads[i].push(link[allRoads[i].peek()]);
                }
            }
            if (allRoads[i].size() > allRoads[biggestSize].size()) {
                biggestSize = i;
            }
        }
        output.println(allRoads[biggestSize].size());
        while (!allRoads[biggestSize].isEmpty()) {
            output.print(allRoads[biggestSize].pop() + " ");
        }
        output.close();
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            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());
        }

    }

}


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

public class Hello {
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

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

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

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

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	public static void main(String[] args) {
		FastReader in = new FastReader();

		// start code here
		int n = in.nextInt();
		int type[] = new int[n];
		for (int i = 0; i < n; ++i)
			type[i] = in.nextInt();
		int a[] = new int[n];
		int deg[] = new int[n];
		for (int i = 0; i < n; ++i) {
			a[i] = in.nextInt() - 1;
			if (a[i] != -1)
				++deg[a[i]];
		}
		LinkedList<Integer> ll = new LinkedList<Integer>();
		for (int i = 0; i < n; ++i) {
			if (type[i] == 1) {
				LinkedList<Integer> nl = new LinkedList<Integer>();
				nl.addFirst(i);// using addFirst to avoid reversing LinkedList at the end
				int temp = i;
				while (a[temp] >= 0 && type[a[temp]] == 0 && deg[a[temp]] == 1) {
					nl.addFirst(a[temp]);
					temp = a[temp];
				}
				if (nl.size() > ll.size())
					ll = nl;
			}
		}
		System.out.println(ll.size());
		for (int i : ll) {
			System.out.print((i + 1) + " ");
		}
	}
}


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

public class Resort {
    InputStream is;
    PrintWriter out;
    String INPUT = "";

    void solve() throws IOException {
        int n=ni();
        boolean[] mountain= new boolean[n+1];
        for (int i = 1; i <=n; i++)
            mountain[i]= ni() ==0;

        int[] parent= new int[n+1];
        int[] outdegree= new int[n+1];
        for (int i = 1; i <=n; i++)
        {
            parent[i]= ni();
            outdegree[parent[i]]++;
        }

        ArrayList<ArrayList<Integer>> list= new ArrayList<>();
        for (int i = 0; i <=n; i++)
            list.add(new ArrayList<>());

        int[] indegree= new int[n+1];
        for (int i = 1; i <=n; i++)
        {
            if(outdegree[parent[i]]>1 || outdegree[i]>2 || parent[i]==0) continue;
            list.get(parent[i]).add(i);
            indegree[i]++;
        }

//        out.println(list);
//        for (int i = 1; i <=n; i++)
//            out.print(outdegree[i]+" ");
//        out.println();
//
//        for (int i = 1; i <=n; i++)
//            out.print(indegree[i]+" ");
//        out.println();

        ArrayList<Integer> ans= new ArrayList<>();
        for(int i=1;i<=n;i++)
        {
            if(indegree[i]==0 && outdegree[i]<=1)
            {
                ArrayList<Integer> temp= new ArrayList<>();
                DFSREC(list, i, temp);

                if(!mountain[temp.get(temp.size()-1)] && ans.size()<temp.size())
                {
                    ans= temp;
//                    out.println(ans);
                }
            }
        }
        out.println(ans.size());
        for(int i: ans)
            out.print(i+" ");
        out.println();
    }

    private void DFSREC(ArrayList<ArrayList<Integer>> list, int curr, ArrayList<Integer> temp)
    {
        temp.add(curr);

        for(int i: list.get(curr))
            DFSREC(list,  i, temp);
    }

    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);

        solve();
        out.flush();
    }

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

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private void tr(Object... o) {
        if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
    }
}


// Name: William James
// Date: Feb 7, 2019
// Description:

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

public class Main {

    public static void main(String[] args) throws Exception {
        in.init(System.in);
        PrintWriter out = new PrintWriter(System.out);

        boolean debug = false;

        int numObjects = in.nextInt();
        int[] objects = new int[numObjects + 1];
        int[] paths = new int[numObjects + 1];
        for (int i = 1; i < numObjects + 1; i++) {
            objects[i] = in.nextInt();
            if (debug) {
                out.println(objects[i]);
            }
        }
        if (debug) {
            out.println("Printing paths...");
        }
        for (int i = 1; i < numObjects + 1; i++) {
            int number = in.nextInt();
            if (number != 0) {
                paths[number]++;
                if (debug) {
                    out.println(paths[number]);
                }
            } else if (debug) {
                out.println(paths[0]);
            }

        }
        int streak = 0;
        int longestStreak = 0;
        int longestStart = 0;
        int longestFinish = 0;
        for (int i = 1; i < objects.length; i++) {
            if (debug) {
                out.println("There are " + (objects.length - 1) + " objects to check.");
                out.println("Currently checking position " + i);
                out.println("Object type for position " + i + ": " + objects[i]);
                out.println("Number paths for position " + i + ": " + paths[i]);
                out.println("Streak: " + streak);
                out.println("Longest streak: " + longestStreak);
                out.println("Longest start: " + longestStart);
                out.println("Longest finish: " + longestFinish);
                out.println();
            }
            if (paths[i] == 1) {
                streak++;
                continue;
            } else if (objects[i] == 1) {
                streak++;
                if (debug) {
                    out.println("We are here.");
                }
                if (streak > longestStreak) {
                    longestFinish = i + 1;
                    longestStart = longestFinish - streak;
                    longestStreak = streak;
                    streak = 0;
                }
            } else {
                streak = 0;
                continue;
            }

        }
        out.println(longestStreak);
        if (debug) {
            out.println("Longest start: " + longestStart);
            out.println("Longest finish: " + longestFinish);
        }

        for (int i = longestStart; i < longestFinish; i++) {
            out.print(i + " ");
        }

        out.println();
        out.close();
    }

}

/**
 * Class for buffered reading integer and double values
 */
class in {

    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

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

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

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

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

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


// Name: William James
// Date: Feb 7, 2019
// Description:

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

public class Main {

    public static void main(String[] args) throws Exception {
        in.init(System.in);
        PrintWriter out = new PrintWriter(System.out);

        boolean debug = false;

        int numObjects = in.nextInt();
        int[] objects = new int[numObjects + 1];
        int[] arr = new int[numObjects + 1];
        int[] paths = new int[numObjects + 1];
        for (int i = 1; i < numObjects + 1; i++) {
            objects[i] = in.nextInt();
            if (debug) {
                out.println(objects[i]);
            }
        }
        if (debug) {
            out.println("Printing paths...");
        }
        for (int i = 1; i < numObjects + 1; i++) {
            int number = in.nextInt();
            arr[i] = number;
            paths[number]++;

        }
        int streak = 0;
        int longestStreak = 0;
        int holder = 0;
        for (int i = numObjects; i >= 1; --i) {
            if (debug) {
                out.println("There are " + (objects.length - 1) + " objects to check.");
                out.println("Currently checking position " + i);
                out.println("Object type for position " + i + ": " + objects[i]);
                out.println("Number paths for position " + i + ": " + paths[i]);
                out.println("Streak: " + streak);
                out.println("Longest streak: " + longestStreak);
                out.println();
            }
            if (objects[i] == 1) {
                int start = i;
                streak = 1;
                while (arr[start] != 0 && paths[arr[start]] <= 1) {
                    start = arr[start];
                    streak++;
                }
                if (streak > longestStreak) {
                    longestStreak = streak;
                    holder = i;
                }
            }

        }

//        if (debug) {
//            out.println("Longest start: " + longestStart);
//            out.println("Longest finish: " + longestFinish);
//        }
//        out.println(longestStreak);
//        for (int i = longestStart; i < longestFinish; i++) {
//            out.print(i + " ");
//        }
        out.println(longestStreak);
        int count = 0;
        int[] output = new int[numObjects];
        while (count < longestStreak) {
            output[count++] = holder;
            holder = arr[holder];
        }
        for (int i = longestStreak - 1; i >= 0; i--) {
            out.print(output[i] + " ");
        }

        out.println();
        out.close();
    }

}

/**
 * Class for buffered reading integer and double values
 */
class in {

    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

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

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

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

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

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


//package c203;

import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class q2 {

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int n=s.nextInt();
        int tmans=-1;

        boolean m[]=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {
            if(s.nextInt()==0)
                m[i]=true;
            else{
                if(tmans==-1)
                    tmans=i;
                m[i]=false;
            }
            }

        int[] frq=new int[n+1];
        int[] des=new int[n+1];
        for(int i=1;i<=n;i++)
        {
            int tmp=s.nextInt();
            frq[tmp]++;
            des[tmp]=i;
        }
        ArrayList<Integer> t=new ArrayList<Integer>();
        ArrayList<Integer> ans=new ArrayList<>();
        ArrayList<Integer> a1=new ArrayList<Integer>();
        boolean [] b=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {//System.out.println(i);
            if(frq[i]==1 && !b[i])
            {
                int j=i;
                ans.add(i);
                for(j=des[i];m[j]!=false;j=des[j])
                {//System.out.println(j+" asas");
                    if(j==i)
                        break;
                    b[j]=true;
                    if(frq[j]==1)
                        {
                        //System.out.println(j);
                        ans.add(j);
                        }
                    else
                        {
                        ans=new ArrayList<>();
                        break;
                        }
                }
                //System.out.println(j);
                if(!m[j])
                    ans.add(j);
                else
                    ans=new ArrayList<Integer>();

                if(ans.size()>a1.size())
                {
                    a1=ans;
                }
                ans=new ArrayList<Integer>();
            }
        }
//      for(int i=1;i<=n;i++)
//      {
//          //System.out.println(des[i]+" "+frq[i]);
//          if(frq[i]==1)
//              t.add(i);
//      }
//      //System.out.println(t);
//      while(!t.isEmpty())
//      {
//          int tmp=t.get(0);
//
//          t.remove((Integer)tmp);
//          ans.add(tmp);
//          while(m[des[tmp]] && !t.isEmpty())
//          {
//              //System.out.println(tmp+"Sd");
//
//                  if(m[des[tmp]])
//                      {
//                      if(t.contains(des[tmp]))
//                          {
//                          ans.add(des[tmp]);
//
//                      tmp=des[tmp];
//                      t.remove((Integer)tmp);
//                      }
//                      else
//                          break;
//                      }
//                  else
//                  {
//                      ans.add(des[tmp]);
//                      if(ans.size()>a1.size())
//                          {
//                          a1=ans;
//                          ans=new ArrayList<>();
//                          }
//                  }
//          }
//          if(!m[des[tmp]])
//          {
//          ans.add(des[tmp]);
//          if(ans.size()>a1.size())
//              {
//              a1=ans;
//              ans=new ArrayList<>();
//              }
//          }
//          ans=new ArrayList<>();
//      }
        System.out.println(a1.size()==0?"1 \n"+tmans:a1.size());
        StringBuilder st=new StringBuilder();
        for(int i=0;i<a1.size();i++)
        st.append(a1.get(i)+" ");

        System.out.println(st);
    }
}

class FasterScanner {
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

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

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

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

    public long nextLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long 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 int nextInt() {
        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 int[] nextIntArray(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    public long[] nextLongArray(int n) {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }

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

    private boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
}


//package c203;

import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class q2 {

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int n=s.nextInt();
        int tmans=-1;

        boolean m[]=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {
            if(s.nextInt()==0)
                m[i]=true;
            else{
                if(tmans==-1)
                    tmans=i;
                m[i]=false;
            }
            }

        int[] frq=new int[n+1];
        int[] des=new int[n+1];
        for(int i=1;i<=n;i++)
        {
            int tmp=s.nextInt();
            frq[tmp]++;
            des[tmp]=i;
        }
        ArrayList<Integer> t=new ArrayList<Integer>();
        ArrayList<Integer> ans=new ArrayList<>();
        ArrayList<Integer> a1=new ArrayList<Integer>();
        boolean [] b=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {//System.out.println(i);
            if(frq[i]==1 && !b[i])
            {
                int j=i;
                for(j=i;m[j]!=false;j=des[j])
                {//System.out.println(j+" asas");
                    b[j]=true;
                    if(frq[j]==1)
                        ans.add(j);
                    else
                        {
                        ans=new ArrayList<>();
                        break;
                        }
                }
                //System.out.println(j);
                if(!m[j])
                    ans.add(j);
                else
                    ans=new ArrayList<Integer>();

                if(ans.size()>a1.size())
                {
                    a1=ans;
                }
                ans=new ArrayList<Integer>();
            }
        }
//      for(int i=1;i<=n;i++)
//      {
//          //System.out.println(des[i]+" "+frq[i]);
//          if(frq[i]==1)
//              t.add(i);
//      }
//      //System.out.println(t);
//      while(!t.isEmpty())
//      {
//          int tmp=t.get(0);
//
//          t.remove((Integer)tmp);
//          ans.add(tmp);
//          while(m[des[tmp]] && !t.isEmpty())
//          {
//              //System.out.println(tmp+"Sd");
//
//                  if(m[des[tmp]])
//                      {
//                      if(t.contains(des[tmp]))
//                          {
//                          ans.add(des[tmp]);
//
//                      tmp=des[tmp];
//                      t.remove((Integer)tmp);
//                      }
//                      else
//                          break;
//                      }
//                  else
//                  {
//                      ans.add(des[tmp]);
//                      if(ans.size()>a1.size())
//                          {
//                          a1=ans;
//                          ans=new ArrayList<>();
//                          }
//                  }
//          }
//          if(!m[des[tmp]])
//          {
//          ans.add(des[tmp]);
//          if(ans.size()>a1.size())
//              {
//              a1=ans;
//              ans=new ArrayList<>();
//              }
//          }
//          ans=new ArrayList<>();
//      }
        System.out.println(a1.size()==0?"1 \n"+tmans:a1.size());
        StringBuilder st=new StringBuilder();
        for(int i=0;i<a1.size();i++)
        st.append(a1.get(i)+" ");

        System.out.println(st);
    }
}

class FasterScanner {
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

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

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

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

    public long nextLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long 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 int nextInt() {
        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 int[] nextIntArray(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    public long[] nextLongArray(int n) {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }

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

    private boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
}


//package c203;

import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class q2 {

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int n=s.nextInt();
        int tmans=-1;

        boolean m[]=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {
            if(s.nextInt()==0)
                m[i]=true;
            else{
                if(tmans==-1)
                    tmans=i;
                m[i]=false;
            }
            }

        int[] frq=new int[n+1];
        int[] des=new int[n+1];
        for(int i=1;i<=n;i++)
        {
            int tmp=s.nextInt();
            frq[tmp]++;
            des[tmp]=i;
        }
        ArrayList<Integer> t=new ArrayList<Integer>();
        ArrayList<Integer> ans=new ArrayList<>();
        ArrayList<Integer> a1=new ArrayList<Integer>();
        boolean [] b=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {//System.out.println(i);
            if(frq[i]==1 && !b[i])
            {
                int j=i;
                for(j=i;m[j]!=false;j=des[j])
                {//System.out.println(j+" asas");
                    if(b[j])
                        break;
                    b[j]=true;
                    if(frq[j]==1)
                        {
                        //System.out.println(j);
                        ans.add(j);
                        }
                    else
                        {
                        ans=new ArrayList<>();
                        break;
                        }
                }
                //System.out.println(j);
                if(!m[j])
                    ans.add(j);
                else
                    ans=new ArrayList<Integer>();

                if(ans.size()>a1.size())
                {
                    a1=ans;
                }
                ans=new ArrayList<Integer>();
            }
        }
//      for(int i=1;i<=n;i++)
//      {
//          //System.out.println(des[i]+" "+frq[i]);
//          if(frq[i]==1)
//              t.add(i);
//      }
//      //System.out.println(t);
//      while(!t.isEmpty())
//      {
//          int tmp=t.get(0);
//
//          t.remove((Integer)tmp);
//          ans.add(tmp);
//          while(m[des[tmp]] && !t.isEmpty())
//          {
//              //System.out.println(tmp+"Sd");
//
//                  if(m[des[tmp]])
//                      {
//                      if(t.contains(des[tmp]))
//                          {
//                          ans.add(des[tmp]);
//
//                      tmp=des[tmp];
//                      t.remove((Integer)tmp);
//                      }
//                      else
//                          break;
//                      }
//                  else
//                  {
//                      ans.add(des[tmp]);
//                      if(ans.size()>a1.size())
//                          {
//                          a1=ans;
//                          ans=new ArrayList<>();
//                          }
//                  }
//          }
//          if(!m[des[tmp]])
//          {
//          ans.add(des[tmp]);
//          if(ans.size()>a1.size())
//              {
//              a1=ans;
//              ans=new ArrayList<>();
//              }
//          }
//          ans=new ArrayList<>();
//      }
        System.out.println(a1.size()==0?"1 \n"+tmans:a1.size());
        StringBuilder st=new StringBuilder();
        for(int i=0;i<a1.size();i++)
        st.append(a1.get(i)+" ");

        System.out.println(st);
    }
}

class FasterScanner {
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

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

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

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

    public long nextLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long 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 int nextInt() {
        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 int[] nextIntArray(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    public long[] nextLongArray(int n) {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }

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

    private boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
}


//package c203;

import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class q2 {

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int n=s.nextInt();
        int tmans=-1;

        boolean m[]=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {
            if(s.nextInt()==0)
                m[i]=true;
            else{
                if(tmans==-1)
                    tmans=i;
                m[i]=false;
            }
            }

        int[] frq=new int[n+1];
        int[] des=new int[n+1];
        for(int i=1;i<=n;i++)
        {
            int tmp=s.nextInt();
            frq[tmp]++;
            des[tmp]=i;
        }
        ArrayList<Integer> t=new ArrayList<Integer>();
        ArrayList<Integer> ans=new ArrayList<>();
        ArrayList<Integer> a1=new ArrayList<Integer>();
        boolean [] b=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {//System.out.println(i);
            if(frq[i]==1 && !b[i])
            {
                int j=i;
                for(j=i;m[j]!=false;j=des[j])
                {//System.out.println(j+" asas");
                    b[j]=true;
                    if(frq[j]==1)
                        ans.add(j);
                    else
                        break;
                }
                //System.out.println(j);
                if(!m[j] && frq[j]==1)
                    ans.add(j);
                else
                    ans=new ArrayList<Integer>();

                if(ans.size()>a1.size())
                {
                    a1=ans;
                }
                ans=new ArrayList<Integer>();
            }
        }
//      for(int i=1;i<=n;i++)
//      {
//          //System.out.println(des[i]+" "+frq[i]);
//          if(frq[i]==1)
//              t.add(i);
//      }
//      //System.out.println(t);
//      while(!t.isEmpty())
//      {
//          int tmp=t.get(0);
//
//          t.remove((Integer)tmp);
//          ans.add(tmp);
//          while(m[des[tmp]] && !t.isEmpty())
//          {
//              //System.out.println(tmp+"Sd");
//
//                  if(m[des[tmp]])
//                      {
//                      if(t.contains(des[tmp]))
//                          {
//                          ans.add(des[tmp]);
//
//                      tmp=des[tmp];
//                      t.remove((Integer)tmp);
//                      }
//                      else
//                          break;
//                      }
//                  else
//                  {
//                      ans.add(des[tmp]);
//                      if(ans.size()>a1.size())
//                          {
//                          a1=ans;
//                          ans=new ArrayList<>();
//                          }
//                  }
//          }
//          if(!m[des[tmp]])
//          {
//          ans.add(des[tmp]);
//          if(ans.size()>a1.size())
//              {
//              a1=ans;
//              ans=new ArrayList<>();
//              }
//          }
//          ans=new ArrayList<>();
//      }
        System.out.println(a1.size()==0?"1 \n"+tmans:a1.size());
        StringBuilder st=new StringBuilder();
        for(int i=0;i<a1.size();i++)
        st.append(a1.get(i)+" ");

        System.out.println(st);
    }
}

class FasterScanner {
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

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

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

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

    public long nextLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long 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 int nextInt() {
        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 int[] nextIntArray(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    public long[] nextLongArray(int n) {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }

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

    private boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
}


//package c203;

import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class q2 {

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int n=s.nextInt();
        int tmans=-1;

        boolean m[]=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {
            if(s.nextInt()==0)
                m[i]=true;
            else{
                if(tmans==-1)
                    tmans=i;
                m[i]=false;
            }
            }

        int[] frq=new int[n+1];
        int[] des=new int[n+1];
        for(int i=1;i<=n;i++)
        {
            int tmp=s.nextInt();
            frq[tmp]++;
            des[tmp]=i;
        }
        ArrayList<Integer> t=new ArrayList<Integer>();
        ArrayList<Integer> ans=new ArrayList<>();
        ArrayList<Integer> a1=new ArrayList<Integer>();
        boolean [] b=new boolean[n+1];
        for(int i=1;i<=n;i++)
        {//System.out.println(i);
            if(frq[i]==1)
            {
                int j=i;
                for(j=i;m[j]!=false;j=des[j])
                {//System.out.println(j+" asas");
                    if(b[j])
                        break;
                    b[j]=true;
                    if(frq[j]==1)
                        {
                        //System.out.println(j);
                        ans.add(j);
                        }
                    else
                        {
                        ans=new ArrayList<>();
                        break;
                        }
                }
                b=new boolean[n+1];
                //System.out.println(j);
                if(!m[j])
                    ans.add(j);
                else
                    ans=new ArrayList<Integer>();

                if(ans.size()>a1.size())
                {
                    a1=ans;
                }
                ans=new ArrayList<Integer>();
            }
        }
//      for(int i=1;i<=n;i++)
//      {
//          //System.out.println(des[i]+" "+frq[i]);
//          if(frq[i]==1)
//              t.add(i);
//      }
//      //System.out.println(t);
//      while(!t.isEmpty())
//      {
//          int tmp=t.get(0);
//
//          t.remove((Integer)tmp);
//          ans.add(tmp);
//          while(m[des[tmp]] && !t.isEmpty())
//          {
//              //System.out.println(tmp+"Sd");
//
//                  if(m[des[tmp]])
//                      {
//                      if(t.contains(des[tmp]))
//                          {
//                          ans.add(des[tmp]);
//
//                      tmp=des[tmp];
//                      t.remove((Integer)tmp);
//                      }
//                      else
//                          break;
//                      }
//                  else
//                  {
//                      ans.add(des[tmp]);
//                      if(ans.size()>a1.size())
//                          {
//                          a1=ans;
//                          ans=new ArrayList<>();
//                          }
//                  }
//          }
//          if(!m[des[tmp]])
//          {
//          ans.add(des[tmp]);
//          if(ans.size()>a1.size())
//              {
//              a1=ans;
//              ans=new ArrayList<>();
//              }
//          }
//          ans=new ArrayList<>();
//      }
        System.out.println(a1.size()==0?"1 \n"+tmans:a1.size());
        StringBuilder st=new StringBuilder();
        for(int i=0;i<a1.size();i++)
        st.append(a1.get(i)+" ");

        System.out.println(st);
    }
}

class FasterScanner {
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

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

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

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

    public long nextLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long 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 int nextInt() {
        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 int[] nextIntArray(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    public long[] nextLongArray(int n) {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }

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

    private boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
}


import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> objeto = new ArrayList<Integer>();
		int[] previous = new int[n];
		int[] outDegree = new int[n];
		for(int i = 0; i < n; i++) {
			outDegree[i] = 0;
		}
		ArrayList<Integer> hoteles = new ArrayList<Integer>();
		for(int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			objeto.add(i, tmp);
			if(objeto.get(i) == 1) {
				hoteles.add(i);
			}
		}
		for(int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			if(tmp == 0) {
				previous[i] = -1;
			}
			else {
				previous[i] = tmp - 1;
				outDegree[tmp - 1]++;
			}
		}
		ArrayList<Integer> bestPath = new ArrayList<Integer>();
		for(int i = 0; i < hoteles.size(); i++) {
			ArrayList<Integer> currentPath = new ArrayList<Integer>();
			int start = hoteles.get(i);
			currentPath.add(start);
			int current = previous[hoteles.get(i)];
			while(current >= 0 && objeto.get(current) == 0 && outDegree[current] <= 1) {
				currentPath.add(current);
				current = previous[current];
			}
			if(currentPath.size() > bestPath.size()) {
				bestPath = currentPath;
			}
		}
		System.out.println(bestPath.size());
		for(int j = bestPath.size() - 1; j >= 0; j--) {
			System.out.println(1 + bestPath.get(j));
		}
		System.out.println();
	}
}
// 1533938876162


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

public class ProblemB {
    InputReader in; PrintWriter out;

    void solve() {
        int n = in.nextInt();
        boolean[] type = new boolean[n];
        for (int i = 0; i < n; i++) {
            int cur = in.nextInt();
            if (cur > 0) {
                type[i] = true;
            }
        }

        boolean[] used = new boolean[n];
        boolean[] duplicate = new boolean[n];

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            int cur = in.nextInt() - 1;
            if (cur >= 0) {
                if (used[cur]) {
                    duplicate[cur] = true;
                }
                a[i] = cur;

                used[cur] = true;
            }
        }

        List<Integer> bestPath = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            if (type[i]) {
                List<Integer> curPath = new ArrayList<Integer>();
                int cur = i;
                while (cur >= 0 && !duplicate[cur]) {
                    curPath.add(cur);
                    cur = a[cur];
                }
                if (bestPath.size() < curPath.size()) {
                    bestPath = curPath;
                }
            }
        }

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

    ProblemB(){
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        try {
            if (oj) {
                in = new InputReader(System.in);
                out = new PrintWriter(System.out);
            }
            else {
                Writer w = new FileWriter("output.txt");
                in = new InputReader(new FileReader("input.txt"));
                out = new PrintWriter(w);
            }
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
        solve();
        out.close();
    }
    public static void main(String[] args){
        new ProblemB();
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

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

    public InputReader(FileReader fr) {
        reader = new BufferedReader(fr);
        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 long nextLong() {
        return Long.parseLong(next());
    }
    public double nextDouble() {
        return Double.parseDouble(next());
    }

}


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

public class ProblemB {
    InputReader in; PrintWriter out;

    void solve() {
        int n = in.nextInt();
        boolean[] type = new boolean[n];
        for (int i = 0; i < n; i++) {
            int cur = in.nextInt();
            if (cur > 0) {
                type[i] = true;
            }
        }

        boolean[] used = new boolean[n];
        boolean[] duplicate = new boolean[n];

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            int cur = in.nextInt() - 1;
            if (cur >= 0) {
                if (used[cur]) {
                    duplicate[cur] = true;
                }
                used[cur] = true;
            }
            a[i] = cur;
        }

        List<Integer> bestPath = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            if (type[i]) {
                List<Integer> curPath = new ArrayList<Integer>();
                int cur = i;
                while (cur >= 0 && !duplicate[cur]) {
                    curPath.add(cur);
                    cur = a[cur];
                }
                if (bestPath.size() < curPath.size()) {
                    bestPath = curPath;
                }
            }
        }

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

    ProblemB(){
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        try {
            if (oj) {
                in = new InputReader(System.in);
                out = new PrintWriter(System.out);
            }
            else {
                Writer w = new FileWriter("output.txt");
                in = new InputReader(new FileReader("input.txt"));
                out = new PrintWriter(w);
            }
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
        solve();
        out.close();
    }
    public static void main(String[] args){
        new ProblemB();
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

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

    public InputReader(FileReader fr) {
        reader = new BufferedReader(fr);
        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 long nextLong() {
        return Long.parseLong(next());
    }
    public double nextDouble() {
        return Double.parseDouble(next());
    }

}


import javafx.scene.layout.Priority;

import java.io.*;
import java.lang.reflect.Array;
import java.net.Inet4Address;
import java.util.*;
import java.lang.*;
import java.util.HashMap;
import java.util.PriorityQueue;
public class templ implements Runnable {
    static class pair implements Comparable
    {
        int f,s;
        pair(int fi,int se)
        {
            f=fi;
            s=se;
        }
        public int compareTo(Object o)
        {
            pair pr=(pair)o;
            if(f>pr.f)
                return 1;
            else
                return -1;
        }
        public boolean equals(Object o)
        {
            pair ob=(pair)o;
            int ff;
            int ss;
            if(o!=null)
            {
                ff=ob.f;
                ss=ob.s;
                if((ff==this.f)&&(ss==this.s))
                    return true;
            }
            return false;
        }
        public int hashCode()
        {
            return (this.f+" "+this.s).hashCode();
        }
    }
    public class triplet implements Comparable
    {
        int f,s;
        int t;
        triplet(int f,int s,int t)
        {
            this.f=f;
            this.s=s;
            this.t=t;
        }
        public boolean equals(Object o)
        {
            triplet ob=(triplet)o;
            int ff,ss;
            int tt;
            if(o!=null)
            {
                ff=ob.f;
                ss=ob.s;
                tt=ob.t;
                if((ff==this.f)&&(ss==this.s)&&(tt==this.t))
                    return true;
            }
            return false;
        }
        public int hashCode()
        {
            return (this.f+" "+this.s+" "+this.t).hashCode();
        }
        public int compareTo(Object o)
        {
            triplet tr=(triplet)o;
            if(f>tr.f)
                return 1;
            else if(f==tr.f)
            {
                if(s>tr.s)
                    return -1;
                else
                    return 1;
            }
            else
                return -1;
        }
    }
    HashSet<Integer>hs=new HashSet<>();
    ArrayList<Integer>g[];
    int dp[],child[];
    int p;
    int m[];
    int dfs(int u,int pp)
    {
        //System.out.println(u+" "+pp);
        if(u!=p&&hs.contains(u))
        {
            child[pp]=pp;
            dp[u]=0;
            return 0;
        }
        if(m[u]>1)
        {
            child[pp]=pp;
            dp[u]=0;
            return 0;
        }
        if(u!=p&&g[u].size()==0)
        {
            child[u]=u;
            dp[u]=1;
            return 1;
        }
        if(dp[u]!=-1)
        {
            return dp[u];
        }
        for(int v : g[u])
        {
            if(v!=pp)
            {
                child[u]=v;
                dp[u]=1+dfs(v,u);
            }
        }
        return dp[u];
    }
    public static void main(String args[])throws Exception
    {
        new Thread(null,new templ(),"templ",1<<27).start();
    }
    public void run()
    {
        try
        {
            InputReader in = new InputReader(System.in);
            PrintWriter out = new PrintWriter(System.out);
            int n=in.ni();
            g=new ArrayList[n+1];
            dp=new int[n+1];
            child=new int[n+1];
            m=new int[n+1];
            Arrays.fill(dp,-1);
            for(int i=1;i<=n;i++)
                g[i]=new ArrayList<>();
            for(int i=1;i<=n;i++)
            {
                int x=in.ni();
                if(x==1)
                    hs.add(i);
            }
            for(int i=1;i<=n;i++)
            {
                int x=in.ni();
                if(x!=0)
                {
                    g[i].add(x);
                }
            }
            for(int i=1;i<=n;i++)
            {
                for(int j : g[i])
                    m[j]++;
            }
            for(int i : hs)
            {
                p=i;
                dfs(p,0);
            }
            int ans=0;
            int ind=0;
            for(int i=1;i<=n;i++)
            {
                if(dp[i]>ans)
                {
                    ans=dp[i];
                    ind=i;
                }
            }
            if(ans!=0)
            {
                out.println(ans);
                Stack<Integer> st = new Stack<>();
                while (true) {
                    st.add(ind);
                    if (ind == child[ind])
                        break;
                    ind = child[ind];
                }
                while (!st.isEmpty())
                    out.print(st.pop() + " ");
            }
            if(ans==0)
            {
                out.println(1);
                out.println(p);
            }
            out.close();
        }
        catch(Exception e){
            return;
        }
    }
    static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

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

        public int ni() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }

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

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

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

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }
}


import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] flag = new int[n+1];
		for ( int i = 1 ; i<=n ; ++i ) {
			flag[i] = sc.nextInt();
		}
		int[] arr = new int[n+1];
		int[] num = new int[n+1];
		for ( int i = 1 ; i<=n ; ++i ) {
			int x = sc.nextInt();
			arr[i] = x;
			++ num[x];
		}
		int maxlen = 0;
		int pos = 0;
		for ( int i = n ; i>=1 ; --i ) {
			if ( flag[i]==1 ) {
				//System.out.println("i: "+i+" "+flag[i]);
				int j = i;
				int len = 1;
				while ( arr[j]!=0 && num[arr[j]]<=1 ) {
					j = arr[j];
					++ len;
				}
				if ( maxlen<len ) {
					maxlen = len;
					pos = i;
				}
			}
		}
		System.out.println(maxlen);
		int[] path = new int[n];
		int cnt = 0;
		while ( cnt<maxlen ) {
			path[cnt++] = pos;
			pos = arr[pos];
		}
		for ( int i = cnt-1 ; i>=0 ; --i ) {
			System.out.print(path[i]+" ");
		}
		System.out.println();
	}
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 *
 * @author Chameera Wijebandara
 */
public class Main {
    public static void main(String[] args)throws IOException
    {
        java.io.BufferedReader in =new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

        int n=Integer.parseInt(in.readLine());
        int  count[] =new int [n];
        int  data[] =new int [n];
        boolean q[] = new boolean[n];

        StringTokenizer st =new StringTokenizer(in.readLine());
        for (int i =0; i <n; i++) {
            if(Integer.parseInt(st.nextToken())==1)
            {
                q[i] =true;
            }
        }

        st =new StringTokenizer(in.readLine());
        for (int i = 0; i <n; i++) {

            int p = Integer.parseInt(st.nextToken())-1;
            if(p!=-1)
            {
                count[p]++;
            }
            data[i]=p;
        }

        ArrayList<Integer> ans =new ArrayList<Integer>();
        for (int i = n-1; i >=0; i--) {
            if(q[i])
            {
                int hold =i;
                ArrayList<Integer> l =new ArrayList<Integer>();
                while(data[hold]>=0 &&count[data[hold]]==1 && !q[data[hold]])
                {
                    l.add(hold);
                    hold = data[hold];
                }
                l.add(hold);
                if(ans.size()<l.size())
                {
                    ans.clear();
                    ans.addAll(l);
                }

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

    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 *
 * @author Chameera Wijebandara
 */
public class Main {
    public static void main(String[] args)throws IOException
    {
        java.io.BufferedReader in =new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

        int n=Integer.parseInt(in.readLine());
        int  count[] =new int [n];
        int  data[] =new int [n];
        boolean q[] = new boolean[n];

        StringTokenizer st =new StringTokenizer(in.readLine());
        for (int i =0; i <n; i++) {
            if(Integer.parseInt(st.nextToken())==1)
            {
                q[i] =true;
            }
        }

        st =new StringTokenizer(in.readLine());
        for (int i = 0; i <n; i++) {

            int p = Integer.parseInt(st.nextToken())-1;
            if(p!=-1)
            {
                count[p]++;
            }
            data[i]=p;
        }

        ArrayList<Integer> ans =new ArrayList<Integer>();
        for (int i = n-1; i >=0; i--) {
            if(q[i])
            {
                int hold =i;
                ArrayList<Integer> l =new ArrayList<Integer>();
                while(data[hold]>=0 &&count[data[hold]]==1 && !q[data[hold]])
                {
                    l.add(hold);
                    hold = data[hold];
                }
                l.add(hold);
                if(ans.size()<l.size())
                {
                    ans.clear();
                    ans.addAll(l);
                }

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

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author karan173
 */
public class Main  implements Runnable{
    public void run() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
    public static void main(String[] args)
    {
        new Thread(null, new Main(), "karan", 1 << 24).start();
    }
}

class TaskB
{
    int n ;
    int types[];
    int outDeg[];
    int dp[];
    boolean done[] ;
    PrintWriter o;
    public void solve(int testNumber, FastReader in, PrintWriter out)
    {
        o = out;
        n = in.nextInt ();
        types = new int[n];
        dp = new int[n];
        done = new boolean[n];
        outDeg = new int[n];

        for (int i = 0; i < n; i++)
        {
            types[i] = in.nextInt ();
            done[i] = false;
            dp[i] = -1;
            outDeg[i] = -1;
        }
        for (int i = 0; i < n; i++)
        {
            int x = in.nextInt () - 1;
            if (x == -1)
            {
                continue;
            }
            if (outDeg[x] == -2)  //more than 1
            {
                continue;
            }
            if (outDeg[x] >= 0)   //already had 1
            {
                outDeg[x] = -2;
                continue;
            }
            //else -1
            outDeg[x] = i;
        }
//        out.println (Arrays.toString (outDeg));
        int maxI = -1;
        for (int i = 0; i < n; i++)
        {
            find (i);
            if (maxI == -1 || dp[maxI] < dp[i])
            {
                maxI = i;
            }
        }
//        out.println (Arrays.toString (dp));
        out.println (dp[maxI]);
        printPath (maxI);

    }

    private void printPath(int i)
    {
        if (types[i] == 1)
        {
            i++;
            o.println (i);
            return;
        }
        o.print ((i+1)+" ");
        printPath (outDeg[i]);

    }

    private int find(int i)
    {
        if (done[i])
        {
            return dp[i];
        }
        done[i] = true;
        if (types[i] == 1)
        {
            dp[i] = 1;
            return dp[i];
        }
        if (outDeg[i] < 0)
        {
            dp[i] = Integer.MIN_VALUE;
            return dp[i];
        }
        dp[i] = 1 + find (outDeg[i]);
        return dp[i];
    }
}

class FastReader
{

    public InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public FastReader(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 nextInt()
    {
        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 boolean isSpaceChar(int c)
    {
        if (filter != null)
        {
            return filter.isSpaceChar (c);
        }
        return isWhitespace (c);
    }

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

    public interface SpaceCharFilter
    {
        public boolean isSpaceChar(int ch);
    }
}