#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}
const int MX = 1e6 + 7, EE = 2e5 + 7;
int n, L;
long long int a[MX], b[MX];
bool f(int sr) {
  int l = 0, r = 3 * n - 1;
  for (int i = 0; i < n; ++i) {
    int lewe = (lower_bound(b, b + 3 * n, a[i] - sr) - b),
        prawe = (upper_bound(b, b + 3 * n, a[i] + sr) - b - 1);
    l = max(l, lewe - i);
    r = min(r, prawe - i);
  }
  return l <= r;
}
int32_t main() {
  scanf("%d%d", &n, &L);
  for (int i = 0; i < n; ++i) scanf("%lld", &a[i]), a[i] += L;
  for (int i = 0; i < n; ++i) scanf("%lld", &b[i]);
  sort(a, a + n);
  sort(b, b + n);
  for (int i = n; i < 3 * n; ++i) b[i] = b[i % n] + L * (i / n);
  int pocz = 0, kon = 1e9;
  while (pocz < kon) {
    int sr = (pocz + kon) / 2;
    if (f(sr))
      kon = sr;
    else
      pocz = sr + 1;
  }
  printf("%d", pocz);
}
