#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")
#pragma GCC optimize("03")
using namespace std;
int n, l;
vector<long long> a;
vector<long long> b;
bool kk(long long x, long long y, long long d) {
  x %= l;
  y %= l;
  return min(abs(x - y), l - abs(x - y)) <= d;
}
int eval(int p, long long x) {
  for (int i = n; i < 2 * n; i++) {
    int ap = p + i - n;
    if (!kk(a[i], b[ap], x)) {
      if (a[i] - a[i - 1] > b[ap] - b[ap - 1])
        return -1;
      else
        return 1;
    }
  }
  return 0;
}
bool ok(long long x) {
  vector<int> pos;
  int p = n;
  while (p < 2 * n && kk(a[n], b[p], x)) {
    pos.push_back(p);
    p++;
  }
  p = n - 1;
  while (p >= 0 && kk(a[n], b[p], x)) {
    pos.push_back(p);
    p--;
  }
  sort((pos).begin(), (pos).end());
  if (pos.empty()) return 0;
  long long act = -1;
  for (int p = (1 << 18); p; p >>= 1) {
    if (act + p >= pos.size()) continue;
    long long v = eval(pos[act + p], x);
    if (!v) return 1;
    if (v == -1) act += p;
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> l;
  a.resize(3 * n);
  b.resize(3 * n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i + n] = a[i] + l;
    a[i + 2 * n] = a[i] + 2 * l;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[i + n] = b[i] + l;
    b[i + 2 * n] = b[i] + 2 * l;
  }
  sort((a).begin(), (a).end());
  sort((b).begin(), (b).end());
  long long res = -1;
  for (int po = (1 << 29); po; po >>= 1)
    if (!ok(res + po)) res += po;
  res++;
  cout << (res) << '\n';
  return 0;
}
