#include <bits/stdc++.h>
using namespace std;
const int limit = 2000000;
int absolut(int x) {
  if (x < 0) return -x;
  return x;
}
int n, L;
void prepare(int v[limit]) {
  for (int i = 0; i < n; i++) cin >> v[i];
  sort(v, v + n);
  for (int i = n; i < 5 * n; i++) v[i] = v[i - n] + L;
}
int a[limit];
int b[limit];
bool can(int dist) {
  int j0 = 0;
  while (a[n] - b[j0] > dist) j0++;
  if (b[j0] - a[n] > dist) return false;
  int j1 = j0;
  while (b[j1 + 1] - a[n] <= dist) j1++;
  int j = j0;
  for (int i = n + 1; i < 3 * n; i++) {
    while (j0 <= j1 and a[i] - b[j0] > dist) j0++;
    if (j0 == j1 + 1 and absolut(a[i] - b[j0]) > dist) return false;
    while (b[j1 + 1] - a[i] <= dist) j1++;
    if (j < j0)
      j = j0;
    else
      j++;
    if (j1 < j) return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> L;
  prepare(a);
  prepare(b);
  int inf = -1;
  int sup = L / 2;
  while (inf + 1 < sup) {
    int med = (inf + sup) / 2;
    if (can(med))
      sup = med;
    else
      inf = med;
  }
  cout << sup << endl;
}
