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