#include <bits/stdc++.h>
using namespace std;
long long n, L, a[200005], b[200005 << 2], c[200005], m;
int judge(int mid) {
  int l = 1, r = m;
  for (int i = 1; i <= n; i++) {
    while (a[i] - mid > b[l]) ++l;
    while (a[i] + mid < b[r]) --r;
    if (l > r) return 0;
    ++l, ++r;
  }
  return 1;
}
int main() {
  cin >> n >> L;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> c[i];
  sort(c + 1, c + 1 + n);
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++) b[i] = c[i] - L;
  for (int i = 1; i <= n; i++) b[i + n] = c[i];
  for (int i = 1; i <= n; i++) b[i + 2 * n] = c[i] + L;
  m = 3 * n;
  int l = 0, r = L, ans, mid;
  while (l < r) {
    mid = l + r >> 1;
    if (judge(mid))
      ans = mid, r = mid;
    else
      l = mid + 1;
  }
  cout << r << '\n';
}
