#include <iostream>

using namespace std;
#include<math.h>
#include <cstdlib>

int main()
{
    int n,x0; int mini=0, maxi=0,dis1=0,dis2=0;
    cin>>n>>x0;
    int a[200][2]; int finalmin [200];int finalmax [200],final[200]; int counter =0;
    for(int i=0;i<n;i++)
    for (int j=0;j<2;j++)
        cin>> a[i][j];
    for (int i=0;i<n;i++)
        for (int j=0;j<1;j++)
    {
        mini= min (a[i][j],a[i][j+1]);
        maxi= max (a[i][j],a[i][j+1]);
        if (x0>= mini && x0<= maxi) continue;
        else {finalmin[counter]=mini;
        finalmax[counter]=maxi;
        counter++;}

    }
    for (int i=0;i<counter;i++)
       {
       dis1=finalmax[i]- x0;
       dis2= finalmin[i] -x0;
       if (abs(dis1)> abs(dis2)) final[i]=dis2;
        else final[i]=dis1;
       }
    for(int i=0;i<counter;i++)
        {cout<< final[i];exit(0);}
    cout<< "0"<<endl;

    return 0;
}


#include<iostream>
using namespace std;
#include <stdlib.h>
int main()
{ int n,x,a,b,m=0,M=100,z;
cin>> n>>x;
for(int i=0;i<n;i++)
{cin>> a>>b;
if (a>b){int temp=a; a=b;b=a;}
if(b<M){M=b;}
if (a>m){m=a;}}
if(x>=m && x<=M)
cout << "o"<<endl;
else z= abs(x-m)< abs(x-M)? abs(x-m): abs(x-M);
if(m<=M) cout << z<<endl;
else cout << "-1"<<endl;

return 0;
}


#include <iostream>
using namespace std;
#include<math.h>
int main()
{int n,x0,counter=0;
int result=0,minresult,maxresult;

cin>>n>>x0;
int  a[n][2],b[n],c[n];
for (int i=0;i<n;i++)
for(int j=0;j<2;j++)
cin>> a[i][j];

for(int i=0;i<n;i++)
{int j=0;
if((x0>a[i][j]&& x0<a[i][j+1]) || (x0>a[i][j+1] && x0>a[i][j])) continue;
else{
b[counter]=min(a[i][j],a[i][j+1]);
c[counter]=max(a[i][j],a[i][j+1]);
counter++;}
}
/**
for(int i=0;i<n;i++)
cout << b[i]<<"  ";
cout <<endl;
for(int i=0;i<n;i++)
cout << c[i]<<"   ";
**/
if (counter==0) cout<<result<<endl;
else if (counter ==1) {
    minresult=b[0];maxresult=c[0];
    result=min(abs(x0-minresult),abs(x0-maxresult));
    cout<<result<<endl;}
else{
minresult=b[0];maxresult=c[0];
for(int i=1;i<n;i++)
{
minresult=max(minresult,b[i]);
maxresult=min(maxresult,c[i]);

    }
result=min(abs(x0-minresult),abs(x0-maxresult));
cout<<result<<endl;}

  return 0;
}


#include<iostream>
using namespace std;
#include <stdlib.h>
#include<stdio.h>
int main()
{ int n,x,a,b,m=0,M=1001,z;
cin>> n>>x;
for(int i=0;i<n;i++)
{cin>> a>>b;
if (a>b){int temp=a; a=b;b=temp;}
if(b<M){M=b;}
if (a>m){m=a;}}
if(x>=m && x<=M)
{cout << "0";exit(0);}
else z= abs(x-m)< abs(x-M)? abs(x-m): abs(x-M);
if(m<=M) cout << z<<endl;
else cout << "-1"<<endl;

return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x;
    cin>>n>>x;
    int a[n],b[n],ab[n];
    int x2=10000000;
    for(int i=0;i<n;i++){
        cin>>a[i]>>b[i];
        if(abs(a[i]-b[i])<x2)
            x2=abs(a[i]-b[i]);}
int ay[n];
int ay1=0;
        int i;
        for( i=0;i<n;i++)
        {
            if(abs(a[i]-b[i])==x2)
                break;
        }
        bool t=0;

        for(int j=min(a[i],b[i]);j<=max(a[i],b[i]);j++)
        {
            for(int u=0;u<n;u++)
            {
                if(j>=min(a[u],b[u])&&j<=max(a[u],b[u])){
                t=1;}
                else{
                    t=0;
                    break;
                }
            }
            if(t){
                ay[ay1]=abs(x-j);
                ay1++;
                }

        }
        sort(ay,ay+ay1);
        if(t)
        cout<<ay[0];
        if(t==0)
            cout<<"-1";}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x;
    cin>>n>>x;
    int a[n],b[n],ab[n];
    int x2=10000000;
    for(int i=0;i<n;i++){
        cin>>a[i]>>b[i];
        if(abs(a[i]-b[i])<x)
            x2=abs(a[i]-b[i]);}

        int i;
        for( i=0;i<n;i++)
        {
            if(abs(a[i]-b[i])==x2)
                break;
        }
        bool t=0;

        for(int j=min(a[i],b[i]);j<=max(a[i],b[i]);j++)
        {
            for(int u=0;u<n;u++)
            {
                if(j>=min(a[u],b[u])&&j<=max(a[u],b[u])){
                t=1;}
                else{
                    t=0;
                    break;
                }
            }
            if(t){
                cout<<abs(x-j);
                break;}

        }
        if(t==0)
            cout<<"-1";

}


#include<bits/stdc++.h>
using namespace std;
int arr[1003];
int main()
{
    int n,x;
    cin>>n>>x;
    for(int i=1;i<=n;i++)
    {
        int l,r;
        cin>>l>>r;
        if(l>r)swap(l,r);
        arr[l]++;
        arr[r+1]--;
    }
    int test= 0,result=1e9;
    for(int i=0;i<=1000;i++)
    {
        test+=arr[i];
        if(test==n)
        result=min(result,abs(i-x));
    }
    if(result==1e9)
    cout<<"-1"<<endl;
    else
    cout<<result<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x;
    cin>>n>>x;
    int a[n],b[n],ab[n];
    int x2=10000000;
    for(int i=0;i<n;i++){
        cin>>a[i]>>b[i];
        if(abs(a[i]-b[i])<x2)
            x2=abs(a[i]-b[i]);}
int ay[n];
int ay1=0;
        int i;
        for( i=0;i<n;i++)
        {
            if(abs(a[i]-b[i])==x2)
                break;
        }
        bool t=0;

        for(int j=min(a[i],b[i]);j<=max(a[i],b[i]);j++)
        {
            for(int u=0;u<n;u++)
            {
                if(j>=min(a[u],b[u])&&j<=max(a[u],b[u])){
                t=1;}
                else{
                    t=0;
                    break;
                }
            }
            if(t){
                ay[ay1++]=abs(x-j);
                }

        }
        sort(ay,ay+ay1);
        if(t)
        cout<<ay[0];
        if(t==0)
            cout<<"-1";}


