#include #include #include #include #include #include using isize = std::ptrdiff_t; using usize = std::size_t; using u64 = std::uint_fast64_t; template void scan(T &d) { int c; while (c = fgetc(stdin), c != C) { d = d * 10 + c - '0'; } } static constexpr usize MaxN = 100000; usize N, diff; u64 L, ans; isize count, max, min; std::array x, y; int main() { scan<' '>(N); scan<'\n'>(L); const usize li = N - 1; for (usize i = 0;i != li;++i) { scan<' '>(x[i]); } scan<'\n'>(x[li]); x[N] = L; for (usize i = 0;i != li;++i) { scan<' '>(y[i]); } scan<'\n'>(y[li]); y[N] = L; 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; } } diff = max - min; if (max == static_cast(0)) { ans = L*diff + y.front(); } else { ans = L*diff - (L - last); } printf("%llu\n", static_cast(ans)); return 0; }