#include using namespace std; int main () { int n, L; cin >> n >> L; int x[n], y[n]; for (int i = 0; i < n; i++) cin >> x[i]; for (int i = 0; i < n; i++) cin >> y[i]; int i = 0, j = 0, prev = 0, count = 0, pos = 0; long long ans = 0; int consecCount[2] = {0, 0}; while (i < n || j < n) { if (j == n) { if (prev == 1) count = 0, pos = x[i]; count += n - i; i = n; } else if (i == n) { if (prev == 0) count = 0, pos = y[j]; count += n - j; j = n; } else if (x[i] < y[j]) { if (prev == 0) count++; else { prev = 0; count = 1; pos = x[i]; } i++; } else { if (prev == 1) count++; else { prev = 1; count = 1; pos = y[j]; } j++; } consecCount[prev] = max(consecCount[prev], count); int loops = count; if (prev == 1 && consecCount[0] < count) loops++; ans = max(ans, (long long)(loops - 1) * L + pos); } cout << ans << endl; }