#include <bits/stdc++.h>
using namespace std;
const int mxN = 2e5;
int n, l, a[mxN], b[mxN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> l;
  for (int i = 0; i < n; ++i) cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n; ++i) cin >> b[i];
  sort(b, b + n);
  int lb = 0, rb = l / 2 - 1;
  while (lb <= rb) {
    int mb = (lb + rb) / 2, k = n - 1;
    bool ok = 1;
    for (int i = 0; i < n && ok; ++i) {
      int c = (a[i] - mb + l) % l, p = lower_bound(b, b + n, c) - b;
      if (p >= n) p = 0;
      ok = min(abs(a[i] - b[p]), l - abs(a[i] - b[p])) <= mb;
    }
    if (ok) {
      int i = 0, j = 0;
      while (j + 1 < n && (min(abs(a[i] - b[(i - j + n) % n]),
                               l - abs(a[i] - b[(i - j + n) % n])) > mb ||
                           min(abs(a[i] - b[(i - j - 1 + n) % n]),
                               l - abs(a[i] - b[(i - j - 1 + n) % n])) <= mb))
        ++j;
      for (; i < n; ++i, ++j) {
        while (j >= n || min(abs(a[i] - b[(i - j + n) % n]),
                             l - abs(a[i] - b[(i - j + n) % n])) > mb)
          --j;
        k = min(j, k);
      }
      ok = 1;
      for (int i = 0; i < n && ok; ++i)
        ok = min(abs(a[i] - b[(i - k + n) % n]),
                 l - abs(a[i] - b[(i - k + n) % n])) <= mb;
    }
    if (ok)
      rb = mb - 1;
    else
      lb = mb + 1;
  }
  cout << lb;
}
