#include <bits/stdc++.h>
using namespace std;
long long INF = 1e9 + 7;
int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  ;
  cin >> n;
  ;
  long long m;
  ;
  cin >> m;
  ;
  long long arr[n];
  for (long long i = 0; i < n; i++) {
    ;
    cin >> arr[i];
    ;
  }
  long long arr2[n];
  set<long long> s1, s2;
  for (long long i = 0; i < n; i++) {
    ;
    cin >> arr2[i];
    ;
  }
  long long ans = INF;
  sort(arr, arr + n);
  sort(arr2, arr2 + n);
  for (long long i = 0; i < n; i++) {
    long long diff = (arr2[i] + m - arr[0]) % m;
    bool ok = 1;
    for (long long j = 0; j < n; j++) {
      if ((arr[j] + diff) % m != arr2[(i + j) % n]) {
        ok = 0;
      }
    }
    if (ok) {
      ans = min(ans, diff);
    }
  }
  cout << ans << endl;
  ;
}
