#include <bits/stdc++.h>
using namespace std;
const long long maxm = 2e5 + 5;
long long a[maxm];
long long b[maxm * 3];
long long n, L;
long long check(long long mid) {
  long long l = 0, r = n * 3 + 1;
  long long mi = 1e18;
  b[0] = -1e18;
  b[n * 3 + 1] = 1e18;
  for (long long i = 1; i <= n; i++) {
    while (a[i] - mid > b[l]) l++;
    while (a[i] + mid < b[r]) r--;
    if (l > r) return 0;
    mi = min(mi, i - l);
    if (i - r > mi) return 0;
    r++;
  }
  return 1;
}
signed main() {
  ios::sync_with_stdio(0);
  cin >> n >> L;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= n; i++) cin >> b[i];
  for (long long i = 1; i <= n; i++) {
    b[i + n] = b[i] - L;
    b[i + n * 2] = b[i] + L;
  }
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n * 3);
  long long l = 0, r = (L + 1) / 2;
  long long ans = L;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
