#include using namespace std; using ll = long long; auto find_next_core(int p, set &ps){ auto it_next = ps.upper_bound(p); if (it_next == ps.end()) it_next = ps.begin(); return it_next; } auto find_prev_core(int p, set &ps){ auto it_next = ps.lower_bound(p); if (it_next == ps.begin()) it_next = ps.end(); auto it_prev = prev(it_next); return it_prev; } int find_and_erase_next(int y0, set &xs, set &ys) { auto it_x = find_next_core(y0, xs); auto it_y = find_next_core(*it_x, ys); auto it_next = find_prev_core(*it_y, xs); int next = *it_next; xs.erase(it_next); return next; } ll solve(int N, int L, set &xs, set &ys){ ll ans = 0; int x = 0; int y = 0; for (int i = 0; i < N; ++i){ x = find_and_erase_next(y, xs, ys); ans += (x > y) ? (x - y):(L + x - y); y = find_and_erase_next(x, ys, xs); ans += (y > x) ? (y - x):(L + y - x); } return ans; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N, L; cin >> N >> L; int x, y; set xs, ys; for (int i = 0; i < N; ++i){ cin >> x; xs.insert(xs.end(), x); } for (int i = 0; i < N; ++i){ cin >> y; ys.insert(ys.end(), y); } cout << solve(N, L, xs, ys) << endl; return 0; }