#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  while (t--) {
    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 diff, ans = INT_MAX;
    bool flg = true;
    for (long long i = 0; i < n; i++) {
      flg = true;
      diff = (b[i] - a[0] + m) % m;
      vector<long long> c(n);
      for (long long j = 0; j < n; j++) {
        c[j] = (a[j] + diff) % m;
      }
      sort(c.begin(), c.end());
      for (long long k = 0; k < n; k++) {
        if (c[k] != b[k]) {
          flg = false;
          break;
        }
      }
      if (flg) {
        ans = min(ans, diff);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
