#include <bits/stdc++.h>
using namespace std;
void Solve() {
  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(b.begin(), b.end());
  int ans = m;
  for (int i = 0; i < n; i++) {
    int x = (b[i] - a[0] + m) % m;
    vector<int> c(n);
    for (int j = 0; j < n; j++) c[j] = (a[j] + x) % m;
    sort(c.begin(), c.end());
    if (c == b) ans = min(ans, x);
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    Solve();
  }
  return 0;
}
