#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000100;
int N, L;
long long A[MAXN], B[MAXN];
int bsearch(long long k) {
  int lo = 0, hi = 3 * N - 1;
  while (lo < hi) {
    int mid = (lo + hi + 1) / 2;
    if (k < B[mid]) {
      hi = mid - 1;
    } else
      lo = mid;
  }
  return lo;
}
pair<int, int> get_offset(int cloc, int x) {
  long long lo = A[cloc] - x, hi = A[cloc] + x;
  int nlo = bsearch(lo - 1), nhi = bsearch(hi);
  nlo++;
  return make_pair(nlo - cloc, nhi - cloc);
}
bool works(int x) {
  int offset_lo = -1e9, offset_hi = 1e9;
  for (int i = 0; i < N; i++) {
    pair<int, int> offset_range = get_offset(i, x);
    offset_lo = max(offset_lo, offset_range.first);
    offset_hi = min(offset_hi, offset_range.second);
  }
  return offset_lo <= offset_hi;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> N >> L;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 0; i < N; i++) cin >> B[i];
  for (int i = N; i < 3 * N; i++) B[i] = B[i - N] + L;
  for (int i = 0; i < N; i++) A[i] += L;
  sort(A, A + N);
  sort(B, B + 3 * N);
  int lo = 0, hi = L / 2;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (works(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << "\n";
}
