#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char i = getchar();
  int f = 1, res = 0;
  while (i < '0' || i > '9') {
    if (i == '-') f = -1;
    i = getchar();
  }
  while (i >= '0' && i <= '9') {
    res = res * 10 + i - '0';
    i = getchar();
  }
  return res * f;
}
const int N = 1e6 + 50;
int n, m, a[N], b[N];
inline bool pd(int mid) {
  int l = 1, r = (n << 1) + n;
  for (register int i = 1; i <= n; ++i) {
    while (b[r] - a[i] > mid) --r;
    while (a[i] - b[l] > mid) ++l;
    if (r < l) return 0;
    ++l, ++r;
  }
  return 1;
}
int main() {
  n = read();
  m = read();
  for (register int i = 1; i <= n; ++i) a[i] = read();
  sort(a + 1, a + 1 + n);
  for (register int i = 1; i <= n; ++i) {
    b[i] = read();
    b[n + i] = b[i] + m;
    b[2 * n + i] = b[i] - m;
  }
  sort(b + 1, b + 1 + (n << 1) + n);
  int l = 0, r = m, mid, ans;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (pd(mid)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
