#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9 + 9091203;
const long long MOD = 1e9 + 696969;
const long long INF = 1e18;
pair<int, int> tab[600100];
int n, T, m, c, DL = 0;
priority_queue<int, vector<int>, greater<int> > pq;
int L, N;
int h1[600100], h2[600100];
bool answer(int k) {
  T = 1;
  while (T--) {
    while (!pq.empty()) pq.pop();
    n = N;
    m = N;
    DL = 0;
    for (int i = 1; i <= m; ++i) {
      int where = h1[i - 1];
      int x = 0, y = N + N + N;
      while (x < y) {
        int sr = (x + y) / 2;
        if (where - h2[sr] > k)
          x = ++sr;
        else
          y = sr;
      }
      int a = x;
      x = 0, y = N + N + N;
      while (x < y) {
        int sr = (x + y) / 2 + 1;
        if (h2[sr] - where > k)
          y = --sr;
        else
          x = sr;
      }
      int b = x;
      if (a > b) return 0;
      if (b - a + 1 >= N) {
        a = 0;
        b = N - 1;
      }
      a %= N;
      b %= N;
      if (a <= b)
        tab[++DL] = make_pair(a, b), tab[++DL] = make_pair(a + n, b + n);
      else
        tab[++DL] = make_pair(a, b + n);
    }
    int akt = 0, wsk = 1;
    sort(tab + 1, tab + DL + 1);
    tab[DL + 1] = make_pair(inf, 0);
    while (akt != inf) {
      while (tab[wsk].first <= akt) pq.push(tab[wsk].second), ++wsk;
      if (pq.empty()) {
        akt = tab[wsk].first;
        continue;
      }
      if ((!pq.empty()) && pq.top() < akt) return 0;
      pq.pop();
      ++akt;
      if ((!pq.empty()) && pq.top() < akt) return 0;
    }
    return 1;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> N >> L;
  for (int i = 0; i < N; ++i) cin >> h1[i];
  sort(h1, h1 + N);
  for (int i = N; i < N + N; ++i) cin >> h2[i];
  sort(h2 + N, h2 + N + N);
  for (int i = 0; i < N; ++i) {
    h2[i + N + N] = h2[i + N] + L;
    h2[i] = h2[i + N] - L;
  }
  int xx = 0, yy = inf;
  while (xx < yy) {
    int mid = ((xx + yy) >> 1);
    if (!answer(mid))
      xx = ++mid;
    else
      yy = mid;
  }
  cout << xx;
}
