#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
long long A[N], B[N];
long long C[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  long long L;
  cin >> n >> L;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> B[i];
  }
  sort(A, A + n), sort(B, B + n);
  for (int i = 0; i < n; i++) {
    B[i + n] = B[i];
    B[i + 2 * n] = B[i] + L;
    B[i] = B[i] - L;
  }
  long long la = 0, ra = L / 2;
  while (la < ra) {
    long long m = (la + ra) >> 1;
    memset(C, 0, sizeof C);
    for (int i = 0; i < n; i++) {
      int lat = lower_bound(B, B + 3 * n, A[i] - m) - B - n - i;
      int rat = upper_bound(B, B + 3 * n, A[i] + m) - B - n - 1 - i;
      if (rat < lat) continue;
      if (rat - lat + 1 >= n) {
        C[0]++;
        continue;
      }
      while (lat < 0) lat += n;
      while (lat >= n) lat -= n;
      while (rat < 0) rat += n;
      while (rat >= n) rat -= n;
      if (lat <= rat) {
        C[lat]++, C[rat + 1]--;
      } else {
        C[lat]++, C[0]++, C[rat + 1]--;
      }
    }
    long long mx = 0;
    for (int i = 0; i < n; i++) {
      if (i) C[i] += C[i - 1];
      mx = max(mx, C[i]);
    }
    if (mx >= n)
      ra = m;
    else
      la = m + 1;
  }
  cout << la << "\n";
  return 0;
}
