#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar();
  return x * f;
}
int a[N], b[N], n;
bool check(int x) {
  int l = 1, r = 3 * n;
  for (int i = 1; i <= n; i++) {
    int R = a[i] + x;
    int L = a[i] - x;
    while (b[l] < L) l++;
    while (b[r] > R) r--;
    if (l > r) return 0;
    l++, r++;
  }
  return 1;
}
int main() {
  n = read();
  int L = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) {
    b[i] = read();
    b[i + n] = b[i] + L;
    b[i + 2 * n] = b[i] - L;
  }
  sort(1 + a, 1 + a + n);
  sort(1 + b, 1 + b + 3 * n);
  int l = 0, r = L, ans = L;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
}
