#include <bits/stdc++.h>
using namespace std;
const long long int inf = 9e18;
const long double pi = 2 * acos(0.0);
long long int t, n, x = inf, m;
vector<long long int> arr, brr, temp;
void solve() {
  cin >> n >> m;
  arr.resize(n), brr.resize(n);
  ;
  for (int i = 0; i < n; i++) cin >> arr[i];
  ;
  for (int i = 0; i < n; i++) cin >> brr[i];
  sort(arr.begin(), arr.end());
  sort(brr.begin(), brr.end());
  ;
  for (int i = 0; i < n; i++) {
    bool valid = true;
    long long int curr = brr[0] - arr[i], ind = 1;
    if (curr < 0) curr += m;
    temp.clear();
    ;
    for (int j = 0; j < n; j++) {
      temp.push_back((arr[j] + curr) % m);
    }
    sort(temp.begin(), temp.end());
    ;
    for (int j = 0; j < n; j++) {
      if (temp[j] != brr[j]) valid = false;
    }
    if (valid) x = min(x, curr);
  }
  cout << x << "\n";
}
int32_t main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  t = 1;
  while (t--) {
    solve();
  }
}
