#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,y,k,yy,edge_sum,head[100005],railway[100005];
bool vis[100005];
queue<ll>q;
struct edge{
ll next,to;
}a[100005];
void add_edge(ll from,ll to){
edge_sum++;
a[edge_sum].next=head[from];
a[edge_sum].to=to;
head[from]=edge_sum;
}
void bfs(ll x){
q.push(x);
vis[x]=true;
while(!q.empty()){
ll t=q.front();
q.pop();
for(int i=head[t];i!=0;i=a[i].next)
{
if(vis[a[i].to])continue;
if(a[i].to==y)return ;
q.push(a[i].to);
vis[a[i].to]=true;
railway[a[i].to]=railway[t]+1;
}
}
cout<<-1;
exit(0);
}
signed main(){
cin>>n>>x>>y;
for(int i=1;i<=n;i++)
{
cin>>k;
for(int j=1;j<=k;j++)
cin>>yy,add_edge(i,yy);
}
bfs(x);
cout<<railway[y];
return 0;
}