/* ---------- STL Libraries ---------- */ // IO library #include #include #include #include #include // algorithm library #include #include #include #include #include // container library #include #include #include #include #include #include #include #include #include #include #include /* ---------- Namespace ---------- */ using namespace std; /* ---------- Type ---------- */ using ll = long long; #define int ll #define P pair /* ---------- Constants */ const double PI = 3.141592653589793238462643383279; const ll MOD = 1e9 + 7; const int INF = 1LL << 55; /* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */ signed main() { int N, L; cin >> N >> L; vector sushis(N); set tea_set; for (int i = 0; i < N; i++) cin >> sushis[i]; for (int i = 0; i < N; i++) { int y; cin >> y; tea_set.insert(y); } int circle_num = 0; int last_sushi_pos = sushis[0]; for (int i = 1; i < N; i++) { auto it = lower_bound(tea_set.begin(), tea_set.end(), sushis[i]); if (it == tea_set.begin()) { it = tea_set.end(); it--; tea_set.erase(it); circle_num++; } else { it--; if (*it > last_sushi_pos) { tea_set.erase(it); } else { tea_set.erase(it); circle_num++; } } } int last_pos = *tea_set.begin(); cout << circle_num * L + last_pos << endl; return 0; }