#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#define maxn 50010
#define inc(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
inline int read(){
char ch=getchar(); int f=1,x=0;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return f*x;
}
int x[maxn],y[maxn],n,t,dis[maxn]; queue<int>q;
struct nd{
int x,y,id;
bool operator < (const nd &a)const{return x!=a.x?x<a.x:y<a.y;}
};
set<nd>st;
int main(){
n=read(); t=read(); inc(i,1,n)x[i]=read(),y[i]=read(),st.insert((nd){x[i],y[i],i});
q.push(0); dis[0]=0;
while(!q.empty()){
int z=q.front(); q.pop();
inc(i,-2,2)inc(j,-2,2){
set<nd>::iterator a=st.find((nd){x[z]+i,y[z]+j,0});
if(a!=st.end()){
dis[a->id]=dis[z]+1; q.push(a->id);
if(a->y==t){printf("%d",dis[a->id]); return 0;} st.erase(a);
}
}
}
printf("-1"); return 0;
}