#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, L;
  scanf("%d %d", &n, &L);
  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  for (int i = 0; i < n; ++i) scanf("%d", &b[i]);
  sort((a).begin(), (a).end());
  sort((b).begin(), (b).end());
  auto dist = [&](int i, int j) {
    i = (i + n) % n;
    j = (j + n) % n;
    int d = abs(a[i] - b[j]);
    return min(d, L - d);
  };
  if (n == 1) {
    printf("%d\n", dist(0, 0));
    return 0;
  }
  function<bool(int)> check = [&](int m) {
    vector<int> l(n), r(n);
    vector<int> cnt(n + 1, 0);
    for (int i = 0; i < n; ++i) {
      int left = (lower_bound((b).begin(), (b).end(), (a[i] - m + L) % L) -
                  b.begin()) %
                 n;
      int right =
          (upper_bound((b).begin(), (b).end(), (a[i] + m) % L) - b.begin()) % n;
      if (dist(i, left) > m || dist(i, right - 1) > m) return false;
      l[i] = (left - i + n) % n;
      r[i] = (right - i + n) % n;
      if (l[i] < r[i]) {
        ++cnt[l[i]];
        --cnt[r[i]];
      } else {
        ++cnt[0];
        --cnt[r[i]];
        ++cnt[l[i]];
      }
    }
    for (int i = 1; i <= n; ++i) {
      cnt[i] += cnt[i - 1];
      if (cnt[i] == n) return true;
    }
    return false;
  };
  int low = -1, hi = L / 2, mid;
  while (hi - low > 1) {
    mid = (low + hi) / 2;
    if (check(mid))
      hi = mid;
    else
      low = mid;
  }
  printf("%d\n", hi);
}
