#include <bits/stdc++.h>
using namespace std;
int a[400000], b[200000];
int c[400000];
int main() {
  int i;
  int n, L;
  scanf("%d %d", &n, &L);
  for (i = 0; i < n; i++) scanf("%d", &a[i]), a[i + n] = a[i] + L;
  for (i = 0; i < n; i++) scanf("%d", &b[i]);
  sort(a, a + 2 * n);
  sort(b, b + n);
  int l = 0, r = L / 2;
  while (l < r) {
    int m = (l + r) / 2;
    int x = 0;
    for (i = 0; i < n; i++) {
      if (b[i] - m < 0)
        c[x++] = b[i] + L;
      else if (b[i] + m >= L)
        c[x++] = b[i];
      else
        c[x++] = b[i], c[x++] = b[i] + L;
    }
    sort(c, c + x);
    int y = 0;
    for (i = 0; i < x; i++) {
      while ((y < 2 * n) && (abs(c[i] - a[y]) > m)) y++;
      if (y == 2 * n)
        break;
      else
        y++;
    }
    if (i == x)
      r = m;
    else
      l = m + 1;
  }
  printf("%d\n", l);
  return 0;
}
