#include <bits/stdc++.h>
using namespace std;
int n, m, a[200010], b[200010 << 2];
int cgy;
bool check(int 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() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    b[n + i] = b[i] + m;
    b[2 * n + i] = b[i] - m;
  }
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + 3 * n);
  int l = 0, r = (m + 1) / 2, mid, ans;
  while (l < r) {
    mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid;
    else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
