#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool smin(T &a, const T &b) {
  return a > b ? a = b : a;
}
template <typename T>
inline bool smax(T &a, const T &b) {
  return a < b ? a = b : a;
}
const long long N = (long long)1e6 + 6, mod = (long long)0;
long long n, len, a[N], b[N], sum[N];
long long check(long long lim) {
  memset(sum, 0, sizeof sum);
  for (long long j = 0; j < n; ++j) {
    long long xl = a[j] + len - lim, xr = a[j] + len + lim + 1;
    xl = lower_bound(b, b + n + n + n, xl) - b;
    xr = lower_bound(b, b + n + n + n, xr) - b;
    xl -= j;
    xr -= j;
    xl += n;
    xr += n;
    xr = min(xr, xl + n);
    sum[xr]--;
    sum[xl]++;
  }
  for (long long j = 1; j < N; ++j) sum[j] += sum[j - 1];
  for (long long j = n; j < N; ++j) sum[j] += sum[j - n];
  for (long long j = 0; j < N; ++j) {
    if (sum[j] > n) assert(0);
  }
  for (long long j = 0; j < N; ++j) {
    if (sum[j] == n) return 1;
  }
  return 0;
}
int32_t main() {
  cin >> n >> len;
  for (long long j = 0; j < n; ++j) cin >> a[j];
  for (long long j = 0; j < n; ++j) cin >> b[j];
  sort(a, a + n);
  sort(b, b + n);
  for (long long j = n; j < n + n + n; ++j) b[j] = b[j - n] + len;
  long long bl = -1, br = len;
  while (bl < br - 1) {
    long long bm = bl + br >> 1;
    if (check(bm)) {
      br = bm;
    } else {
      bl = bm;
    }
  }
  cout << br << endl;
}
