#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]; 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 ans = 1e+15; rep(i, n) { ans = min(ans, simurate(i)); } cout << ans << endl; return 0; }