#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m;
  cin >> n >> m;
  long long int a[n], b[n];
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long int i = 0; i < n; i++) {
    cin >> b[i];
  }
  sort(a, a + n);
  sort(b, b + n);
  vector<long long int> v;
  long long int ans = 9e18;
  for (long long int i = 0; i < n; i++) {
    long long int val = b[0] - a[i] + m;
    val %= m;
    for (long long int j = 0; j < n; j++) {
      v.push_back((a[j] + val) % m);
    }
    sort(v.begin(), v.end());
    long long int ok = 1;
    for (long long int j = 0; j < n; j++) {
      if (v[j] != b[j]) {
        ok = 0;
        break;
      }
    }
    v.clear();
    if (ok) {
      ans = min(ans, val);
    }
  }
  cout << ans << endl;
  return 0;
}
