#include #include #include #include #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const int MOD = 1000000007; // 998244353 const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; /*-------------------------------------------------*/ int main() { cin.tie(0); ios::sync_with_stdio(false); // freopen("input.txt", "r", stdin); int n, l; cin >> n >> l; vector > xy; // true -> sushi REP(i, n) { int x; cin >> x; xy.emplace_back(x, true); } REP(i, n) { int y; cin >> y; xy.emplace_back(y, false); } sort(ALL(xy)); vector d(2 * n); int m = -INF, m_ = INF, p, q; REP(i, 2 * n) { d[i] = (xy[i].second ? 1 : -1); if (i > 0) d[i] += d[i - 1]; if (d[i] >= m) { m = d[i]; p = i; } if (d[i] < m_) { m_ = d[i]; q = i; } } long long t = 0; if (m_ < 0) { int bye = -1; REP(i, q) { if (xy[i].second) { bye = i; break; } } vector > tmp(xy); xy.clear(); FOR(i, q + 1, 2 * n) { int umg = tmp[i].first - tmp[q + 1].first; xy.emplace_back(umg, tmp[i].second); } REP(i, q) if (i != bye) { int umg = l - tmp[q + 1].first + tmp[i].first; xy.emplace_back(umg, tmp[i].second); } if (bye != -1) { int umg = l - tmp[q + 1].first + tmp[q].first; xy.emplace_back(umg, tmp[q].second); n -= 2; } t += tmp[q + 1].first; m = -INF; REP(i, 2 * n) { d[i] = (xy[i].second ? 1 : -1); if (i > 0) d[i] += d[i - 1]; if (d[i] >= m) { m = d[i]; p = i; } } } t += (long long)(m - 1) * l + xy[p + 1].first; cout << t << '\n'; return 0; }