#include <bits/stdc++.h>
using namespace std;
int n, L, l, r, mid, ll, rr, x, n3;
long long a[600005], b[600005];
int main() {
  scanf("%d%d", &n, &L);
  for (int i = 1; i <= n; i++) scanf("%I64d", a + i);
  for (int i = 1; i <= n; i++) scanf("%I64d", b + i);
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  n3 = n * 3;
  for (int i = n + 1; i <= n3; i++) a[i] = a[i - n] + L, b[i] = b[i - n] + L;
  a[n3 + 1] = 1LL << 60;
  int l = 0, r = L / 2;
  while (l < r) {
    mid = (l + r) >> 1;
    ll = -n, rr = n;
    for (int i = n + 1, _end = n * 2; i <= _end; i++) {
      x = lower_bound(a + 1, a + n3 + 1, b[i] - mid) - a;
      ll = max(ll, x - i);
      x = upper_bound(a + 1, a + n3 + 1, b[i] + mid) - a - 1;
      rr = min(rr, x - i);
    }
    if (ll <= rr)
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}
