import java.util.*;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;

public class exp_pm {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("exp.in"));
        PrintWriter writer = new PrintWriter(new File("exp.out"));
        int n = scanner.nextInt();
        Vertex[] v = new Vertex[n];
        for (int i = 0; i < v.length; i++) {
            v[i] = new Vertex(i);
        }
        for (Vertex vertex : v) {
            vertex.color = scanner.nextInt();
            int k = scanner.nextInt();
            for (int j = 0; j < k; j++) {
                int a = scanner.nextInt();
                v[a - 1].next.add(vertex);
                vertex.w++;
            }
        }

        int minC = 0;
        int[] minL = null;
        for (int start = 0; start < 2; start++) {
            Queue<Vertex>[] q = new Queue[2];
            q[0] = new LinkedList<Vertex>();
            q[1] = new LinkedList<Vertex>();
            for (Vertex vertex : v) {
                vertex.ww = vertex.w;
                if (vertex.w == 0) {
                    q[vertex.color].offer(vertex);
                }
            }
            int c = 0;
            int[] l = new int[n];
            int i = start;
            int j = 0;
            while (j < n) {
                c++;
                while (!q[i].isEmpty()) {
                    int a = q[i].remove().ind;
                    l[j++] = a;
                    for (Vertex vertex : v[a].next) {
                        vertex.ww--;
                        if (vertex.ww == 0) {
                            q[vertex.color].offer(vertex);
                        }
                    }
                }
                i = 1 - i;
            }
            if (start == 0) {
                minC = c;
                minL = l;
            } else {
                if (c < minC) {
                    minC = c;
                    minL = l;
                }
            }
        }
        writer.println(minC - 1);
        for (int i : minL) {
            writer.print(i + 1);
            writer.print(" ");
        }
        writer.close();
    }

    static class Vertex {
        Collection<Vertex> next;
        int color;
        int w;
        int ww;
        int ind;
        public Vertex(int ind) {
            this.ind = ind;
            next = new ArrayList<Vertex>();
            w = 0;
        }
    }
}
