#include<bits/stdc++.h>
#define int long long
#define min(x,y) (x>y?y:x)
#define re register
using namespace std;
const int N = 9010 , M = 10010010;
int n,k;
int num;
int dis[M],check[M];
int g[N][N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
scanf("%lld%lld",&n,&k);
for(re int i=1;i<=n;i++) dis[i] = M;
for(re int i=1;i<=n;i++){
int a,b;
scanf("%lld%lld",&a,&b);
g[a][b] = 1;
}
dis[1] = 0;
for(re int i=1;i<=n;i++){
int minn=M,shu;
for(re int j=1;j<=n;j++){
if(!check[j]&&minn>dis[j]) {
minn = dis[j];
shu = j;
}
check[shu] = 1;
for(re int j=1;j<=n;j++){
if(g[shu][j]>0){
dis[j] = min(dis[j],minn+g[shu][j]);
}
}
}
}
if(dis[k]==M) printf("-1");
else printf("%lld",dis[k]+1);
return 0;
}