#include <bits/stdc++.h>
using namespace std;
template <typename T>
void dbg(T a) {}
template <typename T, typename... Arg>
void dbg(T a, Arg... arg) {}
const int maxn = (1e6) + 7;
const int inf = (1e9) + 7;
const long long LLinf = (1e18) + 7;
const long double eps = 1e-9;
const long long mod = 1e9 + 7;
vector<long long> wek[maxn];
long long n;
long long dwa[maxn];
long long tab[maxn];
void dodaj(int l, int p) {
  while (l > n && p > n) {
    l -= n;
    p -= n;
  }
  if (l <= n && p <= n) {
    wek[l].push_back(p);
    wek[l + n].push_back(p + n);
  } else
    wek[l].push_back(p);
}
void czysc() {
  for (int i = 0; i <= 3 * n; i++) wek[i].clear();
}
bool czy(long long val) {
  long long l = 1;
  long long p = 1;
  czysc();
  for (int i = n + 1; i <= 2 * n; i++) {
    while (dwa[l] < tab[i] - val) l++;
    while (p < l + n - 1 && dwa[p + 1] <= tab[i] + val) p++;
    if (abs(dwa[l] - tab[i]) > val || abs(dwa[p] - tab[i]) > val) return 0;
    dodaj(l, p);
  }
  priority_queue<long long> q;
  for (int i = 1; i <= 2 * n; i++) {
    for (auto s : wek[i]) q.push(-s);
    if (((int)(q).size())) {
      int a = -q.top();
      q.pop();
      if (a < i) return 0;
    }
  }
  if (((int)(q).size()) > 0) return 0;
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long dl;
  cin >> n >> dl;
  for (int i = 1; i <= n; i++) {
    cin >> tab[i];
    tab[i + n] = tab[i] + dl;
    tab[i + 2 * n] = tab[i] + 2 * dl;
  }
  sort(tab + 1, tab + n + 1 + n + n);
  for (int i = 1; i <= n; i++) {
    cin >> dwa[i];
    dwa[i + n] = dwa[i] + dl;
    dwa[i + 2 * n] = dwa[i] + 2 * dl;
    dwa[i + 3 * n] = dwa[i] + 3 * dl;
  }
  sort(dwa + 1, dwa + n + 1 + n + n + n);
  int l = -1;
  int p = dl;
  while (l + 1 < p) {
    int sr = (l + p) / 2;
    if (czy(sr) == 1)
      p = sr;
    else
      l = sr;
  }
  cout << p;
  return 0;
}
