#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)

vector<vector<pair<int,ll> > > g; 



int main(){
    int n;
    ll L;
    cin >> n >> L;
    set<ll> st,st2;
    rep(i,n){
        ll a;
        cin >> a;
        st.insert(a);
    }
    rep(i,n){
        ll a;
        cin >> a;
        st2.insert(a);
    }
    ll ans = 0;
    ll now = 0;
    bool flag = 0;
    while(st2.size()>0){
        if(!flag){
            auto p = st.lower_bound(now);
            ll next,next2;
            if(p==st.end()){
                next = *st.begin();
            }else{
                next = *p;
            }
            auto q = st2.lower_bound(next);
            ll goal;
            if(q==st2.end()){
                next2 = *st2.begin();
                auto pp = st.lower_bound(next2);
                if(pp==st.begin()){
                    pp = st.end();
                    pp--;
                    goal = *pp;
                }else{
                    pp--;
                    goal = *pp;
                }
            }else{
                next2 = *q;
                auto pp = st.lower_bound(next2);
                pp--;
                goal = *pp;
            }
            
            
            if(now<goal){
                ans += goal-now;
            }else{
                ans += goal+L-now;
            }
            st.erase(goal);
            now = goal;
            flag = 1;
        }else{
            auto p = st2.lower_bound(now);
            ll next,next2;
            if(p==st2.end()){
                next = *st2.begin();
            }else{
                next = *p;
            }
            auto q = st.lower_bound(next);
            ll goal;
            if(q==st.end()){
                next2 = *st.begin();
                auto pp = st2.lower_bound(next2);
                if(pp==st2.begin()){
                    pp = st2.end();
                    pp--;
                    goal = *pp;
                }else{
                    pp--;
                    goal = *pp;
                }
            }else{
                next2 = *q;
                auto pp = st2.lower_bound(next2);
                pp--;
                goal = *pp;
            }
            if(now<goal){
                ans += goal-now;
            }else{
                ans += goal+L-now;
            }
            st2.erase(goal);
            now = goal;
            flag = 0;
        }
        // for(auto x:st){
        //     cerr << x << " ";
        // }
        // cerr << endl;
        // for(auto x:st2){
        //     cerr << x << " ";
        // }
        // cerr << endl;
    }
    cout << ans << endl;
    return 0;
}