#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream &operator<<(ostream &os, vector<T> v) {
  for (long long i = (0); i < ((long long)(v).size()); i++)
    os << (i ? " " : "") << v[i];
  return os;
}
const bool debug = 1;
const long long INF = 1e18;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, L;
  cin >> n >> L;
  vector<long long> a(n), b(n);
  for (long long i = (0); i < (n); i++) cin >> a[i];
  for (long long i = (0); i < (n); i++) cin >> b[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  long long lo = 0, hi = L / 2;
  while (hi - lo >= 2) {
    long long mid = (lo + hi) / 2;
    auto pos = [&](long long i) { return L * (i / n) + b[i % n]; };
    long long cur = 0;
    long long lLim = -INF, rLim = INF;
    for (long long i = (0); i < (n); i++) {
      long long me = L + a[i];
      while (pos(cur) < me - mid) cur++;
      lLim = max(lLim, cur - i);
    }
    cur = 3 * n;
    for (long long i = (n); i-- > (0);) {
      long long me = L + a[i];
      while (pos(cur) > me + mid) cur--;
      rLim = min(rLim, cur - i);
    }
    if (lLim <= rLim)
      hi = mid;
    else
      lo = mid;
  }
  cout << hi << "\n";
}
