#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  x = 0;
  bool f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch - 48);
    ch = getchar();
  }
  if (f) x = -x;
}
void read(long long &x) {
  x = 0;
  bool f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch - 48);
    ch = getchar();
  }
  if (f) x = -x;
}
int pos1[200000], pos2[600000];
bool used[200000];
int n, L;
bool check(int dis) {
  int left = 0, right = 3 * n - 1;
  for (int i = 0; i < n; i++) {
    while (pos1[i] - pos2[left] > dis) left++;
    while (pos2[right] - pos1[i] > dis) right--;
    left++;
    right++;
  }
  return left <= right;
}
int main() {
  read(n);
  read(L);
  for (int i = 0; i < n; i++) {
    read(pos1[i]);
  }
  for (int i = 0; i < n; i++) {
    read(pos2[i]);
    pos2[i + n] = pos2[i] - L;
    pos2[i + n * 2] = pos2[i] + L;
  }
  sort(pos1, pos1 + n);
  sort(pos2, pos2 + n * 3);
  int l = 0, r = L / 2 + 1;
  while (r - l > 1) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  if (check(l)) {
    cout << l << endl;
  } else {
    cout << r << endl;
  }
  return 0;
}
