#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, n, m, d, f, x, d1, ans = 1e9;
  cin >> n >> m;
  long long a[n], b[n];
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = 0; i < n; i++) cin >> b[i];
  sort(a, a + n);
  sort(b, b + n);
  for (i = 0; i < n; i++) {
    x = i;
    d = (b[0] - a[i % n] + m) % m;
    f = 1;
    for (j = 1; j < n; j++) {
      x++;
      d1 = (b[j] - a[x % n] + m) % m;
      if (d1 != d) {
        f = 0;
        break;
      }
    }
    if (f) ans = min(ans, d);
  }
  cout << ans << endl;
}
