//* Problem  : 2403 - The mysterious X network
//* Contest  : acm.tju.edu.cn
//* Type     : Solution
//* Date     : 2007.07.31
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 6.0

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

struct list_item
{
	int value;
	list_item *p;
};

struct vertex{
	int v;
	int cnt;
	list_item *first;
};

int n, src, dst;
vertex a[100000];

//***Stack  (Queue)
#define stack_size 100000				// sizeofstack

typedef int stack_item;				// type of stack elements

stack_item main_stack[stack_size];
stack_item tmp_stack[stack_size];
int i_main_stack, i_tmp_stack;

void stack_init() {i_main_stack = i_tmp_stack = 0;}

void stack_add(stack_item value) {tmp_stack[i_tmp_stack++] = value;}

void stack_reverse()
{
	memcpy(main_stack, tmp_stack, (i_tmp_stack)*sizeof(stack_item));
	i_main_stack = i_tmp_stack;
	i_tmp_stack = 0;
}

int stack_getsize() {return (i_main_stack);}

stack_item stack_get(int k) {return main_stack[k];}
//*** End Of Stack

void list_add(int h, int value)
{
	list_item *p = (list_item *)malloc(sizeof(list_item));
	p->p = a[h].first; 
	p->value = value;
	a[h].first = p;
}

void process_vertex(int k)
{
	int i, t;
	list_item *p = a[k].first;
	for (i = 0; i < a[k].cnt; i++)
	{
		t = p->value;
		if (!a[t].v)
		{
			a[t].v = a[k].v + 1;
			stack_add(t);
		}
		p = p->p;
	}
}

int solve()
{
	if (src == dst) return 0;
	int k, i, t;
	a[src].v = 1;
	stack_init(); stack_add(src); stack_reverse();
	while ((k = stack_getsize()) && !a[dst].v)
	{
		for (i = 0; i < k; i++)
		{
			t = stack_get(i);
			process_vertex(t);
		}
		stack_reverse();
	}
	return a[dst].v - 2;
}

int main()
{
	memset(a, 0, sizeof(a));
	int i, j, h, t, c, res;
	scanf("%d", &n);
	for (j = 0; j < n; j++)
	{
		scanf("%d %d", &h, &c);
		for (i = 0; i < c; i++)
		{
			scanf("%d", &t);
			list_add(h, t); a[h].cnt++;
			list_add(t, h); a[t].cnt++;
		}
	}
	scanf("%d %d", &src, &dst);
	res = solve();
	printf("%d %d %d\n", src, dst, res);
}