#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
bool g[505][505];
int num[505];
int d[505];
bool vis[505];
int read(){
int s=0,w=1;
char ch=getchar();
while(ch>'9'||ch<'0'){
if(ch=='-')
w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
s=s*10+ch-'0';
ch=getchar();
}
return s*w;
}
void get(string a){
a=' '+a+' ';
int len=a.length();
for(int i=0;i<len;i++){
if(a[i-1]==' '||a[i-1]=='\r'){
int s=a[i]-'0';
for(int j=i+1;j<len;j++){
if(a[j]==' '||a[j]=='\r'){
num[++cnt]=s;
s=0;
i=j+2;
}
else
s=s*10+a[j]-'0';
}
}
}
}
void readp(){
n=read(),m=read();
string a;
for(int i=1;i<=n;i++){
cnt=0;
memset(num,0,sizeof(num));
getline(cin,a);
get(a);
for(int j=1;j<=cnt;j++){
int x=num[j];
for(int k=j+1;k<=cnt;k++){
int y=num[k];
g[x][y]=true;
}
}
}
}
void SPFA(int s){
queue<int>q;
d[s]=0;
vis[s]=true;
q.push(s);
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=false;
for(int i=1;i<=m;i++){
if(g[x][i]==true&&d[i]>d[x]+g[x][i]){
d[i]=d[x]+g[x][i];
if(!vis[i]){
q.push(i);
vis[i]=true;
}
}
}
}
}
int main(){
readp();
for(int i=1;i<=m;i++)
d[i]=1e9;
SPFA(1);
if(d[m]==1e9)
cout<<"NO"<<endl;
else
cout<<d[m]-1<<endl;
return 0;
}
本地和评测的答案不一样,而且程序里可能也有错误