#include <bits/stdc++.h>
int n;
long long m;
long long a[200005], b[200005 * 3];
using namespace std;
template <class T>
void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x *= f;
}
bool check(long long x) {
  int l = 1, r = 3 * n;
  for (int i = 1; i <= n; i++) {
    while (a[i] - b[l] > x) l++;
    while (b[r] - a[i] > x) r--;
    l++;
    r++;
  }
  return l <= r;
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++) read(a[i]);
  for (int i = 1; i <= n; i++) {
    read(b[i]);
    b[n + i] = b[i] + m;
    b[2 * n + i] = b[i] - m;
  }
  sort(a + 1, a + n + 1);
  sort(b + 1, b + 3 * n + 1);
  long long l = 0, r = (m + 1) / 2, ans;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%lld", ans);
  return 0;
}
