#include <bits/stdc++.h>
using namespace std;
const int MAXL = (int)6e5 + 10;
int n, l;
int a[MAXL];
int b[MAXL];
bool chk(int x) {
  int left = 0, right = 3 * n - 1;
  for (int i = 0; i < n; i++) {
    for (; b[left] < a[i] - x; left++)
      ;
    for (; b[right] > a[i] + x; right--)
      ;
    if (left > right) return false;
    left++;
    right++;
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &l);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) scanf("%d", &b[i]);
  for (int i = 0; i < n; i++) {
    b[i + n] = b[i] - l;
    b[i + 2 * n] = b[i] + l;
  }
  sort(a, a + n);
  sort(b, b + 3 * n);
  int left = 0, right = l >> 1;
  while (left <= right) {
    int mid = (left + right) >> 1;
    if (chk(mid))
      right = mid - 1;
    else
      left = mid + 1;
  }
  printf("%d\n", left);
  return 0;
}
