#include <bits/stdc++.h>
const long long N = 1e6 + 10;
const long long mod = 1e9 + 7;
long long dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
using namespace std;
long long n, m;
long long a[N], b[N];
bool ok(long long x) {
  long long l = 1, r = 3 * n;
  for (long long i = 1; i <= n; i++) {
    while (a[i] - b[l] > x) l++;
    while (b[r] - a[i] > x) r--;
    l++, r++;
  }
  if (l <= r) return 1;
  return 0;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    b[i] = x - m;
    b[i + n] = x;
    b[i + n + n] = x + m;
  }
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + 3 * n);
  long long l = 0, r = m, ans = 0;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (ok(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
