#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  sort(begin(b), end(b));
  int ans = m;
  for (int i = 0; i < n; ++i) {
    int x = (b[0] - a[i] + m) % m;
    vector<int> tmp(n);
    for (int j = 0; j < n; ++j) {
      tmp[j] = (a[j] + x) % m;
    }
    sort(begin(tmp), end(tmp));
    if (tmp == b) {
      ans = min(ans, x);
    }
  }
  cout << ans << "\n";
  return 0;
}
