#include <bits/stdc++.h>
using namespace std;
void qmax(int &x, int y) {
  if (x < y) x = y;
}
void qmin(int &x, int y) {
  if (x > y) x = y;
}
inline int read() {
  char s;
  int k = 0, base = 1;
  while ((s = getchar()) != '-' && s != EOF && !(isdigit(s)))
    ;
  if (s == EOF) exit(0);
  if (s == '-') base = -1, s = getchar();
  while (isdigit(s)) k = k * 10 + (s ^ '0'), s = getchar();
  return k * base;
}
inline void write(int x) {
  static char cnt, num[15];
  cnt = 0;
  if (!x) {
    putchar('0');
    return;
  }
  for (; x; x /= 10) num[++cnt] = x % 10;
  for (; cnt; putchar(num[cnt--] + 48))
    ;
}
const int maxn = 6e5 + 100;
int n;
long long L;
long long a[maxn], b[maxn];
bool check(int x) {
  int l1, r1;
  l1 = 1;
  r1 = n * 3;
  for (int i = 1; i <= n; i++) {
    while (r1 >= 1 && b[r1] - a[i] > x) r1--;
    while (l1 <= n * 3 && a[i] - b[l1] > x) l1++;
    l1++;
    r1++;
  }
  if (l1 <= r1) return true;
  return false;
}
const long long mod = 20000909;
void ksm(long long x, long long y) {
  long long sum = 1;
  while (y) {
    if (y % 2 == 1) sum = sum * x % mod;
    x = x * x % mod;
    y /= 2;
  }
  printf("%lld\n", sum);
}
int main() {
  n = read();
  L = read();
  b[n * 3 + 1] = 5e9;
  for (int i = 1; i <= n; i++) {
    a[i] = read() + L;
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    b[i * 3 - 2] = read();
    b[i * 3 - 1] = b[i * 3 - 2] + L;
    b[i * 3] = b[i * 3 - 1] + L;
  }
  sort(b + 1, b + n * 3 + 1);
  int l = 0, r = L, mid, ans = L;
  while (l <= r) {
    mid = (l + r) / 2;
    if (check(mid)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
