#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int s[maxn * 2], b[maxn * 2], a[maxn * 2], n, len;
int check(int x) {
  int top, i, j;
  top = 0;
  for (i = 1; i <= n; i++) {
    if (b[i] >= x) s[++top] = b[i];
  }
  for (i = 1; i <= n; i++) {
    if (b[i] + x < len) s[++top] = b[i] + len;
  }
  i = 1;
  j = 1;
  while (i <= top) {
    while (j <= 2 * n && abs(a[j] - s[i]) > x) ++j;
    if (j == 2 * n + 1) break;
    ++i;
    ++j;
  }
  return (i == (top + 1));
}
int main() {
  int i, l, r, mid, ans;
  scanf("%d%d", &n, &len);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
  }
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  for (i = 1; i <= n; i++) {
    a[i + n] = a[i] + len;
  }
  l = 0;
  r = (len) / 2;
  while (l <= r) {
    mid = (l + r) / 2;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
