#include <bits/stdc++.h>
using namespace std;
const int N = 400010;
pair<int, int> c[N];
int n, L, a[N], b[N];
inline int check(int x) {
  if (x * 2 >= L) return 1;
  int t = 0;
  for (int i = 1; i <= n; i++) {
    int l = b[i] - x, r = b[i] + x;
    if (l < 0) l += L;
    if (r >= L) r -= L;
    if (l <= r)
      c[++t] = pair<int, int>(l, r), c[++t] = pair<int, int>(l + L, r + L);
    else
      c[++t] = pair<int, int>(l, r + L);
  }
  sort(c + 1, c + 1 + t);
  for (int i = 1, p = 1; i <= t; i++) {
    while (p <= n + n && a[p] < c[i].first) p++;
    if (p > n + n || a[p] > c[i].second) return 0;
    p++;
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &L);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) a[i + n] = a[i] + L;
  sort(a + 1, a + 1 + n + n);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  int l = 0, r = L, mid, ans;
  while (l <= r) check(mid = l + r >> 1) ? r = (ans = mid) - 1 : l = mid + 1;
  printf("%d\n", ans);
  return 0;
}
