#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n);
  vector<long long> 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(b.begin(), b.end());
  long long ans = m - 1;
  for (long long i = 0; i < n; i++) {
    long long z;
    if (b[i] > a[0])
      z = (b[i] - a[0]) % m;
    else
      z = (m + b[i] - a[0]) % m;
    vector<long long> temp;
    temp = a;
    for (long long i = 0; i < n; i++) temp[i] = (temp[i] + z) % m;
    sort(temp.begin(), temp.end());
    if (temp == b) ans = min(ans, z);
  }
  cout << ans << endl;
  return 0;
}
