#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 50;
const int INF = 1e9;
int n, L;
long long a[maxn << 1], b[maxn << 2];
inline bool check(int len) {
  register int mx = -INF, x = 1, y = 1;
  for (register int i = 1; i <= n * 2; ++i) {
    while (x <= 4 * n && b[x] < a[i] - len) x++;
    while (y <= 4 * n && b[y] <= a[i] + len) y++;
    mx = max(mx, x - i);
    if (y - i - 1 < mx) return 0;
  }
  return 1;
}
signed main() {
  scanf("%d%d", &n, &L);
  for (register int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
  for (register int i = 1; i <= n; ++i) scanf("%lld", &b[i]);
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  for (register int i = 1; i <= n; ++i) a[i] += L, a[n + i] = a[i] + L;
  for (register int i = 1; i <= 3 * n; ++i) b[n + i] = b[i] + L;
  register int l = 0, r = L / 2;
  while (l < r) {
    register int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  return !printf("%d\n", l);
}
