#include #include #include #include #include using isize = std::ptrdiff_t; using usize = std::size_t; using u64 = std::uint_fast64_t; int main() { usize N; std::cin >> N; u64 L; std::cin >> L; std::vector x(N); for (auto &e : x) { std::cin >> e; } std::vector y(N); for (auto &e : y) { std::cin >> e; } x.emplace_back(std::numeric_limits::max()); y.emplace_back(std::numeric_limits::max()); isize count = 0, max = 0, min = 0; auto xitr = x.cbegin(), yitr = y.cbegin(); u64 last; for (usize i = 0;i != N * 2;++i) { if (*xitr < *yitr) { ++count; if (max <= count) { max = count; last = *yitr; } ++xitr; } else { --count; if (count <= min) { min = count; } ++yitr; } } const usize diff = max - min; if (max == static_cast(0)) { std::abort(); } else if (min == static_cast(0)) { std::cout << y.back() << std::endl; } else { std::cout << L*diff - (L - last) << std::endl; } return 0; }