#include <bits/stdc++.h>
using namespace std;
const int maxn = 400020;
const int maxm = 1000020;
const int MOd = 1e9 + 7;
const int K = 43;
int a, L;
int ar[maxn];
int bs[maxn];
bool f(int n) {
  vector<pair<int, int> > v;
  for (int i = 1; i <= a; i++) {
    int l = bs[i] - n;
    int r = bs[i] + n;
    if (l < 0) l += L, r += L;
    v.push_back(pair<int, int>(l, r));
  }
  sort(v.begin(), v.end());
  int g = a;
  for (int i = 0, l = 1, r = 0, last = 0, fir = 0; i < v.size(); i++) {
    while (ar[l] < v[i].first && l <= a) l++;
    while (ar[r + 1] <= v[i].second && r + 1 <= a + a) r++;
    if (l > r) return false;
    if (i == 0) {
      last = fir = r;
      g = last - l;
    } else {
      last++;
      if (last < l) return false;
      while (last > r && g > 0) last--, fir--, g--;
      if (last > r) return false;
      g = min(g, (last - l));
    }
  }
  return true;
}
int main() {
  scanf("%d %d", &a, &L);
  for (int i = 1; i <= a; i++) scanf("%d", &ar[i]);
  for (int i = 1; i <= a; i++) scanf("%d", &bs[i]);
  sort(ar + 1, ar + 1 + a);
  for (int i = 1; i <= a; i++) ar[i + a] = ar[i] + L;
  int t = -1;
  for (int i = 29; i >= 0; i--)
    if (t + (1 << i) <= (L + 1) / 2 && !f(t + (1 << i))) t += 1 << i;
  t++;
  cout << t << endl;
  return 0;
}
