#include <bits/stdc++.h>
using namespace std;
int const N = 200000;
int n, L, x[N], y[N];
pair<int, int> xy[N << 1];
pair<int, int> sg[N];
inline int dec(int i) { return i ? i - 1 : n - 1; }
inline int inc(int i) { return i + 1 == n ? 0 : i + 1; }
inline int dis(int a, int b) { return a <= b ? b - a : L - a + b; }
bool ok(int m) {
  int l = lower_bound(y, y + n, x[0]) - y, r = dec(l), fl = 0, fr = 0, c = 0,
      d = 0;
  while (xy[c].second) ++c;
  while (fl < n && dis(y[dec(l)], x[0]) <= m) l = dec(l), ++fl;
  while (fr < n && dis(x[0], y[inc(r)]) <= m) r = inc(r), ++fr;
  sg[0] = make_pair(-fl + 1, fr);
  for (int i = 1; i < (int)(n); ++i) {
    ++c;
    ++d;
    while (xy[c].second != i) ++c, ++fl, --fr, --d;
    while (fl && dis(y[l], x[i]) > m) l = inc(l), --fl;
    while (fr < 0) r = inc(r), ++fr;
    while (fr < n && dis(x[i], y[inc(r)]) <= m) r = inc(r), ++fr;
    sg[i] = make_pair(-fl - d + 1, fr - d);
  }
  int mn = 1e9, mx = -1e9;
  for (int i = 0; i < (int)(n); ++i)
    mx = max(mx, sg[i].first), mn = min(mn, sg[i].second);
  return mn >= mx;
}
int main() {
  scanf("%d%d", &n, &L);
  for (int i = 0; i < (int)(n); ++i) scanf("%d", x + i);
  sort(x, x + n);
  for (int i = 0; i < (int)(n); ++i) xy[i] = make_pair(x[i], i);
  for (int i = 0; i < (int)(n); ++i) scanf("%d", y + i);
  sort(y, y + n);
  for (int i = 0; i < (int)(n); ++i) xy[i + n] = make_pair(y[i], i + n);
  sort(xy, xy + (n << 1));
  int l = 0, r = L >> 1;
  while (r > l) {
    int m = l + r >> 1;
    if (ok(m))
      r = m;
    else
      l = m + 1;
  }
  printf("%d\n", l);
}