#include<iostream>
using namespace std;
int main()
{
    int n,x;
    cin>>n>>x;
    int a[n],b[n],ab[n];
    int x2=10000000;
    for(int i=0;i<n;i++){
        cin>>a[i]>>b[i];
        if(abs(a[i]-b[i])<x2)
            x2=abs(a[i]-b[i]);}

int ay1=10000000000;
        int i;
        for( i=0;i<n;i++)
        {
            if(abs(a[i]-b[i])==x2)
                break;
        }
        bool t=0;

        for(int j=min(a[i],b[i]);j<=max(a[i],b[i]);j++)
        {
            for(int u=0;u<n;u++)
            {
                if(j>=min(a[u],b[u])&&j<=max(a[u],b[u])){
                t=1;}
                else{
                    t=0;
                    break;
                }
            }
            if(t){
                if(abs(x-j)<ay1)
                ay1=abs(x-j);
                }

        }

        if(t)
        cout<<ay1;
        if(t==0)
            cout<<"-1";}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main() {
    int n, x, a, b, l, r;
    cin >> n >> x;
    l = r = x;
    for(int i = 1; i <= n; ++i) {
        cin >> a >> b;
        if(a > b) swap(a,b);
        if(b < l) l = b;
        if(a > r) r = a;
    }
    if(r >= l) printf("%d\n",r-l);
    else printf("-1\n");
return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main() {
    int n, x, a, b, l, r;
    cin >> n >> x;
    l = r = x;
    for(int i = 1; i <= n; ++i) {
        cin >> a >> b;
        if(a > b) swap(a,b);
        if(b < l) l = b;
        if(a > r) r = a;
        if(r < l) {
            printf("-1\n");
            return 0;
        }
    }
    printf("%d\n",r-l);
return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main() {
    int n, x, a, b, l, r;
    cin >> n >> x;

    cin >> a >> b;
    if(a > b) swap(a,b);
    l = a, r = b;

    for(int i = 2; i <= n; ++i) {
        cin >> a >> b;
        if(a > b) swap(a,b);
        if(a <= r && a >= l) {
            if(b >= r) l = a;
            else l = a, r = b;
        } else if(b <= r && b >= l) {
            if(a >= l) l = a, r = b;
            else r = b;
        } else if(a <= r && b >= l) {
        } else {
            printf("-1\n");
            return 0;
        }
    }
    if(x >= l && x <= r) {
        printf("0\n");
    } else {
        printf("%d\n",min(abs(r-x),abs(l-x)));
    }
return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main() {
    int n, x, a, b, l, r;
    cin >> n >> x;

    cin >> a >> b;
    if(a > b) swap(a,b);
    l = a, r = b;

    for(int i = 2; i <= n; ++i) {
        cin >> a >> b;
        if(a > b) swap(a,b);
        if(a <= r && a >= l) {
            if(b >= r) l = a;
            else l = a, r = b;
        } else if(b <= r && b >= l) {
            if(a >= l) l = a, r = b;
            else r = b;
        } else {
            printf("-1\n");
            return 0;
        }
    }
    if(x >= l && x <= r) {
        printf("0\n");
    } else {
        printf("%d\n",min(abs(r-x),abs(l-x)));
    }
return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main() {
    int n, x, a, b, l, r;
    cin >> n >> x;

    cin >> a >> b;
    if(a > b) swap(a,b);
    l = a, r = b;

    for(int i = 2; i <= n; ++i) {
        cin >> a >> b;
        if(a > b) swap(a,b);
        if(a <= r && a >= l) {
            if(b >= r) l = a;
            else l = a, r = b;
        } else if(b <= r && b >= l) {
            if(a >= l) l = a, r = b;
            else r = b;
        } else {
            printf("-1\n");
            return 0;
        }
    }
    if(x >= r && x <= l) {
        printf("0\n");
    } else {
        printf("%d\n",min(abs(r-x),abs(l-x)));
    }
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,x,mn=0,mx=1001;bool bo=false;
    cin>>n>>x;
    for(int i=0;i<n;i++){
      int a,b;
      cin>>a>>b;
      if(a>b)swap(a,b);

      if((a>=mn&&a<=mx)||(b>=mn&&b<=mx)){
         mn=max(a,mn); mx=min(b,mx);
      }
      else
        bo=true;

    }
    if(bo)
      cout<<-1<<endl;
    else if(x>=mn&&x<=mx)
        cout<<0<<endl;
    else
     cout<<min(abs(x-mn),abs(x-mx))<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,x,mn=0,mx=1001;bool bo=false;
    cin>>n>>x;
    for(int i=0;i<n;i++){
      int a,b;
      cin>>a>>b;
      if(a>b)swap(a,b);

      if((a>=mn&&a<=mx)||(b>=mn&&b<=mx)||(a<mn&&b>mx)){
         mn=max(a,mn); mx=min(b,mx);
      }
      else
        bo=true;

    }
    if(bo)
      cout<<-1<<endl;
    else if(x>=mn&&x<=mx)
        cout<<0<<endl;
    else
     cout<<min(abs(x-mn),abs(x-mx))<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,x,mn=0,mx=1001;bool bo=false;
    cin>>n>>x;
    for(int i=0;i<n;i++){
      int a,b;
      cin>>a>>b;
      if(a>b)swap(a,b);
      mn=max(a,mn); mx=min(b,mx);
      if(mn>mx) bo=true;

    }
    if(bo)
      cout<<-1<<endl;
    else if(x>=mn&&x<=mx)
        cout<<0<<endl;
    else
     cout<<min(abs(x-mn),abs(x-mx))<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,x,mn=0,mx=1001;bool bo=false;
    cin>>n>>x;
    for(int i=0;i<n;i++){
      int a,b;
      cin>>a>>b;
      if(a>b)swap(a,b);

      if(a>mx)
        bo=true;
      mn=max(a,mn); mx=min(b,mx);
    }
    if(bo)
      cout<<-1<<endl;
    else if(x>=mn&&x<=mx)
        cout<<0<<endl;
    else
     cout<<min(abs(x-mn),abs(x-mx))<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,x,mn=0,mx=1001;bool bo=false;
    cin>>n>>x;
    for(int i=0;i<n;i++){
      int a,b;
      cin>>a>>b;
      if(a>b)swap(a,b);

      if(a>mx)
        bo=true;
      mn=max(a,mn); mx=min(b,mx);
    }
    if(bo)
      cout<<-1<<endl;
    else
     cout<<min(abs(x-mn),abs(x-mx))<<endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <iostream>
#include <functional>
#include <complex>
#include <iomanip>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;
typedef vector<pii> vpii;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define FORD(i,a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-6
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(n) ((n) * (n))
#define INF 1000000007
#define MAXN 10001

int main()
{
	int n, x;
	cin >> n >> x;
	int arr[1002];
	CL(arr, 0);
	REP(i, n)
	{
		int a, b;
		cin >> a >> b;
		arr[min(a,b)]++;
		arr[max(a,b) + 1]--;
	}
	int count = 0;
	REP(i, 1001)
	{
		count += arr[i];
		if (count == n)
		{
			if (x <= i)
			{
				cout << i - x;
				return 0;
			}
			else
			{
				FOR(j,i+1, 1001)
				{
					count += arr[j];
					if (count < n)
					{
						if (x <= j - 1)
						{
							cout << 0;
						}
						else
						{
							cout << x - j + 1;
						}
						return 0;
					}
				}
			}
		}
	}
	cout << -1;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <iostream>
#include <functional>
#include <complex>
#include <iomanip>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;
typedef vector<pii> vpii;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define FORD(i,a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-6
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(n) ((n) * (n))
#define INF 1000000007
#define MAXN 10001

int main()
{
	int n, x;
	cin >> n >> x;
	int arr[1002];
	CL(arr, 0);
	REP(i, n)
	{
		int a, b;
		cin >> a >> b;
		arr[min(a,b)]++;
		arr[max(a,b) + 1]--;
	}
	int count = 0;
	REP(i, 1001)
	{
		count += arr[i];
		if (count == n)
		{
			if (x <= i)
			{
				cout << i - x;
				return 0;
			}
			else
			{
				FOR(j,i+1, 1001)
				{
					count += arr[j];
					if (count == n - 1)
					{
						if (x <= j)
						{
							cout << 0;
						}
						else
						{
							cout << x - j;
						}
						return 0;
					}
				}
			}
		}
	}
	cout << -1;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <iostream>
#include <functional>
#include <complex>
#include <iomanip>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;
typedef vector<pii> vpii;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define FORD(i,a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-6
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(n) ((n) * (n))
#define INF 1000000007
#define MAXN 10001

int main()
{
	int n, x;
	cin >> n >> x;
	int arr[1001];
	CL(arr, 0);
	REP(i, n)
	{
		int a, b;
		cin >> a >> b;
		arr[min(a,b)]++;
		arr[max(a,b)]--;
	}
	int count = 0;
	REP(i, 1001)
	{
		count += arr[i];
		if (count == n)
		{
			if (x <= i)
			{
				cout << i - x;
				return 0;
			}
			else
			{
				FOR(j,i+1, 1001)
				{
					count += arr[j];
					if (count == n - 1)
					{
						if (x <= j)
						{
							cout << x;
						}
						else
						{
							cout << x - j;
						}
						return 0;
					}
				}
			}
		}
	}
	cout << -1;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <iostream>
#include <functional>
#include <complex>
#include <iomanip>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;
typedef vector<pii> vpii;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define FORD(i,a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-6
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(n) ((n) * (n))
#define INF 1000000007
#define MAXN 10001

int main()
{
	int n, x;
	cin >> n >> x;
	int arr[1002];
	CL(arr, 0);
	REP(i, n)
	{
		int a, b;
		cin >> a >> b;
		arr[min(a,b)]++;
		arr[max(a,b) + 1]--;
	}
	int count = 0;
	REP(i, 1001)
	{
		count += arr[i];
		if (count == n)
		{
			if (x <= i)
			{
				cout << i - x;
				return 0;
			}
			else
			{
				FOR(j,i+1, 1001)
				{
					count += arr[j];
					if (count < n)
					{
						if (x <= j - 1)
						{
							cout << 0;
						}
						else
						{
							cout << x - j - 1;
						}
						return 0;
					}
				}
			}
		}
	}
	cout << -1;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define FOR(n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
typedef pair<int, int> pi;

int main()
{
    int n, x;
    cin >> n >> x;
    int a, b, lmax = -1, rmin = INT_MAX;
    FOR(n)
    {
        int t1, t2;
        cin >> t1 >> t2;
        a = min(t1, t2);
        b = max(t1, t2);
        lmax = max(lmax, a[i]);
        rmin = min(rmin, b[i]);
    }
    if (x >= lmax && x <= rmin)
        cout << 0;
    else if (lmax <= rmin)
        cout << min(abs(x - lmax), abs(x - rmin));
    else
        cout << -1;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<int> vi;
typedef pair<int,int> pi;
#define FOR(n) for(int i=0;i<n;i++)
int totalBeads(vector<string> beads)
{
    int n = beads.size();
    int a[n];
    for (int i = 0; i < n; i++)
    {
        if (beads[i] == "S")
            a[i] = 0;
        else if (beads[i] == "M")
            a[i] = 1;
        else if (beads[i] == "L")
            a[i] = 2;
        else
            a[i] = 3;
    }
    int k = 2;
    unordered_map<int, int> freq;
    int start = 0, end = 0, now = 0, l = 0;
    for (int i = 0; i < n; i++)
    {
        freq[a[i]]++;

        if (freq[a[i]] == 1)
            now++;

        while (now > k)
        {
            freq[a[l]]--;
            if (freq[a[l]] == 0)
                now--;

            l++;
        }
        if (i - l + 1 >= end - start + 1)
            end = i, start = l;
    }
    int total_result = end - start + 1;
    return total_result;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define FOR(n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
typedef pair<int, int> pi;

int main()
{
    int n, x;
    cin >> n >> x;
    int a, b, lmax = -1, rmin = INT_MAX;
    FOR(n)
    {
        int t1, t2;
        cin >> t1 >> t2;
        a = min(t1, t2);
        b = max(t1, t2);
        lmax = max(lmax, a);
        rmin = min(rmin, b);
    }
    if (x >= lmax && x <= rmin)
        cout << 0;
    else if (lmax <= rmin)
        cout << min(abs(x - lmax), abs(x - rmin));
    else
        cout << -1;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define FOR(n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
typedef pair<int, int> pi;

int main()
{
    int n, x;
    cin >> n >> x;
    int a[n], b[n], lmax = -1, rmin = INT_MAX;
    FOR(n)
    {
        int t1, t2;
        cin >> t1 >> t2;
        a[i] = min(t1, t2);
        b[i] = max(t1, t2);
        lmax = max(lmax, a[i]);
        rmin = min(rmin, b[i]);
    }

    if (lmax <= rmin)
    {
        cout << x - lmax;
    }
    else
    {
        if (x > lmax)
            cout << x - rmin;
        else
            cout << x - lmax;
    }
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define FOR(n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
typedef pair<int, int> pi;

int main()
{
    int n, x;
    cin >> n >> x;
    int a[n], b[n], lmax = -1, rmin = INT_MAX;
    FOR(n)
    {
        int t1, t2;
        cin >> t1 >> t2;
        a[i] = min(t1, t2);
        b[i] = max(t1, t2);
        lmax = max(lmax, a[i]);
        rmin = min(rmin, b[i]);
    }
    if (x >= lmax && x <= rmin)
        cout << 0;
    else if (lmax <= rmin)
        cout << abs(x - lmax);
    else
        cout << -1;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define FOR(n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
typedef pair<int, int> pi;

int main()
{
    int n, x;
    cin >> n >> x;
    int a[n], b[n], lmax = -1, rmin = INT_MAX;
    FOR(n)
    {
        int t1, t2;
        cin >> t1 >> t2;
        a[i] = min(t1, t2);
        b[i] = max(t1, t2);
        lmax = max(lmax, a[i]);
        rmin = min(rmin, b[i]);
    }
    if (x >= lmax && x <= rmin)
        cout << 0;
    else if (lmax <= rmin)
        cout << abs(x - lmax);
    else
    {
        if (x > lmax)
            cout << x - rmin;
        else
            cout << abs(x - lmax);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define FOR(n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
typedef pair<int, int> pi;

int main()
{
    int n, x;
    cin >> n >> x;
    int a[n], b[n], lmax = -1, rmin = INT_MAX;
    FOR(n)
    {
        int t1, t2;
        cin >> t1 >> t2;
        a[i] = min(t1, t2);
        b[i] = max(t1, t2);
        lmax = max(lmax, a[i]);
        rmin = min(rmin, b[i]);
    }

    if (lmax <= rmin)
    {
        cout << abs(x - lmax);
    }
    else
    {
        if (x > lmax)
            cout << x - rmin;
        else
            cout << abs(x - lmax);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define Tupizm ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define ONE exit(0);
#define nl "\n"

#define ff first
#define ss second

#define bb begin()
#define ee end()

#define pb push_back
#define ppb pop_back

#define ub upper_bound
#define lb lower_bound

#define maxe max_element
#define mine min_element

#define all(x) x.begin(), x.end()

#define sz(x) (int) x.size()

typedef long long ll;
typedef double db;

const int N = 1e2 + 5;
const int M = 1e9 + 7;

int n, k;
int a[N], b[N];
int c[N];
int ans = M;
int main(){

    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);

    Tupizm

    cin >> n >> k;
    int q = M, w = 0;
    for(int i = 0; i < n; ++i){
        cin >> a[i] >> b[i];
        if(a[i] > b[i]){
            swap(a[i], b[i]);
        }
        for(int j = a[i]; j <= b[i]; ++j){
            c[j]++;
        }
        q = min(q, a[i]);
        w = max(w, b[i]);
    }
    for(int i = q; i <= w; ++i){
        if(c[i] == 0){
            cout << "-1" << nl;
            ONE
        }
    }
    for(int i = q; i <= w; ++i){
        int e = 0;
        for(int j = 0; j < n; ++j){
            if(a[j] > i || b[j] < i){
                e++;
                break;
            }
        }
        if(e == 0){
            int x = max(i, k);
            int y = min(i, k);
            ans = min(ans, x - y);
        }
    }
    cout << ans << nl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Tupizm ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define ONE exit(0);
#define nl "\n"

#define ff first
#define ss second

#define bb begin()
#define ee end()

#define pb push_back
#define ppb pop_back

#define ub upper_bound
#define lb lower_bound

#define maxe max_element
#define mine min_element

#define all(x) x.begin(), x.end()

#define sz(x) (int) x.size()

typedef long long ll;
typedef double db;

const int N = 1e2 + 5;
const int M = 1e9 + 7;

int n, k;
int a[N], b[N];
int c[N];
int ans = M;
int main(){

    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);

    Tupizm

    cin >> n >> k;
    int q = M;
    int w = 0;
    for(int i = 1; i <= n; ++i){
        cin >> a[i] >> b[i];
        if(a[i] > b[i]){
            swap(a[i], b[i]);
        }
        for(int j = a[i]; j <= b[i]; ++j){
            c[j]++;
        }
        q = min(a[i], q);
        w = max(b[i], w);
    }
    for(int i = q; i <= w; ++i){
        if(c[i] == 0){
            cout << "-1" << nl;
            ONE
        }
    }
    for(int i = q; i <= w; ++i){
        int z = 0;
        for(int j = 1; j <= n; ++j){
            if(a[j] > i || b[j] < i){
                z++;
                break;
            }
        }
        if(z == 0){
            ans = min(ans, abs(k - i));
        }
    }
    cout << ans << nl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Tupizm ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define ONE exit(0);
#define nl "\n"

#define ff first
#define ss second

#define bb begin()
#define ee end()

#define pb push_back
#define ppb pop_back

#define ub upper_bound
#define lb lower_bound

#define maxe max_element
#define mine min_element

#define all(x) x.begin(), x.end()

#define sz(x) (int) x.size()

typedef long long ll;
typedef double db;

const int N = 1e2 + 5;
const int M = 1e9 + 7;

int n, k;
int a[N], b[N];
int c[N];
int ans = M;
int main(){

    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);

    Tupizm

    cin >> n >> k;
    int q = M, w = 0;
    for(int i = 1; i <= n; ++i){
        cin >> a[i] >> b[i];
        if(a[i] > b[i]){
            swap(a[i], b[i]);
        }
        for(int j = a[i]; j <= b[i]; ++j){
            c[j]++;
        }
        q = min(q, a[i]);
        w = max(w, b[i]);
    }
    for(int i = q; i <= w; ++i){
        if(c[i] == 0){
            cout << "-1" << nl;
            ONE
        }
    }
    for(int i = q; i <= w; ++i){
        int e = 0;
        for(int j = q; j <= w; ++j){
            if(a[i] > i || b[i] < i){
                e++;
            }
        }
        if(e == 0){
            ans = min(ans, abs(k - i));
        }
    }
    cout << ans << nl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Tupizm ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define ONE exit(0);
#define nl "\n"

#define ff first
#define ss second

#define bb begin()
#define ee end()

#define pb push_back
#define ppb pop_back

#define ub upper_bound
#define lb lower_bound

#define maxe max_element
#define mine min_element

#define all(x) x.begin(), x.end()

#define sz(x) (int) x.size()

typedef long long ll;
typedef double db;

const int N = 1e2 + 5;
const int M = 1e9 + 7;

int n, k;
int a[N], b[N];
int ans = M;
int main(){

    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);

    Tupizm

    cin >> n >> k;
    int q = M;
    int w = 0;
    for(int i = 1; i <= n; ++i){
        cin >> a[i] >> b[i];
        if(a[i] > b[i]){
            swap(a[i], b[i]);
        }
        q = min(a[i], q);
        w = max(b[i], w);
    }
    for(int i = q; i <= w; ++i){
        int z = 0;
        for(int j = 1; j <= n; ++j){
            if(a[j] > i || b[j] < i){
                z++;
                break;
            }
        }
        if(z == 0){
            ans = min(ans, abs(k - i));
        }
    }
    if(ans == M){
        cout << "-1" << nl;
        ONE
    }
    cout << ans << nl;
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

struct SportMan
{
    int start, end;
};

int getDistance(int x0, SportMan man);

int main()
{
    int n, x0;
    cin >> n >> x0;
    SportMan mans[n];
    for (int i = 0; i < n; i++)
    {
        cin >> mans[i].start >> mans[i].end;
    }
    int minDistance = getDistance(x0, mans[0]);
    for (int i = 1; i < n; i++)
    {
        int temp = getDistance(x0, mans[i]);
        if (minDistance < temp)
        {
            minDistance = temp;
        }
    }

    cout << minDistance;
    return 0;
}

int getDistance(int x0, SportMan man)
{
    int distance;
    if ((man.start <= x0 && x0 <= man.end) || (man.end <= x0 && x0 <= man.start))
    {
        distance = 0;
    }
    else
    {
        distance = min(abs(x0 - man.start), abs(x0 - man.end));
    }
    return distance;
}


#include <iostream>
#include <cmath>
using namespace std;

struct SportMan
{
    int start, end;
};

int getDistance(int x0, SportMan man);

int main()
{
    int n, x0;
    cin >> n >> x0;
    SportMan mans[n];
    for (int i = 0; i < n; i++)
    {
        cin >> mans[i].start >> mans[i].end;
    }
    int minDistance = getDistance(x0, mans[0]);
    for (int i = 1; i < n; i++)
    {
        int temp = getDistance(x0, mans[i]);
        if (temp < minDistance)
        {
            minDistance = temp;
        }
    }

    cout << minDistance;
    return 0;
}

int getDistance(int x0, SportMan man)
{
    int distance;
    if ((man.start <= x0 && x0 <= man.end) || (man.end <= x0 && x0 <= man.start))
    {
        distance = 0;
    }
    else
    {
        distance = min(abs(x0 - man.start), abs(x0 - man.end));
    }
    return distance;
}


#include <iostream>
#include <cmath>
using namespace std;

struct SportMan
{
    int start, end;
};

int getDistance(int n, int x0, SportMan mans[]);

int main()
{
    int n, x0;
    cin >> n >> x0;
    SportMan mans[n];
    for (int i = 0; i < n; i++)
    {
        cin >> mans[i].start >> mans[i].end;
        if (mans[i].end < mans[i].start)
        {
            swap(mans[i].start, mans[i].end);
        }
    }

    int minDistance = getDistance(n, x0, mans);
    cout << minDistance;
    return 0;
}

int getDistance(int n, int x0, SportMan mans[])
{
    int posStart = mans[0].start,
    posEnd = mans[0].end;

    for (int i = 1; i < n; i++)
    {
        if (mans[i].start > posStart)
        {
            posStart = mans[i].start;
        }
        if (mans[i].end < posEnd)
        {
            posEnd = mans[i].end;
        }
    }

    int distance;
    if (posEnd < posStart)
    {
        distance = -1;
    }
    else if (posStart <= x0 && x0 <= posEnd)
    {
        distance = 0;
    }
    else
    {
        distance = min(abs(x0 - posStart), abs(x0 - posEnd));
    }

    return distance;
}


#include <iostream>
#include <cmath>
using namespace std;

struct SportMan
{
    int start, end;
};

int getDistance(int n, int x0, SportMan mans[]);

int main()
{
    int n, x0;
    cin >> n >> x0;
    SportMan mans[n];
    for (int i = 0; i < n; i++)
    {
        cin >> mans[i].start >> mans[i].end;
        if (mans[i].end < mans[i].start)
        {
            swap(mans[i].start, mans[i].end);
        }
    }

    int minDistance = getDistance(n, x0, mans);
    cout << minDistance;
    return 0;
}

int getDistance(int n, int x0, SportMan mans[])
{
    int posStart = mans[0].start,
    posEnd = mans[0].end;

    for (int i = 1; i < n; i++)
    {
        if (mans[i].start > posStart)
        {
            posStart = mans[i].start;
        }
        if (mans[i].end < posEnd)
        {
            posEnd = mans[i].end;
        }
    }

    int distance;
    if (posEnd < posStart)
    {
        distance = -1;
    }
    else
    {
        distance = min(abs(x0 - posStart), abs(x0 - posEnd));
    }

    return distance;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <list>
#include <map>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <climits>
#include <cstdlib>
using namespace std;

#define rep(i, a, b) for(int i=a; i<b; i++)
#define lets_naacho ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define lli long long int
#define ulli unsigned long long int
#define ii pair<int,int>
#define MOD 10000000007
#define vii vector<ii>
#define vi vector<int>
#define space " "
#define imin INT_MIN
#define imax INT_MAX

int main(void){
    lets_naacho;
    int n, x;
    cin >> n >> x;
    vi arr(1100, 0);
    int a, b;
    for(int i=0; i<n; i++){
        cin >> a >> b;
        if(a>b){
            arr[b]++;
            arr[a+1]--;
        }
        else{
            arr[a]++;
            arr[b+1]--;
        }
    }

    for(int i=1; i<int(arr.size()); i++){
        arr[i] += arr[i-1];
    }

    int t = *(max_element(all(arr)));
    int f, l;

    if(t != n){
        cout << -1;
    }
    else{

        rep(i, 0, int(arr.size())){
            if(arr[i] == t){
                f = i;
                break;
            }
        }

        rep(i, 0, int(arr.size())){
            if(arr[i] == t){
                l = i;
            }
            if(arr[i] == 0)
                break;
        }

        if(x>=f && x<=l)
            cout << 0;
        else
            cout << min(abs(x-f), abs(x-l));
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <list>
#include <map>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <climits>
#include <cstdlib>
using namespace std;

#define rep(i, a, b) for(int i=a; i<b; i++)
#define lets_naacho ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define lli long long int
#define ulli unsigned long long int
#define ii pair<int,int>
#define MOD 10000000007
#define vii vector<ii>
#define vi vector<int>
#define space " "
#define imin INT_MIN
#define imax INT_MAX

int main(void){
    lets_naacho;
    int n, x;
    cin >> n >> x;
    vi arr(1100, 0);
    int a, b;
    for(int i=0; i<n; i++){
        cin >> a >> b;
        if(a>b){
            arr[b] = 1;
            arr[a+1] = -1;
        }
        else{
            arr[a] = 1;
            arr[b+1] = -1;
        }
    }

    for(int i=1; i<int(arr.size()); i++){
        arr[i] += arr[i-1];
    }

    int t = *(max_element(all(arr)));
    int f, l;

    if(t != n){
        cout << -1;
        return 0;
    }

    rep(i, 0, int(arr.size())){
        if(arr[i] == t){
            f = i;
            break;
        }
    }

    rep(i, 0, int(arr.size())){
        if(arr[i] == t){
            l = i;
        }
        if(arr[i] == 0)
            break;
    }

    if(x>f && x<l)
        cout << 0;
    else
        cout << min(abs(x-f), abs(x-l));
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <list>
#include <map>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <climits>
#include <cstdlib>
using namespace std;

#define rep(i, a, b) for(int i=a; i<b; i++)
#define lets_naacho ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define lli long long int
#define ulli unsigned long long int
#define ii pair<int,int>
#define MOD 10000000007
#define vii vector<ii>
#define vi vector<int>
#define space " "
#define imin INT_MIN
#define imax INT_MAX

int main(void){
    lets_naacho;
    int n, x;
    cin >> n >> x;
    vi arr(1100, 0);
    int a, b;
    for(int i=0; i<n; i++){
        cin >> a >> b;
        if(a>b){
            arr[b]++;
            arr[a+1]--;
        }
        else{
            arr[a]++;
            arr[b+1]--;
        }
    }

    for(int i=1; i<int(arr.size()); i++){
        arr[i] += arr[i-1];
    }

    int t = *(max_element(all(arr)));
    int f, l;

    if(t != n){
        cout << -1;
    }
    else{

        rep(i, 0, int(arr.size())){
            if(arr[i] == t){
                f = i;
                break;
            }
        }

        rep(i, 0, int(arr.size())){
            if(arr[i] == t){
                l = i;
            }
        }

        if(x>=f && x<=l)
            cout << 0;
        else
            cout << min(abs(x-f), abs(x-l));
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <list>
#include <map>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <climits>
#include <cstdlib>
using namespace std;

#define rep(i, a, b) for(int i=a; i<b; i++)
#define lets_naacho ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define lli long long int
#define ulli unsigned long long int
#define ii pair<int,int>
#define MOD 10000000007
#define vii vector<ii>
#define vi vector<int>
#define space " "
#define imin INT_MIN
#define imax INT_MAX

int main(void){
    lets_naacho;
    int n, x;
    cin >> n >> x;
    vi arr(1100, 0);
    int a, b;
    for(int i=0; i<n; i++){
        cin >> a >> b;
        if(a>b){
            arr[b] = 1;
            arr[a+1] = -1;
        }
        else{
            arr[a] = 1;
            arr[b+1] = -1;
        }
    }

    for(int i=1; i<int(arr.size()); i++){
        arr[i] += arr[i-1];
    }

    int t = *(max_element(all(arr)));
    int f, l;

    if(t != n){
        cout << -1;
        return 0;
    }

    rep(i, 0, int(arr.size())){
        if(arr[i] == t){
            f = i;
            break;
        }
    }

    rep(i, 0, int(arr.size())){
        if(arr[i] == t){
            l = i;
        }
        if(arr[i] == 0)
            break;
    }

    cout << min(abs(x-f), abs(x-l));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
  int n,x0;
  cin>>n>>x0;

  int arr[1001];
  memset(arr,0,sizeof(arr));

  int minpos=1002;

  for(int i=0;i<n;i++)
  {
  	int a,b;
  	cin>>a>>b;

  	if(a>b)
  	{
  	   	int temp=b;
		b=a;
		a=temp;
	}

  	for(int j=a;j<=b;j++)
  	{
  		arr[j]++;
	}

  }

    /*cout<<"Prinitng track state2 "<<endl;

	for(int k=0;k<1001;k++)
	cout<<arr[k]<<" ";
	cout<<endl;
  */
  int left,right;

  for(right=x0;right<1001;right++)
  {
  //	cout<<"Checking "<<arr[i]<<endl;
  	if(arr[right]==n)
  	{
  	//	cout<<"Found n"<<endl;
	  break;
 	 }
}

  //cout<<"right end found at "<<right<<endl;
  minpos=right-x0;

  for(left=x0;left>=0;left--)
  {
  	if(arr[left]==n)
  	break;
  }

 // cout<<"Left end found at "<<left<<endl;

  if(right==1001 &&left==-1)
  {
  	cout<<"-1";
  	return 0;
  }

  minpos=min(minpos,x0-left);

  cout<<minpos;

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
  int n,x0;
  cin>>n>>x0;

  int arr[1001];
  memset(arr,0,sizeof(arr));

  int minpos=1002;

  for(int i=0;i<n;i++)
  {
  	int a,b;
  	cin>>a>>b;

  	if(a>b)
  	{
  	   int temp=b;
		 b=a;
		 a=temp;
	}

  	for(int j=a;j<=b;j++)
  	{
  		arr[j]++;
		if(arr[j]==n && j<minpos)
        {
        //	cout<<"Updating mispos at "<<j<<endl;
        	minpos=j;
		}
	}

//	cout<<"Prinitng track state "<<endl;

//	for(int k=0;k<1001;k++)
//	cout<<arr[k]<<" ";
//	cout<<endl;
  }

  if(minpos==1002)
  cout<<"-1";
  else
  cout<<abs(x0-minpos);

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
  int n,x0;
  cin>>n>>x0;

  int arr[1001];
  memset(arr,0,sizeof(arr));

  int minpos=1002;

  for(int i=0;i<n;i++)
  {
  	int a,b;
  	cin>>a>>b;

  	if(a>b)
  	{
  	   	int temp=b;
		b=a;
		a=temp;
	}

  	for(int j=a;j<=b;j++)
  	{
  		arr[j]++;
	}

  }

  /*  cout<<"Prinitng track state2 "<<endl;

	for(int k=0;k<1001;k++)
	cout<<arr[k]<<" ";
	cout<<endl;
  */
  int i;
 // cout<<"Starting at "<<x0<<endl;
  for(i=x0;i<1001;i++)
  {
  //	cout<<"Checking "<<arr[i]<<endl;
  	if(arr[i]==n)
  	{
  	//	cout<<"Found n"<<endl;
	  break;
 	 }
}

  //cout<<"right end found at "<<i<<endl;
  minpos=i-x0;
  //cout<<"minpos = "<<minpos<<endl;

  for(i=x0;i>=0;i--)
  {
  	if(arr[i]==n)
  	break;
  }

  //cout<<"Left end found at "<<i<<endl;

  minpos=min(minpos,x0-i);

  if(minpos==1002)
  cout<<"-1";
  else
  cout<<minpos;

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
  int n,x0;
  cin>>n>>x0;

  int arr[1001];
  memset(arr,0,sizeof(arr));

  int minpos=1002;

  for(int i=0;i<n;i++)
  {
  	int a,b;
  	cin>>a>>b;

  	if(a>b)
  	{
  	   	int temp=b;
		b=a;
		a=temp;
	}

  	for(int j=a;j<=b;j++)
  	{
  		arr[j]++;
	}

  }

  /*  cout<<"Prinitng track state2 "<<endl;

	for(int k=0;k<1001;k++)
	cout<<arr[k]<<" ";
	cout<<endl;
  */

  int left,right;

  for(right=x0;right<1001;right++)
  {
  	if(arr[right]==n)
  	{
	  break;
 	 }
}

  //cout<<"right end found at "<<right<<endl;

  if(right!=1001)
  minpos=right-x0;

  for(left=x0;left>=0;left--)
  {
  	if(arr[left]==n)
  	break;
  }

  //cout<<"Left end found at "<<left<<endl;

  if(right==1001 &&left==-1)
  {
  	cout<<"-1";
  	return 0;
  }

  if(left!=-1)
  minpos=min(minpos,x0-left);

  cout<<minpos;

}


#include<iostream>
#include<bits/stdc++.h>
#include<string>
#include<vector>
#include<cmath>
#include<sstream>
#include<map>
#include<set>
#include<iomanip>
typedef long long int ll;
typedef long double ld;
#define fo(i,a,b) for(int i=a;i<b;i++)
#define FO(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define all(a)  a.begin(),a.end()
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, x0;
	cin >> n >> x0;
	int a[1001] = {}, a0, b0;
	fo(i, 0, n) {
		cin >> a0 >> b0;
		fo(j, min(a0, b0), max(a0, b0) + 1)
		a[j]++;
	}
	vector<int>ans;
	fo(i, 0, 1001) {
		if (a[i] == n)
			ans.pb(abs(x0 - i));
	}
	if (ans.size() != 0)
		cout << *min_element(all(ans));
	else
		cout << -1;

}


#include<iostream>
#include<bits/stdc++.h>
#include<string>
#include<vector>
#include<cmath>
#include<sstream>
#include<map>
#include<set>
#include<iomanip>
typedef long long int ll;
typedef long double ld;
#define fo(i,a,b) for(int i=a;i<b;i++)
#define FO(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define all(a)  a.begin(),a.end()
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, x0;
	cin >> n >> x0;
	int a[1001] = {}, a0, b0;
	fo(i, 0, n) {
		cin >> a0 >> b0;
		fo(j, min(a0, b0), max(a0, b0) + 1)
		a[j]++;
	}
	if (a[x0] == n) {
		cout << 0;
		return 0;
	}
	fo(i, 0, 1001) {
		if (a[i] == n) {
			cout << abs(x0 - i);
			return 0;
		}
	}
	cout << -1;

}


#include<iostream>
#include<bits/stdc++.h>
#include<string>
#include<vector>
#include<cmath>
#include<sstream>
#include<map>
#include<set>
#include<iomanip>
typedef long long int ll;
typedef long double ld;
#define fo(i,a,b) for(int i=a;i<b;i++)
#define FO(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define all(a)  a.begin(),a.end()
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, x0;
	cin >> n >> x0;
	int a[1001] = {}, a0, b0;
	fo(i, 0, n) {
		cin >> a0 >> b0;
		fo(j, min(a0, b0), max(a0, b0) + 1)
		a[j]++;
	}
	fo(i, 0, 1001) {
		if (a[i] == n) {
			cout << abs(x0 - i);
			return 0;
		}
	}
	cout << -1;

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,x;
    cin>>n>>x;
    vector<pair<int,int> > pp;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        pp.push_back(make_pair(min(a,b),max(a,b)));
    }
    int ans=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(pp[i].second<pp[j].first){
                ans=-1;
                break;
            }
        }
    }
    if(ans==-1){
        cout<<-1;
        return 0;
    }
    int rangex=INT_MIN,rangey=INT_MAX;
    for(int i=0;i<n;i++){
        if(rangex<pp[i].first)
            rangex=pp[i].first;
        if(rangey>pp[i].second)
            rangey=pp[i].second;
    }
    if(x>rangex && x<rangey)
        ans=0;
    else if(x<rangex)
        ans=rangex-x;
    else
        ans=x-rangey;
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,x;
    cin>>n>>x;
    vector<pair<int,int> > pp;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        pp.push_back(make_pair(min(a,b),max(a,b)));
    }
    int ans=0;
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            if(pp[i].second<pp[j].first){
                ans=-1;
                break;
            }
        }
    }
    if(ans==-1){
        cout<<-1;
        return 0;
    }
    int rangex=INT_MIN,rangey=INT_MAX;
    for(int i=0;i<n;i++){
        if(rangex<pp[i].first)
            rangex=pp[i].first;
        if(rangey>pp[i].second)
            rangey=pp[i].second;
    }
    if(x>rangex && x<rangey)
        ans=0;
    else if(x<rangex)
        ans=rangex-x;
    else
        ans=x-rangey;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,x;
    cin>>n>>x;
    vector<pair<int,int> > pp;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        pp.push_back(make_pair(min(a,b),max(a,b)));
    }
    int ans=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(pp[i].second<pp[j].first){
                ans=-1;
                break;
            }
        }
    }
    if(ans==-1){
        cout<<-1;
        return 0;
    }
    int rangex=INT_MIN,rangey=INT_MAX;
    for(int i=0;i<n;i++){
        if(rangex<pp[i].first)
            rangex=pp[i].first;
        if(rangey>pp[i].second)
            rangey=pp[i].second;
    }
    if(x>=rangex && x<=rangey)
        ans=0;
    else if(x<rangex)
        ans=rangex-x;
    else
        ans=x-rangey;
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,x;
    cin>>n>>x;
    vector<pair<int,int> > pp;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        pp.push_back(make_pair(min(a,b),max(a,b)));
    }
    int ans=0;
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            if(pp[i].second<pp[j].first){
                ans=-1;
                break;
            }
        }
    }
    if(ans==-1){
        cout<<-1;
        return 0;
    }
    int rangex=INT_MIN,rangey=INT_MAX;
    for(int i=0;i<n;i++){
        if(rangex<pp[i].first)
            rangex=pp[i].first;
        if(rangey>pp[i].second)
            rangey=pp[i].second;
    }
    if(x>=rangex && x<=rangey)
        ans=0;
    else if(x<rangex)
        ans=rangex-x;
    else
        ans=x-rangey;
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,x;
    cin>>n>>x;
    vector<pair<int,int> > pp;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        pp.push_back(make_pair(min(a,b),max(a,b)));
    }
    int ans=0;
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            if(pp[i].second<pp[j].first){
                ans=-1;
                break;
            }
        }
    }
    if(ans==-1){
        cout<<-1;
        return 0;
    }
    int rangex=INT_MIN,rangey=INT_MAX;
    for(int i=0;i<n;i++){
        if(rangex<pp[i].first)
            rangex=pp[i].first;
        if(rangey>pp[i].second)
            rangey=pp[i].second;
    }
    if(x>rangex && x<rangey)
        ans=0;
    else if(x<rangex)
        ans=rangex-x;
    else
        ans=x-rangey;
    cout<<ans;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <math.h>

#define fill(v,val) fill((v).begin(), (v).end(), (val))
#define fillt(tab, n, val) fill((t), (t) + (n) ,(val))
#define MAX 100005
#define INF 9999999
#define in(a,b)  ( (b).find(a) != (b).end())
#define clr(a,b) memset((a), (b), sizeof((a)))
#define pb       push_back
#define all(a)   a.begin(), a.end()
#define mp         make_pair
const long double pi = 3.141592653589793238L;
/**
*
* @Author Lahia omar
*/

using namespace std;
int n, x;
int a, b,mi, ma ;
int M = -1 , N = INF;
vector<int> tab(1005, 0);
vector<int> ta(1005, 0);
void mm() {
	if (a > b) {
		mi = b;
		ma = a;
	}
	else {
		mi = a;
		ma = b;
	}
}
void f() {
	mm();
	//cout << mi << ' ' << ma << endl;
	for (int i = mi; i <= ma; i++) {
		ta[i]++;
	}
}
bool test() {
	//cout << N << ' ' << M << endl;
	for (int i = N; i <=M; i++) {
		if (ta[i] == 0)
			return false;
	}
	return true;
}
int solve() {
	bool flag = true;
	int i = x;
	int j = x;
	if (ta[x] == n )
		return 0;
	i++; j--;
	while (flag) {
		if (i + 1 <= M) {
			//cout << " i = " << i << endl;
			if (ta[(++i)] == n) {
				//cout << " i = " << i << endl;
				return abs(x - i);
			}
		}
		else
				i++;
		if (j - 1 >= N)
			if (ta[--j] == n)
				return abs(x - j);
			else
				j--;

		if (i >M && j <N)
			return -1;
	}
}
int main(int argc, char* argv[]) {
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin >> n >> x;
	for (int i = 0; i < n;i++) {
		cin >> a >> b;
		tab[a]++;
		tab[b]++;
		f();
		if (M < ma) M = ma;
		if (N > mi) N = mi;
	}
	if (test()) {
		cout << solve() << endl;
	}
	else
		cout << -1 << endl;
	//cout << ta[5] << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <math.h>

#define fill(v,val) fill((v).begin(), (v).end(), (val))
#define fillt(tab, n, val) fill((t), (t) + (n) ,(val))
#define MAX 100005
#define INF 9999999
#define in(a,b)  ( (b).find(a) != (b).end())
#define clr(a,b) memset((a), (b), sizeof((a)))
#define pb       push_back
#define all(a)   a.begin(), a.end()
#define mp         make_pair
const long double pi = 3.141592653589793238L;
/**
*
* @Author Lahia omar
*/

using namespace std;
int n, x;
int a, b,mi, ma ;
int M = -1 , N = INF;
vector<bool> tab(1005, false);
vector<bool> ta(1005, false);
void mm() {
	if (a > b) {
		mi = b;
		ma = a;
	}
	else {
		mi = a;
		ma = b;
	}
}
void f() {
	mm();
	//cout << mi << ' ' << ma << endl;
	for (int i = mi; i <= ma; i++) {
		ta[i] = true;
	}
}
bool test() {

	for (int i = N; i <M; i++) {
		if (ta[i] == false)
			return false;
	}
	return true;
}
int solve() {
	int count = 0;
	bool flag = true;
	int i = x;
	int j = x;
	if (tab[x] == true)
		return 1;
	while (flag) {
		if (tab[(++i)] == true && i<=M) {
			return abs(x - i);
		}
		if (tab[--j] ==  true && j>=0)
			return abs(x-j);
		count++;
		if (i > n && j < 0)
			return -1;
	}
}
int main(int argc, char* argv[]) {
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin >> n >> x;
	for (int i = 0; i < n;i++) {
		cin >> a >> b;
		tab[a] = true;
		tab[b] = true;
		f();
		if (M < ma) M = ma;
		if (N > mi) N = mi;
	}
	if (test()) {
		cout << solve() << endl;
	}
	else
		cout << -1 << endl;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <math.h>

#define fill(v,val) fill((v).begin(), (v).end(), (val))
#define fillt(tab, n, val) fill((t), (t) + (n) ,(val))
#define MAX 100005
#define INF 9999999
#define in(a,b)  ( (b).find(a) != (b).end())
#define clr(a,b) memset((a), (b), sizeof((a)))
#define pb       push_back
#define all(a)   a.begin(), a.end()
#define mp         make_pair
const long double pi = 3.141592653589793238L;
/**
*
* @Author Lahia omar
*/

using namespace std;
int n, x;
int a, b;
int mi = INF, ma = -1;
vector<int> tab(1005,0);
void f(int a,int b) {
	for (int i = a; i <= b; i++) {
		tab[i]++;
	}
}
bool test() {
	for (int i = mi; i <= ma; i++) {
		if (tab[i] == 0)
			return false;
	}
	return true;
}
int solve() {
	int i = x;
	int j = x;
	if (tab[x] == n)
		return 0;
	i++; j--;
	while (true) {
		if (tab[i] == n) {
			return abs(x - i);
		}
		if (tab[j] == n)
			return abs(x-j);
		i++;
		j--;
	}
}
int main(int argc, char* argv[]) {
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin >> n >> x;
	for (int i = 0; i < n; i++) {
		cin >> a >> b;
		f(min(a,b),max(a,b));
	}
	if (mi > min(a, b))
		mi = min(a, b);
	if (ma < max(a, b))
		ma = max(a,b);
	if (test()) {
		cout << solve() << endl;
	}
	else
		cout << -1 << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <math.h>

#define fill(v,val) fill((v).begin(), (v).end(), (val))
#define fillt(tab, n, val) fill((t), (t) + (n) ,(val))
#define MAX 100005
#define INF 9999999
#define in(a,b)  ( (b).find(a) != (b).end())
#define clr(a,b) memset((a), (b), sizeof((a)))
#define pb       push_back
#define all(a)   a.begin(), a.end()
#define mp         make_pair
const long double pi = 3.141592653589793238L;
/**
*
* @Author Lahia omar
*/

using namespace std;
int n, x;
int a, b;
int mi = INF, ma = -1;
vector<int> tab(1005,0);
void f(int a,int b) {
	for (int i = a; i <= b; i++) {
		tab[i]++;
	}
}
bool test() {
	for (int i = mi; i <= ma; i++) {
		if (tab[i] == 0)
			return false;
	}
	return true;
}
int solve() {
	int i = x;
	int j = x;
	if (tab[x] == n)
		return 0;
	i++; j--;
	while (true) {
		if(i<=ma)
			if (tab[i] == n) {
				return abs(x - i);
			}
		if(j>=mi)
			if (tab[j] == n)
				return abs(x-j);
		i++;
		j--;
		if (i > ma && j < mi)
			return -1;
	}
}
int main(int argc, char* argv[]) {
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin >> n >> x;
	for (int i = 0; i < n; i++) {
		cin >> a >> b;
		f(min(a,b),max(a,b));
	}
	if (mi > min(a, b))
		mi = min(a, b);
	if (ma < max(a, b))
		ma = max(a,b);
	if (test()) {
		cout << solve() << endl;
	}
	else
		cout << -1 << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <math.h>

#define fill(v,val) fill((v).begin(), (v).end(), (val))
#define fillt(tab, n, val) fill((t), (t) + (n) ,(val))
#define MAX 100005
#define INF 9999999
#define in(a,b)  ( (b).find(a) != (b).end())
#define clr(a,b) memset((a), (b), sizeof((a)))
#define pb       push_back
#define all(a)   a.begin(), a.end()
#define mp         make_pair
const long double pi = 3.141592653589793238L;
/**
*
* @Author Lahia omar
*/

using namespace std;
int n, x;
int a, b,mi, ma ;
int M = -1 , N = INF;
vector<bool> tab(1005, false);
vector<bool> ta(1005, false);
void mm() {
	if (a > b) {
		mi = b;
		ma = a;
	}
	else {
		mi = a;
		ma = b;
	}
}
void f() {
	mm();
	//cout << mi << ' ' << ma << endl;
	for (int i = mi; i <= ma; i++) {
		ta[i] = true;
	}
}
bool test() {

	for (int i = N; i <M; i++) {
		if (ta[i] == false)
			return false;
	}
	return true;
}
int solve() {
	int count = 0;
	bool flag = true;
	int i = x;
	int j = x;
	if (tab[x] == true)
		return 0;
	while (flag) {
		if (tab[(++i)] == true && i<=M) {
			return abs(x - i);
		}
		if (tab[--j] ==  true && j>=0)
			return abs(x-j);
		count++;
		if (i > n && j < 0)
			return -1;
	}
}
int main(int argc, char* argv[]) {
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin >> n >> x;
	for (int i = 0; i < n;i++) {
		cin >> a >> b;
		tab[a] = true;
		tab[b] = true;
		f();
		if (M < ma) M = ma;
		if (N > mi) N = mi;
	}
	if (test()) {
		cout << solve() << endl;
	}
	else
		cout << -1 << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int x,n;
int paint[1000];
int main()
{
	int a,b;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		if(a<b)
		{
			for(int j=a;j<=b;j++)
			{
				paint[j]++;;
			}
		}
		else
		{
			for(int j=b;j<=a;j++)
			{
				paint[j]++;
			}
		}
	}
	for(int i=x,k=x;i<x+1000;i++,k--)
	{
		if(k>=0&&i<1000)
		{
			if(paint[i]==n)
			{
			cout<<i-x;
			return 0;
			}
			else if(paint[k]==n)
			{
			cout<<x-k;
			return 0;
			}
		}
		else if(k<0&&i<1000)
		{
			if(paint[i]==n)
			{
			cout<<i-x;
			return 0;
			}
		}
		else if(k>=0&&i>=1000)
		{
			if(paint[k]==n)
			{
			cout<<x-k;
			return 0;
			}
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int x,n;
int paint[1000];
int main()
{
	int a,b;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		if(a<b)
		{
			for(int j=a;j<=b;j++)
			{
				paint[j]++;;
			}
		}
		else
		{
			for(int j=b;j<=a;j++)
			{
				paint[j]++;
			}
		}
	}
	for(int i=x,k=x;i<x+1000;i++,k--)
	{
		if(k>=0)
		{
			if(paint[i]==n)
			{
			cout<<i-x;
			return 0;
			}
			else if(paint[k]==n)
			{
			cout<<x-k;
			return 0;
			}
		}
		else
		{
			if(paint[i]==n)
			{
			cout<<i-x;
			return 0;
			}
		}
		if(i>1000)
		{
			if(paint[k]==n)
			{
			cout<<x-k;
			return 0;
			}
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int x,n;
int paint[1000];
int main()
{
	int a,b;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		if(a<b)
		{
			for(int j=a;j<=b;j++)
			{
				paint[j]++;;
			}
		}
		else
		{
			for(int j=b;j<=a;j++)
			{
				paint[j]++;
			}
		}
	}
	for(int i=x,k=x;i<x+1000;i++,k--)
	{
		if(k>=0&&i<=1000)
		{
			if(paint[i]==n)
			{
			cout<<i-x;
			return 0;
			}
			else if(paint[k]==n)
			{
			cout<<x-k;
			return 0;
			}
		}
		else if(k<0&&i<=1000)
		{
			if(paint[i]==n)
			{
			cout<<i-x;
			return 0;
			}
		}
		else if(k>=0&&i<=1000)
		{
			if(paint[k]==n)
			{
			cout<<x-k;
			return 0;
			}
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int x,n;
int paint[1000];
int main()
{
	int a,b;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		if(a<b)
		{
			for(int j=a;j<=b;j++)
			{
				paint[j]++;;
			}
		}
		else
		{
			for(int j=b;j<=a;j++)
			{
				paint[j]++;
			}
		}
	}
	for(int i=x,k=x;i<1000;i++,k--)
	{
		if(k>=0)
		{
			if(paint[i]==n)
		{
			cout<<i-x;
			return 0;
		}
		else if(paint[k]==n)
		{
			cout<<x-k;
			return 0;
		}
		}
		else
		{
			if(paint[i]==n)
		{
			cout<<i-x;
			return 0;
		}
		}

	}
	for(int i=0;i<1000;i++)
	{
		cout<<paint[i];
	}
}


#include<bits/stdc++.h>
using namespace std;
int x,n;
int paint[1000];
int main()
{
	int a,b;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		if(a<b)
		{
			for(int j=a;j<=b;j++)
			{
				paint[j]++;;
			}
		}
		else
		{
			for(int j=b;j<=a;j++)
			{
				paint[j]++;
			}
		}
	}
	for(int i=x,k=x;i<x+1000;i++,k--)
	{
		if(k>=0)
		{
			if(paint[i]==n)
		{
			cout<<i-x;
			return 0;
		}
		else if(paint[k]==n)
		{
			cout<<x-k;
			return 0;
		}
		}
		else
		{
			if(paint[i]==n)
		{
			cout<<i-x;
			return 0;
		}
		}

	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int x,n;
int paint[1000];
int main()
{
	int a,b;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		if(a<b)
		{
			for(int j=a;j<=b;j++)
			{
				paint[j]++;;
			}
		}
		else
		{
			for(int j=b;j<=a;j++)
			{
				paint[j]++;
			}
		}
	}
	for(int i=x,k=x;i<1000;i++,k--)
	{
		if(k>=0)
		{
			if(paint[i]==n)
		{
			cout<<i-x;
			return 0;
		}
		else if(paint[k]==n)
		{
			cout<<x-k;
			return 0;
		}
		}
		else
		{
			if(paint[i]==n)
		{
			cout<<i-x;
			return 0;
		}
		}

	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int x,n;
int paint[1001];
int main()
{
	int a,b;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		if(a<b)
		{
			for(int j=a;j<=b;j++)
			{
				paint[j]++;;
			}
		}
		else
		{
			for(int j=b;j<=a;j++)
			{
				paint[j]++;
			}
		}
	}
	for(int i=x,k=x;i<x+1000;i++,k--)
	{
		if(k>=0&&i<1000)
		{
			if(paint[i]==n)
			{
			cout<<i-x;
			return 0;
			}
			else if(paint[k]==n)
			{
			cout<<x-k;
			return 0;
			}
		}
		else if(k<0&&i<1000)
		{
			if(paint[i]==n)
			{
			cout<<i-x;
			return 0;
			}
		}
		else if(k>=0&&i>=1000)
		{
			if(paint[k]==n)
			{
			cout<<x-k;
			return 0;
			}
		}
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;

int a[105],b[105];

int main(){
	int n,x0,x,y,i;

	cin >> n >> x0;

	for(i=0;i<n;i++){
		cin >> a[i] >> b[i];
		if(b[i]<a[i])swap(a[i],b[i]);
	//	cout << a[i] << " " << b[i] << endl;
	}
	for(i=0;i<n-1;i++){
		x=max(a[i],a[i+1]);
		y=min(b[i],b[i+1]);
	}
	if(x0>x && x0<y){
		cout << 0;
		return 0;
	}
	if(abs(x-x0)<abs(y-x0)){
		if(x<x0){
			cout << x-x0;
			return 0;
		}
		else{
			cout << x0-x;
			return 0;
		}
	}

	else{
		if(y<x0){
			cout << y-x0;
			return 0;
		}
		else{
			cout << x0-y;
			return 0;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int a[105],b[105];

int main(){
	int n,x0,x,y,i;

	cin >> n >> x0;

	for(i=0;i<n;i++){
		cin >> a[i] >> b[i];
		if(b[i]<a[i])swap(a[i],b[i]);
	//	cout << a[i] << " " << b[i] << endl;
	}
	for(i=0;i<n-1;i++){
		if(a[i+1]>=b[i]){
			cout << -1;
			return 0;
		}
		x=max(a[i],a[i+1]);
		y=min(b[i],b[i+1]);

	}
	if(x0>x && x0<y){
		cout << 0;
		return 0;
	}
	cout << min(abs(x-x0),abs(y-x0));
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int a[105],b[105];

#define INF 1e9
int main(){
	int n,x0,i;

	cin >> n >> x0;

	for(i=0;i<n;i++){
		cin >> a[i] >> b[i];
		if(b[i]<a[i])swap(a[i],b[i]);
	//	cout << a[i] << " " << b[i] << endl;
	}
	int x=-1,y=INF;

	for(i=0;i<n;i++){
		x=max(x,a[i]);
		y=min(y,b[i]);
	}

	if(x>y){
		cout << -1;
		return 0;
	}

	if(x<=x0 && x0<=y){
		cout << 0;
		return 0;
	}
	cout << min(abs(x-x0),abs(y-x0));
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int a[105],b[105];

int main(){
	int n,x0,x,y,i;

	cin >> n >> x0;

	for(i=0;i<n;i++){
		cin >> a[i] >> b[i];
		if(b[i]<a[i])swap(a[i],b[i]);
	//	cout << a[i] << " " << b[i] << endl;
	}
	for(i=0;i<n-1;i++){
		x=max(a[i],a[i+1]);
		y=min(b[i],b[i+1]);
	}
	cout << min(abs(x-x0),abs(y-x0));
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int a[105],b[105];

int main(){
	int n,x0,x,y,i;

	cin >> n >> x0;

	for(i=0;i<n;i++){
		cin >> a[i] >> b[i];
		if(b[i]<a[i])swap(a[i],b[i]);
	//	cout << a[i] << " " << b[i] << endl;
	}
	for(i=0;i<n-1;i++){
		x=max(a[i],a[i+1]);
		y=min(b[i],b[i+1]);
	}
	if(x0>x && x0<y){
		cout << 0;
		return 0;
	}
	cout << min(abs(x-x0),abs(y-x0));
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int a[105],b[105];

#define INF 1e9
int main(){
	int n,x0,i;

	cin >> n >> x0;

	for(i=0;i<n;i++){
		cin >> a[i] >> b[i];
		if(b[i]<a[i])swap(a[i],b[i]);
	//	cout << a[i] << " " << b[i] << endl;
	}
	int x=-1,y=INF;

	for(i=0;i<n-1;i++){
		if(a[i+1]>=b[i]){
			cout << -1;
			return 0;
		}
		x=max(x,a[i+1]);
		y=min(y,b[i+1]);
	}

	if(x>y){
		cout << -1;
		return 0;
	}

	if(x<=x0 && x0<=y){
		cout << 0;
		return 0;
	}
	cout << min(abs(x-x0),abs(y-x0));
	return 0;
}


/* Goal to be an expert*/
#include<bits/stdc++.h>
#include<tuple>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
#define min3(a,b,c) min(a,min(b,c))
#define s(v) (ll)v.size()
#define e(v) v.empty()
#define bscount(x) __builtin_popcountll(x)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}

ll  isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}
bool isPowerOfTwo(ll n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
ll power1(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1LL;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

bool cmp(pll a,pll b)
{
  if(a.first>b.first) return true;
  else if(a.first<b.first) return false;
  else
  {
    if(a.second<b.second)return true;
    else return false;
  }
}

void solve()
{
  cin>>n>>m;
  ll dp[1005];
  memset(dp,0,sizeof(dp));
  ll maxm = 0;
  fo(i,n)
  {
    ll a,b;cin>>a>>b;
    maxm = max(a,maxm);
    maxm = max(b,maxm);
    dp[min(a,b)] +=1;
    dp[max(b,a)+1] -= 1;
  }
  ll ans = 0;
  int idx = -1;
  for(int i = 1;i<=maxm;i++){
    dp[i]+=dp[i-1];
    if(dp[i]>ans)
    {
      idx = i;
      ans = dp[i];
    }
    if(dp[i]==ans)
    {
      if(abs(idx-m)>abs(m-i))
        idx = i;

    }
  }

  // for(int i = 0;i<=maxm;i++)
  //    cout<<dp[i]<<" ";
  //  cout<<endl;

  if(ans==n)
  {
    cout<<abs(idx-m)<<endl;
    return;
  }
  else
  {
    cout<<"-1"<<endl;
  }

}
 int main()
 {
  #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("output.txt", "w", stdout);
      #endif
     int T;
     T=1;
        // cin>>T;

     while(T--)
     {
         solve();
     }
     cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
 }


/* Goal to be an expert*/
#include<bits/stdc++.h>
#include<tuple>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
#define min3(a,b,c) min(a,min(b,c))
#define s(v) (ll)v.size()
#define e(v) v.empty()
#define bscount(x) __builtin_popcountll(x)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}

ll  isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}
bool isPowerOfTwo(ll n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
ll power1(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1LL;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

bool cmp(pll a,pll b)
{
  if(a.first>b.first) return true;
  else if(a.first<b.first) return false;
  else
  {
    if(a.second<b.second)return true;
    else return false;
  }
}

void solve()
{
  cin>>n>>m;
  ll dp[1005];
  memset(dp,0,sizeof(dp));
  ll maxm = 0;
  fo(i,n)
  {
    ll a,b;cin>>a>>b;
    maxm = max(a,maxm);
    maxm = max(b,maxm);
    dp[min(a,b)] =1;
    dp[max(b,a)+1] = -1;
  }
  ll ans = 0;
  int idx = -1;
  for(int i = 1;i<=maxm;i++){
    dp[i]+=dp[i-1];
    if(dp[i]>ans)
    {
      idx = i;
      ans = dp[i];
    }
  }

  if(ans==n)
  {
    cout<<abs(idx-m)<<endl;
    return;
  }
  else
  {
    cout<<"-1"<<endl;
  }

}
 int main()
 {
  #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("output.txt", "w", stdout);
      #endif
     int T;
     T=1;
        // cin>>T;

     while(T--)
     {
         solve();
     }
     cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
 }


/* Goal to be an expert*/
#include<bits/stdc++.h>
#include<tuple>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
#define min3(a,b,c) min(a,min(b,c))
#define s(v) (ll)v.size()
#define e(v) v.empty()
#define bscount(x) __builtin_popcountll(x)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}

ll  isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}
bool isPowerOfTwo(ll n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
ll power1(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1LL;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

bool cmp(pll a,pll b)
{
  if(a.first>b.first) return true;
  else if(a.first<b.first) return false;
  else
  {
    if(a.second<b.second)return true;
    else return false;
  }
}

void solve()
{
  cin>>n>>m;
  ll dp[1005];
  memset(dp,0,sizeof(dp));
  ll maxm = 0;
  fo(i,n)
  {
    ll a,b;cin>>a>>b;
    maxm = max(a,maxm);
    maxm = max(b,maxm);
    dp[min(a,b)] =1;
    dp[max(b,a)+1] = -1;
  }
  ll ans = 0;
  int idx = -1;
  // for(int i = 1;i<=maxm;i++){
  //   dp[i]+=dp[i-1];
  //   if(dp[i]>ans)
  //   {
  //     idx = i;
  //     ans = dp[i];
  //   }
  // }

  if(ans==n)
  {
    cout<<abs(idx-m)<<endl;
    return;
  }
  else
  {
    cout<<"-1"<<endl;
  }

}
 int main()
 {
  #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("output.txt", "w", stdout);
      #endif
     int T;
     T=1;
        // cin>>T;

     while(T--)
     {
         solve();
     }
     cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
 }


/* Goal to be an expert*/
#include<bits/stdc++.h>
#include<tuple>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
#define min3(a,b,c) min(a,min(b,c))
#define s(v) (ll)v.size()
#define e(v) v.empty()
#define bscount(x) __builtin_popcountll(x)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}

ll  isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}
bool isPowerOfTwo(ll n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
ll power1(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1LL;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

bool cmp(pll a,pll b)
{
  if(a.first>b.first) return true;
  else if(a.first<b.first) return false;
  else
  {
    if(a.second<b.second)return true;
    else return false;
  }
}

void solve()
{
  cin>>n>>m;
  ll dp[1005];
  memset(dp,0,sizeof(dp));
  ll maxm = 0;
  fo(i,n)
  {
    ll a,b;cin>>a>>b;
    maxm = max(a,maxm);
    maxm = max(b,maxm);
    dp[min(a,b)] =1;
    dp[max(b,a)+1] = -1;
  }
  ll ans = 0;
  int idx = -1;
  for(int i = 1;i<=maxm;i++){
    dp[i]+=dp[i-1];
    if(dp[i]>ans)
    {
      idx = i;
      ans = dp[i];
    }
  }

 cout<<idx-m;

}
 int main()
 {
  #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("output.txt", "w", stdout);
      #endif
     int T;
     T=1;
        // cin>>T;

     while(T--)
     {
         solve();
     }
     cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
 }


/* Goal to be an expert*/
#include<bits/stdc++.h>
#include<tuple>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
#define min3(a,b,c) min(a,min(b,c))
#define s(v) (ll)v.size()
#define e(v) v.empty()
#define bscount(x) __builtin_popcountll(x)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}

ll  isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}
bool isPowerOfTwo(ll n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
ll power1(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1LL;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

bool cmp(pll a,pll b)
{
  if(a.first>b.first) return true;
  else if(a.first<b.first) return false;
  else
  {
    if(a.second<b.second)return true;
    else return false;
  }
}

void solve()
{
  cin>>n>>m;
  ll dp[1005];
  memset(dp,0,sizeof(dp));
  ll maxm = 0;
  fo(i,n)
  {
    ll a,b;cin>>a>>b;
    maxm = max(a,maxm);
    maxm = max(b,maxm);
    dp[min(a,b)] +=1;
    dp[max(b,a)+1] -= 1;
  }
  ll ans = 0;
  int idx = -1;
  for(int i = 1;i<=maxm;i++){
    dp[i]+=dp[i-1];
    if(dp[i]>ans)
    {
      idx = i;
      ans = dp[i];
    }
  }

  // for(int i = 0;i<=maxm;i++)
  //    cout<<dp[i]<<" ";
  //  cout<<endl;

  if(ans==n)
  {
    cout<<abs(idx-m)<<endl;
    return;
  }
  else
  {
    cout<<"-1"<<endl;
  }

}
 int main()
 {
  #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("output.txt", "w", stdout);
      #endif
     int T;
     T=1;
        // cin>>T;

     while(T--)
     {
         solve();
     }
     cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
 }


/* Goal to be an expert*/
#include<bits/stdc++.h>
#include<tuple>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
#define min3(a,b,c) min(a,min(b,c))
#define s(v) (ll)v.size()
#define e(v) v.empty()
#define bscount(x) __builtin_popcountll(x)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}

ll  isPrime(ll n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}
bool isPowerOfTwo(ll n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
ll power1(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1LL;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

bool cmp(pll a,pll b)
{
  if(a.first>b.first) return true;
  else if(a.first<b.first) return false;
  else
  {
    if(a.second<b.second)return true;
    else return false;
  }
}

void solve()
{
  cin>>n>>m;
  ll dp[1005];
  memset(dp,0,sizeof(dp));
  ll maxm = 0;
  fo(i,n)
  {
    ll a,b;cin>>a>>b;
    maxm = max(a,maxm);
    maxm = max(b,maxm);
    dp[min(a,b)] =1;
    dp[max(b,a)+1] = -1;
  }
  ll ans = 0;
  int idx = -1;
  for(int i = 1;i<=maxm;i++){
    dp[i]+=dp[i-1];
    if(dp[i]>ans)
    {
      idx = i;
      ans = dp[i];
    }
  }

 cout<<abs(m-idx);

}
 int main()
 {
  #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("output.txt", "w", stdout);
      #endif
     int T;
     T=1;
        // cin>>T;

     while(T--)
     {
         solve();
     }
     cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
 }


#include <iostream>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <vector>
#include <string>
#include <array>
#include <cmath>
#define endl "\n"

using namespace std;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, x,min1=10000,max1=-1,min2,max2;
	cin >> n >> x;
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		max2 = max(a, b);
		min2 = min(a, b);
		min1 = min(max2, min1);
		max1 = max(max1, min2);
	}
	if (max1 <= x && min1 >= x) {
		cout << 0 << endl;
	}
	else if (max1 > x && min1 >= max1) {
		cout << max1 - x << endl;
	}
	else if (min1 < x && min1 >= max1) {
		cout << x - min1 << endl;
	}
	else if (min1 < max1) {
		cout << -1 << endl;
	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define F(n) for(int i = 0; i < n; i++);
#define FI(n) for(int i = 0; i <= n; i++);
#define FS(a, n) for(int i = a; i < n; i++);
#define vi vector<int>;
#define vl vector<long int>;
#define vll vector<long long>;

int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base:: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll n = 0, x = 0;
    cin >> n >> x;
    vector<ll> f(n, 0);
    vector<ll> l(n, 0);
    ll min_f = INT64_MIN;
    ll max_l = INT64_MAX;

    int min_found = 0;
    int max_found = 0;
    for(int i = 0; i < n; i++) {
        cin >> f[i] >> l[i];
        if(min(f[i], l[i]) > x) {
            min_f = max(min(f[i], l[i]), min_f);
            min_found = 1;
        }
        if(max(f[i], l[i]) < x) {
            max_l = min(max(f[i], l[i]), max_l);
            max_found = 1;
        }
    }

    if(min_found && max_found) {
        cout << abs(min_f - max_l) << endl;
        return 0;
    }
    else if(min_found) {
        cout << abs(x - min_f) << endl;
        return 0;
    }
    else {
        cout << abs(x - max_l) << endl;;
        return 0;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define F(n) for(int i = 0; i < n; i++);
#define FI(n) for(int i = 0; i <= n; i++);
#define FS(a, n) for(int i = a; i < n; i++);
#define vi vector<int>;
#define vl vector<long int>;
#define vll vector<long long>;

int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base:: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll n = 0, x = 0;
    cin >> n >> x;
    vector<ll> f(n, 0);
    vector<ll> l(n, 0);
    ll min_f = INT64_MAX;
    ll max_l = INT64_MIN;
    for(int i = 0; i < n; i++) {
        cin >> f[i] >> l[i];
        min_f = max(min_f, f[i]);
        max_l = min(max_l, l[i]);
    }

    cout << abs(max_l - min_f) << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define F(n) for(int i = 0; i < n; i++);
#define FI(n) for(int i = 0; i <= n; i++);
#define FS(a, n) for(int i = a; i < n; i++);
#define vi vector<int>;
#define vl vector<long int>;
#define vll vector<long long>;

int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base:: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll n = 0, x = 0;
    cin >> n >> x;
    vector<ll> f(n, 0);
    vector<ll> l(n, 0);
    ll min_f = INT64_MIN;
    ll max_l = INT64_MAX;

    int min_found = 0;
    int max_found = 0;
    for(int i = 0; i < n; i++) {
        cin >> f[i] >> l[i];
        min_f = max(min(f[i], l[i]), min_f);
        max_l = min(max(f[i], l[i]), max_l);
    }
    ll ans = 0;
    if(max_l < min_f) ans = -1;
    else {
        if(min_f >= x >= max_l) {
            ans = 0;
        }
        else {
            if(x < min_f) {
                ans = min_f - x;
            }
            else {
                ans = x - max_l;
            }
        }
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define F(n) for(int i = 0; i < n; i++);
#define FI(n) for(int i = 0; i <= n; i++);
#define FS(a, n) for(int i = a; i < n; i++);
#define vi vector<int>;
#define vl vector<long int>;
#define vll vector<long long>;

int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base:: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll n = 0, x = 0;
    cin >> n >> x;
    vector<ll> f(n, 0);
    vector<ll> l(n, 0);
    ll min_f = INT64_MIN;
    ll max_l = INT64_MAX;

    int min_found = 0;
    int max_found = 0;
    for(int i = 0; i < n; i++) {
        cin >> f[i] >> l[i];
        min_f = max(min(f[i], l[i]), min_f);
        max_l = min(max(f[i], l[i]), max_l);
    }
    ll ans = 0;
    if(max_l < min_f) ans = -1;
    else {
        if(min_f >= x >= max_l) {
            ans = 0;
        }
        else {
            if(x > min_f) {
                ans = abs(x - min_f);
            }
            else {
                ans = abs(max_l - ans);
            }
        }
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define F(n) for(int i = 0; i < n; i++);
#define FI(n) for(int i = 0; i <= n; i++);
#define FS(a, n) for(int i = a; i < n; i++);
#define vi vector<int>;
#define vl vector<long int>;
#define vll vector<long long>;

int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base:: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll n = 0, k = 0;
    cin >> n >> k;
    ll x = 0, y = 0;
    ll c = 0, d = 0;
    for(int i=1;i<=n;i++){
        cin>>x>>y;
        if(x>y) swap(x,y);
        c=max(c,x);
        d=min(d,y);
    }
    if(c>d) cout<<-1;
    else{
        if(c<=k&&d>=k) cout<<0;
        else{
            if(c>k) cout<<c-k;
            else cout<<k-d;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define F(n) for(int i = 0; i < n; i++);
#define FI(n) for(int i = 0; i <= n; i++);
#define FS(a, n) for(int i = a; i < n; i++);
#define vi vector<int>;
#define vl vector<long int>;
#define vll vector<long long>;

int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base:: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll n = 0, x = 0;
    cin >> n >> x;
    vector<ll> f(n, 0);
    vector<ll> l(n, 0);
    ll min_f = INT64_MIN;
    ll max_l = INT64_MAX;

    int min_found = 0;
    int max_found = 0;
    for(int i = 0; i < n; i++) {
        cin >> f[i] >> l[i];
        if(min(f[i], l[i]) > x) {
            min_f = max(min(f[i], l[i]), min_f);
            min_found = 1;
        }
        if(max(f[i], l[i]) < x) {
            max_l = min(max(f[i], l[i]), max_l);
            max_found = 1;
        }
    }

    if(min_found && max_found) {
        cout << abs(min_f - max_l) << endl;
        return 0;
    }
    else if(min_found) {
        cout << abs(x - min_f) << endl;
        return 0;
    }
    else if (max_found) {
        cout << abs(x - max_l) << endl;;
        return 0;
    }
    else {
        cout << "0" << endl;
        return 0;
    }

    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
#define pi 3.1415926535
using namespace std;

void solve()
{
    ll n,x0,a,b,c=0;
    cin>>n>>x0;
    while(n--)
    {
        cin>>a>>b;
        ll k1=min(a,b);
        ll k2=max(a,b);
        if(x0>=k1&&x0<=k2)
            continue;
        else
        {
            if(abs(k1-x0)>abs(k2-x0))
            {
                c=c+k2-x0;
                x0=k2;
            }
            else
            {
                c=c+k1-x0;
                x0=k1;
            }

        }
        cout<<c<<'\n';

    }

}
int main()
{
    solve();
}


#include <bits/stdc++.h>
typedef long long ll;
#define pi 3.1415926535
using namespace std;

void solve()
{
    ll n,x0,a,b,c=0;
    cin>>n>>x0;
    while(n--)
    {
        cin>>a>>b;
        ll k1=min(a,b);
        ll k2=max(a,b);
        if(x0>=k1&&x0<=k2)
            continue;
        else
        {
            if(abs(k1-x0)>abs(k2-x0))
            {
                c=c+k2-x0;
                x0=k2;
            }
            else
            {
                c=c+k1-x0;
                x0=k1;
            }

        }
     }
     cout<<c<<'\n';

}
int main()
{
    solve();
}


#include <bits/stdc++.h>
typedef long long ll;
#define pi 3.1415926535
using namespace std;

void solve()
{
    ll n,x0,a,b,c=0;
    cin>>n>>x0;
    while(n--)
    {
        cin>>a>>b;
        ll k1=min(a,b);
        ll k2=max(a,b);
        if(x0>=k1&&x0<=k2)
            continue;
        else
        {
            if(abs(k1-x0)>abs(k2-x0))
            {
                c=c+k2-x0;
                x0=k2;
            }
            else
            {
                c=c+k1-x0;
                x0=k1;
            }

        }
        if(c==0)
            cout<<-1;
        else
            cout<<c<<'\n';

    }

}
int main()
{
    solve();
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int ans,MAX,MIN;

struct node{
	int a;
	int b;
};node leng[1001];

int cmp(node x,node y){
	return x.a<y.a;
}

int main(){
	int n,x0;
	cin>>n>>x0;
	for (int i=1;i<=n;i++){
		scanf("%d%d",&leng[i].a,&leng[i].b);
		if (leng[i].a>leng[i].b){
			int t=leng[i].a;
			leng[i].a=leng[i].b;
			leng[i].b=t;
		}
		if(leng[i].a>MAX){
			MAX=leng[i].a;
		}
		if(leng[i].b<MIN){
			MIN=leng[i].b;
		}
	}
	ans=max(MAX-x0,MIN-x0);
	cout<<ans;
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 1000+20;
int n,x;
int l,r;
int a[N];
int ans=N;
int main()
{
	scanf("%d%d",&n,&x);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&l,&r);
		if(l>r)
		{
			int temp=l;l=r;r=temp;
		}
		for(int j=l;j<=r;j++) a[j]++;
	}
	for(int i=0;i<=N;i++)
	{
		if(a[i]==n) ans=min(ans,abs(x-i));
	}
	f(ans==N) printf("-1");
	else printf("%d",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
	int n,x,a,b,min=-1,max=9999,t;
	cin>>n>>x;
	while(n--)
	{
		cin>>a>>b;
		if(a>b)
		{
			t=a;
			a=b;
			b=t;
		}
		if(min<a)min=a;
		if(max>b)max=b;
	}
	if(max<min) cout<<"-1"<<endl;
	else if(x<min) cout<<min-x<<endl;
	else if(x>max) cout<<x-max<<endl;
	else cout<<"0"<<endl;
	return 0;
}


#include<stdio.h>
int main()
{
    int n,x;
    int al,ar;
    int ansl=0,ansr=1000;
    int flag=0;
    int t;
    scanf("%d%d",&n,&x);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&al,&ar);
        if(al>ar) t=al,al=ar,ar=t;
        if(al>=ansl) ansl=al;
        if(ar<=ansr) ansr=ar;
        if(ansl>ansr) {
            flag=1;
            break;
        }
    }
    if(flag==1) printf("-1");
    else
    {
        if(x<ansl) printf("%d",ansl-x);
        else if(x>ansr) printf("%d",x-ansr);
        else printf("0");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
int a[1010][2];
int main()
{
	int i,j,k,x;
	cin>>n>>x;
	for(i=1;i<=n;i++)
	{
		cin>>j>>k;
		a[i][0]=min(j,k);
		a[i][1]=max(j,k);
	}
	int ans=2000;bool flag;
	for(i=0;i<=1000;i++)
	{
		flag=0;
		for(j=1;j<=n;j++)
		{
			if(a[j][0]>i||a[j][1]<i)
			{
				flag=1;
				break;
			}
		}
		if(flag==0)
		{
			ans=min(ans,abs(i-x));
			//cout<<"\n"<<i<<" "<<ans<<"\n";
		}
	}
	if(ans==2000)cout<<"-1\n";
	else cout<<ans<<"\n";
	return 0;
}


#include<stdio.h>
int main()
{
    int n,x;
    int al,ar;
    int ansl=0,ansr=1000;
    int flag=0;
    int t;
    scanf("%d%d",&n,&x);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&al,&ar);
        if(al>ar) t=al,al=ar,ar=t;
        if(al>ansl) ansl=al;
        if(ar<ansr) ansr=ar;
        if(ansl>ansr) {
            flag=1;
            break;
        }
    }
    if(flag==1) printf("-1\\n");
    else
    {
        if(x<ansl) printf("%d\n",ansl-x);
        else if(x>ansr) printf("%d\n",ansr-x);
        else printf("0\n");
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int n,x;
    int al,ar;
    int ansl=0,ansr=1000;
    int flag=0;
    int t;
    scanf("%d%d",&n,&x);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&al,&ar);
        if(al>ar) t=al,al=ar,ar=t;
        if(al>=ansl) ansl=al;
        if(ar<=ansr) ansr=ar;
        if(ansl>ansr) {
            flag=1;
            break;
        }
    }
    if(flag==1) printf("-1\\n");
    else
    {
        if(x<ansl) printf("%d",ansl-x);
        else if(x>ansr) printf("%d",ansr-x);
        else printf("0");
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

const int maxn = 1001;

int a[maxn];

void keep(int &l,int &r) {
    return;
    //does nothing
}

int main() {
    int n,x;
    cin>>n>>x;
    int l,r;
    for(int i=1;i<=n;i++) {
        cin>>l>>r;
        l>r?swap(l,r):keep(l,r);
        a[l]++;
        a[r+1]--;
    }
    int tmp = 0;
    int ans = 2147483646;
    for(int i=0;i<=1000;i++) {
        tmp+=a[i];
        if(tmp==n) {
            ans=min(ans,abs(i-x));
        }
    }
    if(ans == 2147483646) {
        cout<<-1<<endl;
    }
    else {
        cout<<ans<<endl;
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cmath>

int main() {
    int n, x;
    scanf("%d %d", &n, &x);
    int minp = 0x2333333, maxp = -1;
    while(n--) {
        int a, b;
        scanf("%d %d", &a, &b);
        if(a > b) std::swap(a, b);
        maxp = std::max(maxp, a);
        minp = std::min(minp, b);
    }
    int ans = -1;
    for(int i = maxp; i <= minp; i++) {
        if(ans >= 0) {
            ans = std::min(ans, std::abs(i-x));
        }else ans = std::abs(i-x);
    }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int ans=10000,MAX,MIN=100000;

struct node{
	int a;
	int b;
};node leng[1001];

int number[10001];

int main(){
	int n,x0;
	cin>>n>>x0;
	for (int i=1;i<=n;i++){
		scanf("%d%d",&leng[i].a,&leng[i].b);
		if (leng[i].a>leng[i].b){
			int t=leng[i].a;
			leng[i].a=leng[i].b;
			leng[i].b=t;
		}
		for (int j=leng[i].a;j<leng[i].b;j++){
			number[j]++;
		}
	}
	for (int i=1;i<1000;i++){
		if (number[i]==n){
			int len=abs(i-x0);
			if (len<ans) ans=len;
		}
	}
	if (ans==10000) ans=-1;
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxn = 1006;

int a[maxn];

int main()

{

    int n,x;

    scanf("%d%d",&n,&x);

    for(int i=1;i<=n;i++)

    {

        int l,r;

        scanf("%d%d",&l,&r);

        if(l>r)swap(l,r);

        a[l]++;

        a[r+1]--;

    }

    int tmp = 0;

    int ans = 1e9;

    for(int i=0;i<=1000;i++)

    {

        tmp+=a[i];

        if(tmp==n)ans=min(ans,abs(i-x));

    }

    if(ans==1e9)cout<<"-1"<<endl;

    else cout<<ans<<endl;

}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
const int INF=0x3f3f3f3f;
const int maxn=1e5+5;
const int mod=1e9+7;
const double eps=1e-7;
typedef long long ll;
#define vi vector<int>
#define si set<int>
#define pii pair<ll,ll>
#define pi acos(-1.0)
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
#define sci(x) scanf("%d",&(x))
#define scll(x) scanf("%lld",&(x))
#define sclf(x) scanf("%lf",&(x))
#define pri(x) printf("%d",(x))
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define per(i,j,k) for(int i=j;i>=k;--i)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
ll ans,n,s,a[maxn],b[maxn],num1,num2;
priority_queue<pii,vector<pii>,greater<pii> >to1,to2;//to1表示2->1,to2表示1->2
void init()
{
        ans=0;num1=0;num2=0;
		while(to1.size())to1.pop();
		while(to2.size())to2.pop();
		mem(a,0),mem(b,0);
}
int main()
{
	while(~scanf("%lld%lld",&n,&s))
	{
		init();
		rep(i,0,n-1)
		{
			ll s,a,b;
			scanf("%lld%lld%lld",&s,&a,&b);
			if(a>b)
			{
				num1+=s;
				ans+=s*a;
				to2.push(pii(a-b,s));//表明向2转化的cost,先转化浪费少的
			}
			else
			{
				num2+=s;
				ans+=s*b;
				to1.push(pii(b-a,s));
			}
		}
			num1%=s,num2%=s;//正好的就用s装,剩下的部分最多两个s就能装得下
			if(num1+num2>s)//需要两个 不需要转化
			{
				printf("%lld\n",ans);
				continue;
			}
			else
			{
				ll tmp1=num1,tmp2=num2,cost1=0,cost2=0;
				while(tmp1)//将1全转化为2
				{
					pii tmp=to2.top();
					to2.pop();
					ll num=min(tmp1,tmp.second);
					tmp1-=num;
					cost1+=num*tmp.first;
				}
				while(tmp2)
				{
					pii tmp=to1.top();
					to1.pop();
					int num=min(tmp2,tmp.second);
					tmp2-=num;
					cost2+=num*tmp.first;
				}
				printf("%lld\n",max(ans-cost1,ans-cost2));
			}
		}
	return 0;
}