#include using namespace std; #define rep(i,n) REP(i,0,n) #define REP(i,s,e) for(int i=(s); i<(int)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--) #define pb push_back #define all(r) r.begin(),r.end() #define rall(r) r.rbegin(),r.rend() #define fi first #define se second typedef long long ll; typedef vector vi; typedef vector vl; typedef pair pii; typedef pair pll; const int INF = 1e9; const ll MOD = 1e9 + 7; double EPS = 1e-8; int main() { ll N, L; cin >> N >> L; vector x(N), y(N); rep(i, N) cin >> x[i]; rep(i, N) cin >> y[i]; set X, Y; rep(i, N) { X.insert(x[i]); Y.insert(y[i]); } auto getIt = [](const set& st, ll val) { auto it = st.upper_bound(val); if (it != st.end()) return it; return st.begin(); }; ll ans = 0LL; ll cur = 0LL; rep(i, N) { { auto itX = getIt(X, cur); if (cur < *itX) ans += (*itX - cur); else ans += (L - cur + *itX); cur = *itX; X.erase(itX); } // cout << " " << cur; { auto itY = getIt(Y, cur); // cout << "(first itY " << *itY << "), "; if(X.size() > 0) { auto itX = getIt(X, *itY); // cout << " (nxt itX " << *itX << "), "; { itY = getIt(Y, *itX); // cout << "(nxt itY " << *itY << "), "; if(itY == Y.begin()) { itY = Y.end(); } --itY; } } if (cur < *itY) ans += (*itY - cur); else ans += (L - cur + *itY); cur = *itY; Y.erase(itY); } // cout << " " << cur << " -> " << ans << endl; } cout << ans << endl; return 0; }