#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, L;
  cin >> n >> L;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  vector<int> B;
  for (int x : b) B.push_back(x - L);
  for (int x : b) B.push_back(x);
  for (int x : b) B.push_back(x + L);
  int l = -1;
  int r = L;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    int border1 = -n;
    int border2 = n;
    for (int i = 0; i < n; i++) {
      while (border1 <= n && a[i] - B[i + border1 + n] > m) border1++;
      while (border2 >= -n && B[i + border2 + n] - a[i] > m) border2--;
    }
    if (border1 <= border2)
      r = m;
    else
      l = m;
  }
  cout << r << '\n';
}
