#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 5;
int read() {
  int s = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    s = s * 10 + c - '0';
    c = getchar();
  }
  return s * f;
}
int n, m;
long long a[N], b[N];
bool Check(int len) {
  int Max = -inf, l = 1, r = 1;
  for (int i = 1; i <= n * 2; i++) {
    while (l < n * 4 && b[l] < a[i] - len) l++;
    while (r < n * 4 && b[r + 1] <= a[i] + len) r++;
    Max = max(Max, l - i);
    if (r - i < Max) return false;
  }
  return true;
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) b[i] = read();
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    a[i] += m;
    a[i + n] = a[i] + m;
  }
  for (int i = 1; i <= n * 3; i++) b[i + n] = b[i] + m;
  int l = 0, r = m / 2, ans = inf;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (Check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
