#include <bits/stdc++.h>
using namespace std;
int n, L;
int a[200005], b[200005 * 3];
inline void next(int &t) {
  t++;
  if (t > n) t = 1;
}
inline void pre(int &t) {
  t--;
  if (!t) t = n;
}
inline int gdis(int a, int b) { return min(abs(a - b), L - abs(a - b)); }
int num[200005];
int np;
inline int gets(int x) { return x >= 0 ? x : x + n; }
inline void add(int l, int r) {
  if (l > r) return;
  int L = gets(l - np), R = gets(r - np);
  if (L > R) {
    num[L]++;
    num[n + 1]--;
    num[0]++;
    num[R + 1]--;
    return;
  }
  num[L]++, num[R + 1]--;
  return;
}
inline void addl(int l, int r) {
  if (l > r) return;
  if (l <= n) {
    add(l, n);
    add(1, r - n);
  } else {
    add(l - n, r - n);
  }
  return;
}
inline void addr(int l, int r) {
  if (l > r) return;
  if (r > n * 2) {
    add(l - n, n);
    add(1, r - n * 2);
  } else {
    add(l - n, r - n);
  }
  return;
}
bool check(int d) {
  memset(num, 0, sizeof(num));
  int p = n + 1;
  while (!(b[p - 1] <= a[1] && a[1] <= b[p])) p++;
  for (int i = 1; i <= n; i++) {
    np = i;
    int l = 1, r = p;
    while (l < r) {
      int mid = (l + r) >> 1;
      if (a[i] - b[mid] <= d)
        r = mid;
      else
        l = mid + 1;
    }
    addl(l, p - 1);
    int tc = l;
    r = min(n * 3, l + n * 2 - 1);
    l = p - 1;
    while (l < r) {
      int mid = ((l + r) >> 1) + 1;
      if (b[mid] - a[i] <= d)
        l = mid;
      else
        r = mid - 1;
    }
    if (l <= n * 2) {
      l = min(l, tc + n - 1);
    }
    addr(p, l);
    if (i < n) {
      while (!(b[p - 1] <= a[i + 1] && a[i + 1] <= b[p])) p++;
    }
  }
  int c = 0;
  for (int i = 0; i <= n; i++) {
    c += num[i];
    if (c == n) return 1;
  }
  return 0;
}
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 + n]);
  sort(a + 1, a + n + 1);
  sort(b + n + 1, b + n * 2 + 1);
  for (int i = n + 1; i <= n * 2; i++) b[i + n] = b[i] + L;
  for (int i = 1; i <= n; i++) b[i] = b[i + n] - L;
  int l = 0, r = L;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}
