#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'; } } int main() { usize N = 0; u64 L = 0; scan<' '>(N); scan<'\n'>(L); std::vector x(N + 1), y(N + 1); 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; 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; u64 ans; if (max == static_cast(0)) { ans = L*diff + y.front(); } else { ans = L*diff - (L - last); } printf("%llu\n", static_cast(ans)); return 0; }