#include <bits/stdc++.h>
using namespace std;
int n,m,i,j,a[300005],t[300005],ans,l[300005],r[300005],del[300005],c11;
int main(){
	cin>>n>>m;ans=n+1;
	for(i=1;i<=n;i++){
		cin>>a[i];
	}
	for(i=1;i<=n;i++){
		cin>>t[i];
		ans+=t[i]/(m*2);
		t[i]%=m*2;
	}
	for(i=1;i<=n;i++){
		if(!t[i]){
			ans--;
			del[i]=1;
			continue;
		}
		l[i]=(2*(m-a[i])>=t[i]);
		r[i]=(2*a[i]>=t[i]);
		del[i]=(!l[i])&&(!r[i]);
	}
	if(l[n]) ans--;
	for(i=1;i<n;i++)if(!del[i]){
		if(!l[i]) break;
		if(r[i]) c11++;
		else if(c11){
			ans--;
			c11--;
		}
	}
	int tmp=c11;c11=0;j=i;
	for(i=n-1;i>=j;i--)if(!del[i]){
		if(!r[i]) break;
		if(l[i]) c11++;
		else if(c11){
			ans--;
			c11--;
		}
	}
	c11+=tmp;
	ans-=c11/2;
	cout<<2ll*m*ans<<endl; 
	return 0;
}