#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    ;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  int ans = 2e10;
  for (int i = 0; i < n; i++) {
    int mn = 2e9, mx = 0;
    for (int j = 0; j < n; j++) {
      int x = (b[(j + i) % n] - a[j] + m) % m;
      mn = min(mn, x);
      mx = max(mx, x);
    }
    if (mn == mx) {
      ans = min(ans, mn);
    }
  }
  cout << ans;
  return 0;
}
