#include<iostream> #include<cstring> #include<algorithm> #include<iomanip> #include<cmath> #include<cstdio> #include<vector> #include<string> #include<map> #include<queue> #include<vector> using namespace std; /* 单源最短路 路径方向与switch方向同向那么路径权值0,否则为1 */ #define MAXN 101 #define INF 0x3f3f3f3f bool g[MAXN][MAXN],TO[MAXN][MAXN],been[MAXN]; int lowcost[MAXN],A,B,N; int Dijkstra(int beg,int to) { for(int i=1;i<=N;i++) { lowcost[i] = INF; been[i] = false; } lowcost[beg] = 0; for(int i=0;i<N;i++) { int Minc = INF,k = -1; for(int j=1;j<=N;j++) { if(!been[j]&&lowcost[j]<Minc) { Minc = lowcost[j]; k = j; } } if(k==-1) break; been[k] = true; for(int j=1;j<=N;j++) { int cost; if(!g[k][j]) cost = INF; else { if(TO[k][j]==true) cost = 0; else cost = 1; } if(!been[j]&&lowcost[j]>lowcost[k]+cost) { lowcost[j] = lowcost[k]+cost; } } } return lowcost[to]; } int main() { cin>>N>>A>>B; int k,tmp; for(int i=1;i<=N;i++) { cin>>k; for(int j=1;j<=k;j++) { cin>>tmp; if(j==1) TO[i][tmp] = true; g[i][tmp] = true; } } int ans = Dijkstra(A,B); if(ans!=INF) cout<<ans<<endl; else cout<<-1<<endl; return 0; }