#include<bits/stdc++.h>

using namespace std;

#define N 120000
#define LL long long

LL n,m,ans,a[N],b[N],pre[N],suf[N],q[N];

int main(){
	scanf("%lld%lld",&n,&m);
	for (LL i=1;i<=n;++i) scanf("%lld%lld",&a[i],&b[i]);
	for (LL i=1;i<=n;++i) pre[i]=pre[i-1]+b[i];
	for (LL i=n;i;--i) suf[i]=suf[i+1]+b[i];
	LL tt=1,ww=0;
	for (LL i=1;i<=n;++i){
		ans=max(ans,suf[i]-(m-a[i]));
		for (;tt<=ww&&suf[q[ww]]-(m-a[q[ww]])*2<=suf[i]-(m-a[i])*2;--ww);
		q[++ww]=i;
	}
	for (LL i=1;i<=n;++i){
		for (;tt<=ww&&q[tt]<=i;++tt);
		if (tt<=ww) ans=max(ans,suf[q[tt]]-(m-a[q[tt]])*2+pre[i]-a[i]);
	}
	tt=1,ww=0;
	for (LL i=n;i;--i){
		ans=max(ans,pre[i]-a[i]);
		for (;tt<=ww&&pre[q[ww]]-a[q[ww]]*2<=pre[i]-a[i]*2;--ww);
		q[++ww]=i;
	}
	for (LL i=n;i;--i){
		for (;tt<=ww&&q[tt]>=i;++tt);
		if (tt<=ww) ans=max(ans,pre[q[tt]]-a[q[tt]]*2+suf[i]-(m-a[i]));
	}
	printf("%lld\n",ans);
	
	return 0;
}
			