#include using namespace std; using i64 = long long; int main() { i64 N,L; cin >> N >> L; vector x(N); vector y(N); for(int i = 0;i < N;i++) cin >> x[i]; for(int i = 0;i < N;i++) cin >> y[i]; vector> vec; for(int i = 0;i < N;i++) vec.push_back({x[i], 1}); for(int i = 0;i < N;i++) vec.push_back({y[i], -1}); vector d(N + 1, 0); sort(begin(vec), end(vec)); i64 MAX = 0; i64 MI = 0; i64 MIN = 0; for(int i = 0;i < N * 2;i++) { d[i + 1] = d[i] + vec[i].second; if(MAX <= d[i + 1]) { MAX = d[i + 1]; MI = i; } if(d[i + 1] <= MIN) { MIN = d[i + 1]; } } i64 delta = MAX - MIN; if(MAX == 0) { cout << delta * L + y.front(); } else { cout << (delta - 1) * L + vec[MI + 1].first; } cout << endl; }