Idea: For the station map given in order, as in the example, 4 can go to 7 3 6, 7 can go to 3 6, and 3 can go to 6 to build a directed graph, and then find the number from point 1 to point m The shortest distance, the distance of the same question line is 1, and the question is the number of transfers, so the final answer is -1
//#pragma GCC optimize(2)#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;#define SIS std::ios::sync_with_stdio(false)#define space putchar(' ')#define enter putchar('\n')#define lson root<<1#define rson root<<1|1typedef pair<int,int> PII;constint mod=1e9+7;constint N=1e5+5;constint inf=0x7f7f7f7f;intgcd(int a,int b){
return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){
return a*(b/gcd(a,b));}template<classT>voidread(T &x){
char c;bool op =0;while(c =getchar(), c <'0'|| c >'9')if(c =='-')
op =1;
x = c -'0';while(c =getchar(), c >='0'&& c <='9')
x = x *10+ c -'0';if(op)
x =-x;}template<classT>voidwrite(T x){
if(x <0)
x =-x,putchar('-');if(x >=10)write(x /10);putchar('0'+ x %10);}
ll qsm(int a,int b,int p){
ll res=1%p;while(b){
if(b&1)
res=res*a%p;
a=1ll*a*a%p;
b>>=1;}return res;}int mp[2005][2005];int dis[N];int vis[N];int n,m,k;int a[N];voiddj(){
memset(dis,inf,sizeof dis);
dis[1]=0;
queue<int> q;
q.push(1);while(!q.empty()){
//cout<<2333<<endl;int now=q.front();// cout<<now<<endl;
q.pop();for(int i=1;i<=m;i++){
if(dis[i]>dis[now]+1&&mp[now][i]!=-1){
dis[i]=dis[now]+1;
q.push(i);}}}}intmain(){
memset(mp,-1,sizeof mp);
cin>>n>>m;
string line;getline(cin,line);//cout<<line<<endl;while(n--){
int cnt=0;int x;getline(cin,line);//cout<<line<<endl;
stringstream st(line);while(st>>x) a[cnt++]=x;for(int i=0;i<cnt;i++)for(int j=i+1;j<cnt;j++)
mp[a[i]][a[j]]=1;}dj();if(dis[m]==inf) cout<<"NO"<<endl;else cout<<max(0,dis[m]-1)<<endl;return0;}