#include <bits/stdc++.h>
 
//    #include <boost/multiprecision/cpp_int.hpp>
// #define int long long
 #define inf  1000000007
 #define pa pair<int,int>
 #define ll long long
 #define pal pair<double,double>
 #define ppap pair<pa,int>
  #define PI 3.14159265358979323846
  #define paa pair<int,char>
  #define  mp make_pair
  #define  pb push_back
  #define EPS (1e-8)
                                          
    int dx[8]={0,1,0,-1,1,1,-1,-1};
    int dy[8]={1,0,-1,0,-1,1,1,-1};
                                            using namespace std;
                                   			class pa3{
                                            	public:
                                            	int x;
                                   				int y,z;
                                            	pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}
                                            	bool operator < (const pa3 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		 return z<p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                   				bool operator > (const pa3 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		 return z>p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa3 &p) const{
                                            		return x==p.x && y==p.y && z==p.z;
                                            	}
                                            		bool operator != (const pa3 &p) const{
                                            			return !( x==p.x && y==p.y && z==p.z);
                                            	}
                                            
                                            };
                                            
                                            class pa4{
                                            	public:
                                            	int x;
                                            	int y,z,w;
                                            	pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}
                                            	bool operator < (const pa4 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		if(z!=p.z)return z<p.z;
                                            		return w<p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator > (const pa4 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		if(z!=p.z)return z>p.z;
                                            		return w>p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa4 &p) const{
                                            		return x==p.x && y==p.y && z==p.z &&w==p.w;
                                            	}
                                            		
                                            
                                            };
                                            class pa2{
                                            	public:
                                            	int x,y;
                                            	pa2(int x=0,int y=0):x(x),y(y) {}
                                            	pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
                                            	pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}
                                            	bool operator < (const pa2 &p) const{
                                            		return y != p.y ? y<p.y: x<p.x;
                                            	}
                                            	bool operator > (const pa2 &p) const{
                                            		return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa2 &p) const{
                                            		return abs(x-p.x)==0 && abs(y-p.y)==0;
                                            	}
                                            	bool operator != (const pa2 &p) const{
                                            		return !(abs(x-p.x)==0 && abs(y-p.y)==0);
                                            	}
                                            		
                                            
                                            };
                                            
 
                      
                                string itos( int i ) {
                                ostringstream s ;
                                s << i ;
                                return s.str() ;
                                }
                                 
                                int gcd(int v,int b){
                                	if(v==0) return b;
                                	if(b==0) return v;
                                	if(v>b) return gcd(b,v);
                                	if(v==b) return b;
                                	if(b%v==0) return v;
                                	return gcd(v,b%v);
                                }
                 
                            
                                int mod;
int extgcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = extgcd(b, a%b, y, x);
    y -= a/b * x;
    return d;
}
pa operator+(const pa & l,const pa & r) {   
    return {l.first+r.first,l.second+r.second};                                    
}    
pa operator-(const pa & l,const pa & r) {   
    return {l.first-r.first,l.second-r.second};                                    
}  
                int beki(int wa,int rr,int warukazu){
                	if(rr==0) return 1%warukazu;
                	if(rr==1) return wa%warukazu;
                	wa%=warukazu;
                	if(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu;
                	ll zx=beki(wa,rr/2,warukazu);
                	return (zx*zx)%warukazu;
                }
 
              /*                  
                int pr[700100];
                int inv[700010];
                

                
    			int comb(int nn,int rr){
    				if(rr<0 || rr>nn || nn<0) return 0;
    				int r=pr[nn]*inv[rr];
    				r%=mod;
    				r*=inv[nn-rr];
    				r%=mod;
    				return r;
    			}
               
                void gya(int ert){
                	pr[0]=1;
                	for(int i=1;i<=ert;i++){
                		pr[i]=((ll)pr[i-1]*i)%mod;
                	}
                		inv[ert]=beki(pr[ert],mod-2,mod);
                	for(int i=ert-1;i>=0;i--){
                		inv[i]=(ll)inv[i+1]*(i+1)%mod;
                	}
                }
                */
              //   cin.tie(0);
    		//	ios::sync_with_stdio(false);
    			//priority_queue<pa3,vector<pa3>,greater<pa3>> pq;            
                 //sort(ve.begin(),ve.end(),greater<int>());
 //   mt19937(clock_per_sec);
  //  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ;

int g=0;
int g2=0;
struct seg_rangeadd_getmin{
    //       1
    //   2        3
    // 4   5   6    7
     
     
    private:
    public:
     
    // (1<<15)=32768
    // 1<<16 = 65536
    // 1<<17 = 131072
    // 1<<18 = 262144
     
    int cor=(1<<19);
    int inff=inf*2+100;
     
    vector<int> vec;
    vector<int> lazy;
    void shoki1(){
        vec.resize(2*cor+3, 0);
        lazy.resize(2*cor+3, 0);
    }
    void shoki2(){
        //lazy[1]=(1ll<<31)-1;
        //初期値
    	for(int i=cor-1;i>0;i--)vec[i]=min(vec[i*2],vec[i*2+1]);
    }
         
    void chien(int k,int l,int r){
        //if(lazy[k]==-1) return;
        if(l+1<r){
            vec[k]+=lazy[k];
            lazy[2*k]+=lazy[k];
            lazy[2*k+1]+=lazy[k];
            lazy[k]=0;
        }
        else {
            vec[k]+=lazy[k];
            lazy[k]=0;
        }
    }
     
    int rangeadd(int a,int b,int w, int k=1,int l=0,int r=-3){
        //[a,b)を wに変更
        if(r<0) r=cor;
         
        if(a<=l && r<=b){
            lazy[k]+=w;
            chien(k,l,r);
            return vec[k];
        }
        if(r<=a || b<=l){
            chien(k,l,r);
            return vec[k];
        }
         
        chien(k,l,r);
//  cout<<k<<" "<<l<<" "<<r<<endl;
        int w1=rangeadd(a,b,w,k*2,l,(l+r)/2);
        int w2=rangeadd(a,b,w,k*2+1,(l+r)/2,r);
        vec[k]=min(w1,w2);
        return vec[k];
    }
     
    // [a,b)
    // k-th node
    // k no kukanha [l,r)
     
    int getmin(int a,int b,int k=1,int l=0,int r=-1){
        //[a,b)のminを取得
        if(r<0) r=cor;
        chien(k,l,r);
        if(r<=a || b<=l){// 区間外
            return inff;
        }
         
        if(a<=l && r<=b){// 完全含む
             
            return vec[k];
        }
        int w1=getmin(a,b,k*2,l,(l+r)/2);
        int w2=getmin(a,b,k*2+1,(l+r)/2,r);
        return min(w1,w2);
    }
    void pre(){
        for(int i=1;i<2*cor;i++){
            cout<<vec[i]<<" ";
            if(((i+1)&(-(i+1)))==i+1) cout<<endl;
        }
    }
     
};
int x[300030],y[300030],a1[300030],b1[300030];
vector<int> a,b,c;

int aa,bb,m;
bool ch(int r){
	if(r==0)return 0;

	
	if(r*2<aa+bb+m)return 0;
	if(r<aa)return 0;
	if(r<bb)return 0;
	if(r>=aa+bb+m)return 1;
		
	int d=aa+bb+m-r;
	
//	cout<<d<<endl;
	
	

//	cout<<r<<endl;
	if(aa<d)x[0]=-1;
	else x[0]=0;

	
	int pos=max(0,aa-d);
	for(int i=0;i<m;i++){
		if(aa-pos+1+i<d)x[i+1]=-1;
		else if(aa-pos+1+i==d)x[i+1]=i+1;
		else{
			if(aa<=pos||a[pos]<c[m-i-1]){
				x[i+1]=x[i]+1;
				pos++;
			}
			else x[i+1]=x[i];
		}
		x[i+1]=min(x[i+1],d);
		
	}
	
	if(bb<d)y[m]=-1;
	else y[m]=0;
	pos=min(d-1,bb-1);
	for(int i=0;i<m;i++){
		if(pos+1+i+1<d)y[m-i-1]=-1;
		else if(pos+1+i+1==d)y[m-i-1]=i+1;
		else{
			if(pos<0||b[pos]>c[i]){
				y[m-i-1]=y[m-i]+1;
				pos--;
			}
			else y[m-i-1]=y[m-i];
		}
		y[m-i-1]=min(y[m-i-1],d);
		
	}

	vector<pa> ve;
	for(int i=0;i<=m;i++)if(x[i]>=0 &&y[i]>=0){
		if(ve.size()==0)ve.pb(mp(x[i],y[i]));
		else{
			if(ve.back().first==x[i])continue;
			if(ve.back().second==y[i])ve.back()=mp(x[i],y[i]);
			else ve.pb(mp(x[i],y[i]));
		}
	}

	int q=ve.size();
	reverse(ve.begin(),ve.end());
	if(q==0)return 0;

	
	if(1){
		for(int i=0;i<d;i++)a1[i]=-1,b1[i]=-1;
		for(int ii=0;ii<1;ii++){
			pa v=ve[ii];

			
			
			int	tt1=0,tt2=0;
			int t2=m-v.first,t1=aa-d+v.first;
			bool bo=1;
			for(int i=0;i<d;i++){
				int aq,bq;
				if(t1<aa&&t2<m&&a[t1]<c[t2] || t2==m){
					aq=a[t1];
					t1++;
				}
				else {
					aq=c[t2];
					t2++;
				}
				
				
				if(tt1<d-v.second&&tt2<v.second&&b[tt1]<c[tt2] || tt2==v.second){
					bq=b[tt1];
					tt1++;
				}
				else{
					bq=c[tt2];
					tt2++;
				}
				
				if(bq==b.back())bq=0;
				a1[i]=aq,b1[i]=bq;
				if(aq<bq){
					bo=0;
					break;
				}
			}
			if(!bo) continue;
			return 1;
	
			
			
		}
		
	}
	if(q==1)return 0;
		 seg_rangeadd_getmin ss;
	ss.shoki1();

	for(int i=1;i<q;i++){
		pa v=ve[i];
		if(i==1){
			for(int j=aa-1;j>=aa-(d-v.first);j--)ss.vec[a[j]+ss.cor]=-1;
			for(int j=m-1;j>=m-v.first;j--)ss.vec[c[j]+ss.cor]=-1;
			
			for(int j=0;j<d-v.second;j++)ss.vec[b[j]+ss.cor]=1;
			for(int j=0;j<v.second;j++)ss.vec[ss.cor+c[j]]=1;
			
			for(int j=ss.cor+1;j<2*ss.cor;j++)ss.vec[j]+=ss.vec[j-1];
			ss.shoki2();
		}
		else{
			ss.rangeadd(a[aa-(d-v.first)],ss.cor,-1);
			ss.rangeadd(c[m-v.first-1],ss.cor,2);
			ss.rangeadd(b[d-v.second],ss.cor,-1);
		}

		if(ss.getmin(0,ss.cor)>=0)return 1;
		
	}
	return 0;
	
	
	return 0;
}

signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n,l;
	cin>>n>>l;
	
	
	ll ans=0ll;
	vector<int> X(n);
	for(int i=0;i<n;i++)cin>>X[i];
	for(int i=0;i<n;i++){
		int x=X[i];
		int t;
		cin>>t;
		ans+=t/(2*l);
		t%=2*l;
		if(t==0){
			ans--;
			t+=2*l;
		}
		if(t>2*l-2*x &&t>2*l-2*(l-x) ){
			ans++;
			g=x;
			continue;
		}
		else if(t>2*l-2*x &&t<=2*l-2*(l-x) ){
			b.pb(x);
			g2=max(g2,x);
		}
		else if(t<=2*l-2*x &&t>2*l-2*(l-x)){
			a.pb(x);
			g2=max(g2,x);
		}
		else c.pb(x),g2=max(g2,x);
	}

	

	if(g>g2)a.pb(g);
	 m=c.size();
	b.pb(inf*2+100);
	
	set<int> se;
	map<int,int>ma;
	for(auto v:a)se.insert(v);
	for(auto v:b)se.insert(v);
	for(auto v:c)se.insert(v);
	
	int cnt=1;
	for(auto i:se){
		ma[i]=cnt;
		cnt++;
	}
	for(auto &v:a)v=ma[v];
	for(auto &v:b)v=ma[v];
	for(auto &v:c)v=ma[v];
	

	
	int si=0,ue=300010;
		 aa=a.size();
	 bb=b.size();	
	
	while(ue-si>1){
		int me=(ue+si)/2;
		bool bo=ch(me);

		if(bo)ue=me;
		else si=me;
	}

	ll hoge=ans+ue;
	hoge*=(ll)2*l;
	cout<<hoge<<endl;
	return 0;

return 0;
 }


Measured memory_bytes: 43756