#include <bits/stdc++.h>
using namespace std;
const int maxm = 2e5 + 10;
const int INF = 0x7fffffff;
int A[maxm], B[maxm << 2], n, L;
bool check(int x) {
  int L = -INF, R = INF;
  for (int i = 0; i < n; ++i) {
    int tl = lower_bound(B + 1, B + 1 + 3 * n, A[i] - x) - B - i;
    int tr = upper_bound(B + 1, B + 1 + 3 * n, A[i] + x) - B - i - 1;
    L = max(L, tl);
    R = min(R, tr);
  }
  return L <= R;
}
int main() {
  scanf("%d%d", &n, &L);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  for (int i = n; i < 2 * n; ++i) {
    scanf("%d", &B[i]);
  }
  sort(A, A + n), sort(B + n, B + 2 * n);
  for (int i = 0; i < n; ++i) B[i] = B[i + n] - L;
  for (int i = 2 * n - 1; i < 3 * n; ++i) B[i] = B[i - n] + L;
  int l = 0, r = L - 1, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}
