#include <bits/stdc++.h>
using namespace std;
const int maxN = 200005;
const int INF = 1e9;
long long arr[maxN * 3];
long long brr[maxN];
bool check(int n, long long L, long long d) {
  int l = 0, r = 3 * n;
  while (true) {
    int m = (l + r) / 2;
    int res = 0;
    for (int i = 0; i < n; i++) {
      if (arr[m + i] < brr[i] - d) {
        res = -1;
        break;
      }
      if (arr[m + i] > brr[i] + d) {
        res = 1;
        break;
      }
    }
    if (res == 0) return 1;
    if (r - l == 1) return 0;
    if (res == -1)
      l = m;
    else
      r = m;
  }
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n;
  long long L;
  cin >> n >> L;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) cin >> brr[i];
  sort(arr, arr + n);
  sort(brr, brr + n);
  for (int i = 0; i < n; i++) arr[i] -= L;
  for (int i = 0; i < n; i++) {
    arr[n + i] = arr[i] + L;
    arr[2 * n + i] = arr[n + i] + L;
  }
  long long l = -1, r = L;
  while (r - l > 1) {
    long long m = (l + r) / 2;
    if (check(n, L, m))
      r = m;
    else
      l = m;
  }
  cout << r << endl;
  return 0;
}
