#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const long long INFL = 1e18 + 123;   const double PI = atan2(0, -1);   mt19937 tw(960172);   long long rnd(long long x, long long y) {     static uniform_int_distribution<long long> d;     return d(tw) % (y - x + 1) + x;   }   int cur_sum, deleted_sum;   vector<tuple<int, int, int>> back, front;   void init() {     cur_sum = 0;     deleted_sum = 0;     back.clear();     front.clear();   }   int get_min_back() {     if (((int)(back).size())) {       return get<2>(back.back());     }     return INF;   }   int get_min_front() {     if (((int)(front).size())) {       return get<2>(front.back());     }     return INF;   }   void add(int val) {     cur_sum += val;     back.push_back({val, cur_sum, min(cur_sum, get_min_back())});   }   void pop() {     assert(((int)(back).size()) || ((int)(front).size()));     if (!((int)(front).size())) {       while (((int)(back).size())) {         int val, sum, tmp;         tie(val, sum, tmp) = back.back();         back.pop_back();         front.push_back({val, sum, min(sum, get_min_front())});       }     }     deleted_sum += get<0>(front.back());     front.pop_back();   }   int get_min() { return min(get_min_front(), get_min_back()) - deleted_sum; }   void solve() {     int n, l;     cin >> n >> l;     vector<int> boys, girls;     for (int i = 0; i < n; ++i) {       int num;       cin >> num;       boys.push_back(num);       boys.push_back(num + l);     }     for (int i = 0; i < n; ++i) {       int num;       cin >> num;       girls.push_back(num - l);       girls.push_back(num);       girls.push_back(num + l);     }     sort(boys.begin(), boys.end());     sort(girls.begin(), girls.end());     int lg = -1, rg = l / 2;     while (rg - lg > 1) {       int mid = (lg + rg) / 2;       init();       bool flag = true;       int c1 = 0, c2 = 0, c3 = 0;       while (girls[c2] - mid <= boys[c1]) {         ++c2;       }       c3 = c2;       while (c3) {         if (boys[0] - girls[c3 - 1] <= mid) {           --c3;         } else {           break;         }       }       int c0 = c2;       for (int i = 0; i < n; ++i) {         while (girls[c3] < boys[i] - mid) {           ++c3;         }         while (girls[c0] - mid <= boys[i]) {           ++c0;         }         c2 = max(c2, c0);         while (true) {           if (girls[c2] - mid <= boys[c1]) {             if (c2 == c3 + n) {               break;             }             add(1);             ++c2;           } else {             if (boys[c1] + mid >= boys[i] + l - mid) {               break;             }             add(-1);             ++c1;           }         }         if (c0 - c3 + get_min() < 0) {           flag = false;           break;         }         if (i == n - 1) {           break;         }         int tmp = deleted_sum;         pop();         --tmp;         assert(tmp == deleted_sum);         while ((((int)(front).size()) || ((int)(back).size())) &&                girls[c0] - mid <= boys[i + 1]) {           pop();           ++tmp;           ++c0;         }         assert(tmp == deleted_sum);       }       if (flag) {         rg = mid;       } else {         lg = mid;       }     }     cout << rg <<   n ;   }   int main() {     cerr << fixed << setprecision(15);     cout << fixed << setprecision(15);     ios::sync_with_stdio(false);     int tests = 1;     for (int it = 1; it <= tests; ++it) {       solve();     }     return 0;   }