#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n), b(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  long long res = 1e9 + 2;
  for (long long i = 0; i < n; i++) {
    long long x = a[0] <= b[i] ? b[i] - a[0] : m - (a[0] - b[i]);
    bool ok = true;
    for (long long j = 1; j < n; j++) {
      long long next = (i + j) % n;
      if (((a[j] + x) % m) != b[next]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      res = min(res, x);
    }
  }
  cout << res << endl;
  return 0;
}
