#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, L;
  cin >> n >> L;
  long long a[n], b[3 * n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
    b[n + i] = b[i] + L;
    b[2 * n + i] = b[i] - L;
  }
  sort(a, a + n);
  sort(b, b + 3 * n);
  auto check = [&](long long val) {
    long long l = 0, r = 3 * n - 1;
    for (long long i = 0; i < n; i++) {
      while (a[i] - b[l] > val) l++;
      while (b[r] - a[i] > val) r--;
      l++, r++;
    }
    return (l <= r);
  };
  long long l = 0, r = L;
  while (l < r) {
    long long mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << "\n";
}
