#include #include #include #define int long long #define rep(i, n) for(i = 0; i < n; i++) using namespace std; int n, l; int x[100000]; int y[100000]; //lastについて狭義凸な関数 int simurate(int last) { set A; set B; set::iterator it; int i; rep(i, n) if (i > 0) A.insert(x[i]); rep(i, n) if (i != last) B.insert(y[i]); int now = x[0]; int ret = 0; rep(i, 2 * n - 2) { if (i % 2 == 0) { it = B.lower_bound(now); if (it == B.end()) it = B.lower_bound(0); int nxt = (*it); if (nxt > now) { ret += nxt - now; } else { ret += l + nxt - now; } B.erase(it); now = nxt; } else { it = A.lower_bound(now); if (it == A.end()) it = A.lower_bound(0); int nxt = (*it); if (nxt > now) { ret += nxt - now; } else { ret += l + nxt - now; } A.erase(it); now = nxt; } } if (y[last] > now) ret += y[last] - now; else ret += l + y[last] - now; ret += x[0]; return ret; } signed main() { int i; cin >> n >> l; rep(i, n) cin >> x[i]; rep(i, n) cin >> y[i]; int st = 0, ed = n - 1, mid; while (ed - st >= 2) { mid = (st + ed) / 2; int sub = simurate(mid + 1) - simurate(mid); if (sub < 0) st = mid; else ed = mid; } cout << min(simurate(0), simurate(st + 1)) << endl; return 0; }