#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define inpfile "exp.in"
#define outfile "exp.out"

#define NMAX 1005
#define MMAX 500005

struct NODE {
	int x,next;
};

//Global variables
int a[NMAX];
NODE p[MMAX];
int col[NMAX];
int d[NMAX];
int h[NMAX];
int n,pn;

int u[NMAX];
int r[NMAX];
int res,rn;
int bol;
//----------------

void Init()
{
	freopen(inpfile,"r",stdin);
	freopen(outfile,"w",stdout);
	memset(a,0,sizeof(a[0])*NMAX);
	memset(h,0,sizeof(h[0])*NMAX);
}

void Out()
{
	int i;
	printf("%d\n",res);
	for (i=1; i<=n; i++)
		printf("%d ",r[i]);

	exit(0);
}

void Inp()
{
	int i,j,k;
	int x;

	scanf("%d",&n);

	pn=0;
	for (i=1; i<=n; i++) {
		scanf("%d %d",col+i,&k);
		for (j=1; j<=k; j++) {
			scanf("%d",&x);

			pn++;
			p[pn].x=i;
			p[pn].next=a[x];
			a[x]=pn;

			d[i]++;
		}
	}
}

void Dfs(int x)
{
	int i,d;

	h[x]=1;

	for (i=a[x]; i; i=p[i].next) {
		if (!h[p[i].x]) Dfs(p[i].x);

		if (col[x]!=col[p[i].x]) d=1;
		else d=0;

		if (h[x]<h[p[i].x]+d) h[x]=h[p[i].x]+d;
	}
}

void Add(int x)
{
	u[x]=1;
	rn++;
	r[rn]=x;

	int i;

	for (i=a[x]; i; i=p[i].next) d[p[i].x]--;
	for (i=a[x]; i; i=p[i].next)
		if ((!d[p[i].x]) && (col[p[i].x]==bol)) Add(p[i].x);
	
}

void Solve()
{
	int i,j,maxi;
	int dd;

	for (i=1; i<=n; i++)
		if (!h[i]) Dfs(i);

	maxi=1; dd=0;
	for (i=2; i<=n; i++) {
		if ((h[i]==h[maxi]) && (col[i]!=col[maxi]) && (h[i]&1)) dd=1;
		if (h[i]>h[maxi]) { maxi=i; dd=0;}
	}

	res=h[maxi]-1+dd; 
	
	memset(u,0,sizeof(u[0])*NMAX);
	rn=0;

	bol=col[maxi];
	for (i=1; i<=h[maxi]+dd; i++) {
		for (j=1; j<=n; j++)
			if ((col[j]==bol) && (!u[j]) && (!d[j])) Add(j);
		bol=1-bol;
	}
}

int main()
{
	Init();
	Inp();
	Solve();
	Out();

	return 0;
}
