#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];
  long long int ans = INT_MAX;
  sort(b, b + n);
  for (long long int i = 0; i < n; i++) {
    long long int val = (m + b[i] - a[0]) % m;
    long long int c[n];
    bool poss = true;
    for (long long int j = 0; j < n; j++) c[j] = (a[j] + val) % m;
    sort(c, c + n);
    for (long long int j = 0; j < n; j++) {
      if (c[j] != b[j]) poss = false;
    }
    if (poss) {
      ans = min(ans, val);
    }
  }
  cout << ans << "\n";
  return 0;
}
