#include <bits/stdc++.h>
using namespace std;
int n, L;
int a[800000], b[800000];
inline int read() {
  int x = 0;
  char c = getchar();
  while (c > '9' || c < '0') c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x;
}
void pre() {
  n = read(), L = read();
  for (register int i = 1; i <= n; i++) a[i] = read();
  for (register int i = 1; i <= n; i++)
    b[i] = read(), 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);
}
bool check(int x) {
  int l = 1, r = n * 3;
  for (register int i = 1; i <= n; i++) {
    while (a[i] - b[l] > x) ++l;
    while (b[r] - a[i] > x) --r;
    ++l, ++r;
  }
  return l <= r;
}
void work() {
  int l = 0, r = (L + 1) >> 1, mid;
  while (l < r) {
    mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
}
int main() {
  pre();
  work();
  return 0;
}
