#include <bits/stdc++.h>
using namespace std;
long long one[200001], two[200001];
vector<long long> pl;
int main() {
  int n;
  long long m;
  scanf("%d%lld", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &one[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", &two[i]);
  }
  sort(one, one + n);
  sort(two, two + n);
  pl.resize(n * 3);
  for (int i = 0; i < n; i++) {
    pl[i] = two[i] - m;
  }
  for (int i = 0; i < n; i++) {
    pl[i + n] = two[i];
  }
  for (int i = 0; i < n; i++) {
    pl[i + n + n] = two[i] + m;
  }
  long long ss = 0, ee = (m / 2) + 1, ans = (m / 2) + 2;
  while (ss <= ee) {
    long long mid = (ss + ee) / 2;
    int msd = -4 * n, med = 4 * n;
    for (int i = 0; i < n; i++) {
      auto sit = lower_bound(pl.begin(), pl.end(), one[i] - mid);
      auto eit = upper_bound(pl.begin(), pl.end(), one[i] + mid);
      int sd = distance(pl.begin(), sit) - i;
      int ed = distance(pl.begin(), eit) - 1 - i;
      msd = max(msd, sd);
      med = min(med, ed);
    }
    if (msd <= med) {
      ans = mid;
      ee = mid - 1;
    } else {
      ss = mid + 1;
    }
  }
  printf("%lld\n", ans);
}
