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

ll solve(vector<ll>& A) {
    int n = A.size();
    vector<ll> C(n+1);
    for ( int i = 0; i < n; i++ ) {
        C[i+1] = C[i] + A[i];
    }

    auto diff = [](ll m1, ll m2) {
        return max(m1,m2) - min(m1,m2);
    };

    int i1,i2,i3;
    ll ans = 1LL<<60;
    for ( i2 = 2; i2 <= n-2 ; i2++ ) {
        i1 = lower_bound(C.begin(), C.end(), (C[i2]+1)/2) - C.begin();
        if ( i1 >= 2 ) {
            if ( diff(C[i2]-C[i1-1],C[i1-1]-C[0]) < diff(C[i2]-C[i1],C[i1]-C[0]) ) i1--;
        }
        if ( i1 <= 0 ) i1 = 1;
        if ( i1 >= i2 ) i1 = i2-1;

        i3 = lower_bound(C.begin(), C.end(), C[i2] + ((C[n]-C[i2])+1)/2 ) - C.begin();
        if ( i3 > n ) i3 = n;
        if ( i3 >= i2+2 ) {
            if ( diff(C[n]-C[i3-1],C[i3-1]-C[i2]) < diff(C[n]-C[i3],C[i3]-C[i2]) ) i3--;
        }
        if ( i3 <= i2 ) i3 = i2+1;
        if ( i3 >= n ) i3 = n-1;

        ll x = max({C[i1],C[i2]-C[i1],C[i3]-C[i2],C[n]-C[i3]}) - min({C[i1],C[i2]-C[i1],C[i3]-C[i2],C[n]-C[i3]});
        if ( ans > x ) ans = x;
    }

    return ans;
}

int main() {
    ll N;
    cin >> N;
    vector<ll> A(N);
    for ( int i = 0; i < N; i++ ) {
        cin >> A[i];
    }    
    auto ans = solve(A);
    cout << ans << "\n";
    return 0;
}
#include<bits/stdc++.h>
#define lowbit(i) ((i)&(-(i)))
#define lson(i) ((i)<<1)
#define rson(i) ((i)<<1|1)
#define sqr(x) ((x)*(x))
#define fi first
#define se second
#define mp(x,y) make_pair((x),(y))
#define pb push_back
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef pair<int,int> pii;
typedef vector<int> vii;
typedef vector<pii> vpi;
typedef queue<int> qii;

const int inf=0x3f3f3f3f;
const int maxn=100007;

ll n,s;
struct node{
	ll x,p;void init(){scanf("%lld%lld",&x,&p);}
}a[maxn];

pair<ll,ll> solve(ll l,ll r){
	if(a[r].x<s) {
		return mp(-1,s-a[l].x);
  	}else if(a[l].x>s) {
  		return mp(1,a[r].x-s);
    }
  	if(l==r){
		if(a[l].x<s) return mp(-1,s-a[l].x);
		else return mp(1,a[l].x-s);
	}
	if(a[l].p<a[r].p){
		a[r].p+=a[l].p;
		pair<ll,ll> t=solve(l+1,r);
		// cout<<l<<' '<<r<<' '<<a[l].p<<' '<<a[r].p-a[l].p<<' ';
		// cout<<t.fi<<' '<<t.se<<'\n';
		if(t.fi==-1) return mp(-1,t.se+a[l+1].x-a[l].x);
		else return mp(-1,t.se+a[r].x-a[l].x);
	}else{
		a[l].p+=a[r].p;
		pair<ll,ll> t=solve(l,r-1);
		// cout<<l<<' '<<r<<' '<<a[l].p-a[r].p<<' '<<a[r].p<<' ';
		// cout<<t.fi<<' '<<t.se<<'\n';
		if(t.fi==1) return mp(1,t.se+a[r].x-a[r-1].x);
		else return mp(1,t.se+a[r].x-a[l].x);
	}
}

int main(){
// 	freopen("","r",stdin);5
// 	freopen("","w",stdout);
	scanf("%lld%lld",&n,&s);
	for(int i=1;i<=n;i++) a[i].init();
	// for(int i=1;i<=n;i++) cout<<a[i].p<<' ';cout<<'\n';
	printf("%lld\n",solve(1,n).se);
	return 0;
}
/*
3 2
1 3
3 4
4 2
*/