#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define ls rt<<1
#define rs ls1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-9)
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 100000007
#define pi acos(-1.0)
#define M 1000+5
int a[M],b[M];
int main(){
int n,i,j,x,cn;
while(~scanf("%d %d",&n,&x)){
for(i=0;i<n;i++){
scanf("%d %d",&a[i],&b[i]);
if(a[i]>b[i]){
j=a[i];
a[i]=b[i];
b[i]=j;
}
}
sort(a,a+n); sort(b,b+n);
if(a[n-1]<=b[0]){
if(x<a[n-1])printf("%d\n",a[n-1]-x);
else if(x>b[0])printf("%d\n",x-b[0]);
else puts("0");
}else puts("-1");
}
return 0;
}
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define ls rt<<1
#define rs ls1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-9)
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 100000007
#define pi acos(-1.0)
#define M 1000+5
int a[M],b[M];
int main(){
int n,i,j,x,cn;
while(~scanf("%d %d",&n,&x)){
for(i=0;i<n;i++){
scanf("%d %d",&a[i],&b[i]);
if(a[i]>b[i]){
j=a[i];
a[i]=b[i];
b[i]=j;
}
}
sort(a,a+n); sort(b,b+n);
if(a[n-1]<=b[0]){
if(x<a[n-1])printf("%d\n",a[n-1]-x);
else if(x>b[0])printf("%d\n",x-b[0]);
else puts("0");
}else puts("-1");
}
return 0;
}