#include <bits/stdc++.h>
using LL = long long;
using namespace std;
const int N = 200000, T = N * 6 + 20;
int n, t;
LL ln, a[T], b[T];
int main() {
  scanf("%d%lld", &n, &ln);
  for (int i = 0; i < n; i++) scanf("%lld", a + i);
  for (int i = 0; i < n; i++) scanf("%lld", b + i);
  sort(a, a + n);
  sort(b, b + n);
  t = n;
  for (int r = 0; r < 5; r++) {
    for (int i = 0; i < n; i++) {
      a[t] = a[i] + ln * (r + 1);
      b[t] = b[i] + ln * (r + 1);
      t++;
    }
  }
  LL low = 0, high = ln - 1;
  while (low < high) {
    LL mi = (low + high) >> 1;
    int j1 = 0, j2 = 0, found = 0, max_diff = (int)-1e9;
    for (int i = 2 * n; i < 4 * n; i++) {
      while (b[j1] < a[i] - mi) j1++;
      while (b[j2] <= a[i] + mi) j2++;
      max_diff = max(max_diff, j1 - i);
      if (j2 - i - 1 < max_diff) {
        found = 1;
        break;
      }
    }
    if (found)
      low = mi + 1;
    else
      high = mi;
  }
  printf("%lld\n", low);
}
