#include<map>
#include<queue>
#include<time.h>
#include<limits.h>
#include<cmath>
#include<ostream>
#include<iterator>
#include<set>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep_1(i,m,n) for(int i=m;i<=n;i++)
#define mem(st) memset(st,0,sizeof st)
int read()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-') //判断正负
flag=1;
else if(ch>='0'&&ch<='9') //得到完整的数
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef pair<double,double> pdd;
const int inf = 0x3f3f3f3f;
const int N=101010;
int e[N],ne[N],idx,h[N],w[N];
int n,a,b;
int dist[N];
int st[N];
void add(int a,int b,int c)
{
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
void spfa(int start)
{
queue<int>q;
q.push(start);
dist[start]=0;
st[start]=1;
while(q.size())
{
int t=q.front();
q.pop();
st[t]=0;
for(int i=h[t];i!=-1;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];
if(!st[j])
{
q.push(j);
st[j]=1;
}
}
}
}
}
int main()
{
memset(h,-1,sizeof h);
cin>>n>>a>>b;
for(int i=1;i<=n;i++)
{
int m;
cin>>m;
for(int j=1;j<=m;j++)
{
int x;
cin>>x;
if(j==1)
add(i,x,0);
else
add(i,x,1);
}
}
memset(dist,0x3f,sizeof dist);
spfa(a);
if(dist[b]>=0x3f3f3f3f)
cout<<"-1"<<endl;
else
cout<<dist[b]<<endl;
return 0;
}