// includes #include <cstdio> #include <cstdint> #include <iostream> #include <iomanip> #include <string> #include <queue> #include <stack> #include <vector> #include <set> #include <map> #include <unordered_map> #include <algorithm> #include <utility> #include <functional> #include <cmath> #include <climits> #include <bitset> #include <list> #include <random> // macros #define ll long long int #define pb emplace_back #define mk make_pair #define pq priority_queue #define FOR(i, a, b) for(int i=(a);i<(b);++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--) #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using namespace std; // types typedef pair<int, int> P; typedef pair<ll, int> Pl; typedef pair<ll, ll> Pll; typedef pair<double, double> Pd; // constants const int inf = 1e9; const ll linf = 1LL << 50; const double EPS = 1e-10; const int mod = 1e9 + 7; // solve template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;} template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;} bool comp(const Pl &x, const Pl &y){ return x.first < y.first; } int main(int argc, char const* argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); int n; ll l; cin >> n >> l; vector<ll> x(n), y(n); rep(i, n)cin >> x[i]; rep(i, n)cin >> y[i]; vector<int> d(2 * n, 0); int mini = inf, maxi = -1; int mi = 0, ma = 0; vector<Pl> vec; rep(i, n){ vec.pb(mk(x[i], 1)); vec.pb(mk(y[i], -1)); } sort(all(vec), comp); rep(i, 2 * n){ d[i] = vec[i].second; if(i > 0)d[i] += d[i-1]; maxi = max(maxi, d[i]); if(maxi <= d[i]){ maxi = d[i]; ma = i; } if(mini > d[i]){ mini = d[i]; mi = i; } } ll tmp = 0; if(mini < 0){ tmp = vec[mi].first; vector<Pl> v; bool flag = true; for(int i = mi + 1; i < 2 * n; i++){ v.pb(mk(vec[i].first - tmp, vec[i].second)); } for(int i = 0; i < mi; i++){ if(flag && vec[i].second == 1){ flag = false; continue; } v.pb(mk(vec[i].first - tmp + l, vec[i].second)); } d.assign(2 * n - 2, 0); maxi = -1, mini = inf, ma = 0, mi = 0; rep(i, 2 * n - 2){ d[i] = v[i].second; if(i > 0)d[i] += d[i-1]; maxi = max(maxi, d[i]); if(maxi <= d[i]){ maxi = d[i]; ma = i; } if(mini > d[i]){ mini = d[i]; mi = i; } } ll res = (maxi - 1) * l + v[ma+1].first; cout << res + tmp << endl; }else{ ll res = (maxi - 1) * l + vec[ma+1].first; cout << res + tmp << endl; } return 0; }