#include <bits/stdc++.h>
using namespace std;
const int maxn = 5 * 1e5 + 10;
int t;
int n, len;
int a[maxn], b[4 * maxn];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
bool pd(int x) {
  int l = 1, r = n * 3;
  for (int i = 1; i <= n; i++) {
    while (a[i] - b[l] > x) l++;
    while (b[r] - a[i] > x) r--;
    l++, r++;
    if (l > r) return 0;
  }
  return 1;
}
void qqsort(int *a, int l, int r) {
  int i = l, j = r, mid = a[(l + r) / 2];
  do {
    while (a[i] < mid) i++;
    while (a[j] > mid) j--;
    if (i <= j) {
      int tmp = a[i];
      a[i] = a[j];
      a[j] = tmp;
      i++, j--;
    }
  } while (i <= j);
  if (i < r) qqsort(a, i, r);
  if (j > l) qqsort(a, l, j);
}
int main() {
  n = read();
  len = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) b[i] = read();
  qqsort(a, 1, n);
  qqsort(b, 1, n);
  for (int i = 1; i <= n; i++) b[i] -= len;
  for (int i = n + 1; i <= 3 * n; i++) b[i] = b[i - n] + len;
  int l = 0, r = len, ans, mid;
  while (r >= l) {
    mid = (l + r) >> 1;
    if (pd(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
}
