#include <bits/stdc++.h>
int64_t a[400005], b[200005];
int64_t len;
int n;
bool check(int64_t value) {
  std::vector<std::pair<int64_t, int64_t>> v;
  for (int i = 0; i < n; ++i) {
    auto rg = std::make_pair(b[i] - value, b[i] + value);
    if (rg.first < 0) {
      rg.first += len;
      rg.second += len;
    }
    v.push_back(rg);
    if (rg.second < len) v.emplace_back(rg.first + len, rg.second + len);
  }
  std::sort(v.begin(), v.end());
  int pa = 0, pr = 0;
  while (pr < (int)v.size()) {
    while (pa < n * 2 && (a[pa] < v[pr].first || a[pa] > v[pr].second)) ++pa;
    if (pa >= n * 2)
      return false;
    else {
      ++pa;
      ++pr;
    }
  }
  return true;
}
int main(int argc, char* argv[]) {
  scanf("%d%lld", &n, &len);
  for (int i = 0; i < n; ++i) scanf("%lld", &a[i]);
  for (int i = 0; i < n; ++i) scanf("%lld", &b[i]);
  std::sort(a, a + n);
  for (int i = 0; i < n; ++i) a[n + i] = a[i] + len;
  int64_t left = 0, right = len;
  int64_t ans = 0;
  while (right >= left) {
    int64_t mid = (left + right) / 2;
    if (check(mid)) {
      ans = mid;
      right = mid - 1;
    } else
      left = mid + 1;
  }
  printf("%lld\n", ans);
  return 0;
}
