#include <bits/stdc++.h>
using namespace std;
int n;
long long a[600010], b[600010];
int can(int x) {
  int L = -600010, R = 600010;
  for (int i = n; i < n * 2; i++) {
    int st = lower_bound(b, b + 2 * n, a[i] - x) - b;
    int nd = upper_bound(b + n, b + 3 * n, a[i] + x) - (b + n);
    L = max(L, st - i);
    R = min(R, nd + n - i);
  }
  return L < R;
}
int main() {
  int l;
  scanf("%d%d", &n, &l);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    a[i] = x;
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    b[i] = x;
  }
  sort(b, b + n);
  for (int i = n; i < 3 * n; i++) {
    a[i] = a[i - n] + l;
    b[i] = b[i - n] + l;
  }
  int f = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) f = 0;
  }
  if (f) {
    puts("0");
    return 0;
  }
  int lo = 0, hi = l / 2;
  while (lo + 1 < hi) {
    int mid = lo + (hi - lo) / 2;
    if (can(mid)) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  printf("%d\n", hi);
  return 0;
}
