#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T qmin(const T &x, const T &y) {
  return x < y ? x : y;
}
template <typename T>
inline T qmax(const T &x, const T &y) {
  return x > y ? x : y;
}
template <typename T>
inline void getmin(T &x, const T &y) {
  if (y < x) x = y;
}
template <typename T>
inline void getmax(T &x, const T &y) {
  if (y > x) x = y;
}
inline void fileio(const string &s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
const int inf = (int)1e9 + 7;
const long long linf = (long long)1e18 + 7;
const int N = 4e5 + 7;
int a[N], b[N];
int n, l;
inline bool check(int len) {
  static int ul[N], ur[N];
  int lst1 = 1, lst2 = 0;
  for (register int i = (1); i <= (n << 1); ++i) {
    while (lst1 <= (n << 1) && b[lst1] < a[i] - len) ++lst1;
    while (lst2 < (n << 1) && b[lst2 + 1] <= a[i] + len) ++lst2;
    ul[i] = lst1 - i, ur[i] = lst2 - i;
  }
  deque<int> q1, q2;
  for (register int i = (1); i <= (n << 1); ++i) {
    while (!q1.empty() && ul[i] >= ul[q1.back()]) q1.pop_back();
    q1.push_back(i);
    while (!q2.empty() && ur[i] <= ur[q2.back()]) q2.pop_back();
    q2.push_back(i);
    if (i < n) continue;
    while (!q1.empty() && q1.front() <= i - n) q1.pop_front();
    while (!q2.empty() && q2.front() <= i - n) q2.pop_front();
    if (ul[q1.front()] <= ur[q2.front()]) return true;
  }
  return false;
}
int main() {
  scanf("%d%d", &n, &l);
  for (register int i = (1); i <= (n); ++i)
    scanf("%d", a + i), a[i + n] = a[i] + l;
  for (register int i = (1); i <= (n); ++i)
    scanf("%d", b + i), b[i + n] = b[i] + l;
  sort(a + 1, a + (n << 1) + 1);
  sort(b + 1, b + (n << 1) + 1);
  int ul = -1, ur = l >> 1;
  while (ur - ul > 1) {
    int mid = ul + ur >> 1;
    if (check(mid))
      ur = mid;
    else
      ul = mid;
  }
  printf("%d\n", ur);
  return 0;
}
