#include <bits/stdc++.h>
using namespace std;
int n, L;
vector<long long> a, b;
int main() {
  scanf("%d %d", &n, &L);
  a.resize(n);
  b.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", &b[i]);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int rep = 0; rep < 5; rep++) {
    for (int i = 0; i < n; i++) {
      a.push_back(a[i] + 1LL * (rep + 1) * L);
      b.push_back(b[i] + 1LL * (rep + 1) * L);
    }
  }
  int low = 0, high = L - 1, ans = L - 1;
  while (low <= high) {
    int mid = (low + high) >> 1;
    int j1 = 0, j2 = 0;
    int max_diff = -1e9;
    bool miscount = false;
    for (int i = 2 * n; i < 4 * n; i++) {
      while (b[j1] < a[i] - mid) {
        j1++;
      }
      while (b[j2] <= a[i] + mid) {
        j2++;
      }
      max_diff = max(max_diff, j1 - i);
      if (j2 - i - 1 < max_diff) {
        miscount = true;
        break;
      }
    }
    if (miscount) {
      low = mid + 1;
    } else {
      ans = mid;
      high = mid - 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
