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