#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}

int main(){
    ll n;
    ll L;
    cin >> n >> L;
    vector<ll>x(n),t(n);
    rep(i,n)cin >> x[i];
    rep(i,n)cin >> t[i];
    ll res = 0;
    vector<int> s;
    int p = -1;
    rep(i,n){
        ll a = x[i];
        ll aa = a + t[i];
        ll ma = aa % (2*L);
        bool fa = 0;
        if(ma<=x[i]){
            aa = (aa/(2*L))*2*L + x[i];
        }else if(ma<=2*L-x[i]){
            fa = 1;
            aa = (aa/(2*L))*2*L + 2*L-x[i];    
        }else{
            aa = (aa/(2*L) + 1)*2*L + x[i];
        }
        ll b = 2*L-x[i];
        ll bb = b + t[i];
        ll mb = bb % (2*L);
        bool fb = 0;
        if(mb<=x[i]){
            bb = (bb/(2*L))*2*L + x[i];
        }else if(mb<=2*L-x[i]){
            fb = 1;
            bb = (bb/(2*L))*2*L + 2*L-x[i];    
        }else{
            bb = (bb/(2*L) + 1)*2*L + x[i];
        }
        if((!fa)&&fb){
            res += aa - a;
            p = s.size();
        }else if((fa)&&(!fb)){
            res += (aa-a) -(2*L-2*x[i]);
            s.push_back(0);
        }else if(fa){
            res += (aa-a) -(2*L-2*x[i]);
            s.push_back(1);
        }else{
            res += (bb-b) -2*x[i];
            s.push_back(2);
        }
    }
    // cerr << res << endl;
    // for(auto x:s){
    //     cerr << x << " ";
    // }
    // cerr << endl;
    int m = s.size();
    if(m==0){
        cout << res + 2*L << endl;
        return 0;
    }
    ll ddd = m;
    rep(i,m){
        if(s[i]==2){
            ddd = i;
            break;
        }
    }
    ll c = 0;
    ll xx = 0;
    ll yy = 0;
    int ym = -1;
    int pp = -1;
    rep(i,ddd){
        if(s[i]==0){
            xx++;
        }else{
            if(xx>0){
                c++;
                xx--;
            }else{
                yy++;
            }
        }
    }
    ll xxx = 0;
    ll zz = 0;
    bool flag = 0;
    int qqq = -1;
    for(int i=m-1;i>=ddd;i--){
        flag = 1;
        if(s[i]==0){
            xxx++;
            if(qqq==-1)qqq = i;
        }else{
            if(xxx>0){
                c++;
                xxx--;
                if(xxx==0)qqq = -1;
            }else{
                zz++;
            }
        }
    }
    cerr << res << endl;
    cerr << c << " " << xx << " " << xxx << " " << yy << " " << zz << " " << flag  << endl;
    if(flag){
        c += (xx+xxx)/2;
        if(zz==0){
            c += yy + zz + (xx+xxx)%2;
            if(qqq <=p)c++;
        }else{
            c += (xx+xxx)%2;
            c += yy + zz + 1;
        }
        cout << res + c*2*L << endl;
    }else{
        if(xx!=0){
            c += (xx)/2;
            if(xx%2==1){
                c++;
                c += yy;
                if(ddd<=p)c++;
            }else{
                c += yy;
                c++;
            }
        }else{
            if(yy!=0){
                c += yy;
            }else{
                c++;
            }
        }
        cerr << c << endl;
        cout << res + c*2*L << endl;
    }
    return 0;
}

Measured memory_bytes: 7156