#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 500;
int g[N * 2], ls[N], rs[N], a[N], b[N], n, L;
struct Q {
  int id, val;
} q[N * 2];
bool check(int len) {
  if (n == 1) {
    int ll = min(fabs((a[1] - b[1] + L) % L), fabs((b[1] - a[1] + L) % L));
    if (len < ll) return false;
  }
  for (int i = 1; i <= n; i++) g[i] = a[i], g[i + n] = a[i] + L;
  int l = 1, r = n * 2, st = 0, tl = -1;
  for (int i = 1; i <= n; i++) {
    while (l <= n * 2 && g[l] < b[i] + L - len) ++l;
    ls[i] = l - (i + n);
    while (r > 0 && g[r] > b[n - i + 1] + len) --r;
    rs[n - i + 1] = r - (n - i + 1);
  }
  for (int i = 1; i <= n * 2; i++) {
    int j = i > n ? i - n : i;
    while (st <= tl && q[st].id <= i - n) ++st;
    if (st <= tl && q[st].val > rs[j]) return false;
    q[++tl] = (Q){i, ls[j]};
    while (st < tl && q[tl].val > q[tl - 1].val) q[tl - 1] = q[tl], --tl;
  }
  return true;
}
int work(void) {
  int l = 0, r = L - 1, ret = r;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ret = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return ret;
}
int main(void) {
  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]);
  sort(a + 1, a + 1 + n), sort(b + 1, b + 1 + n);
  int ans = work();
  for (int i = 1; i <= n - i + 1; i++)
    swap(a[i], a[n - i + 1]), swap(b[i], b[n - i + 1]);
  for (int i = 1; i <= n; i++) a[i] = L - a[i], b[i] = L - b[i];
  ans = min(ans, work());
  printf("%d\n", ans);
  return 0;
}
