#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> ar, br;
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    ar.push_back(y);
  }
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    br.push_back(y);
  }
  sort(br.begin(), br.end());
  int ans = m;
  for (int i = 0; i < n; i++) {
    int x = (br[i] - ar[0] + m) % m;
    vector<int> v;
    for (int j = 0; j < n; j++) {
      int p = (ar[j] + x) % m;
      v.push_back(p);
    }
    sort(v.begin(), v.end());
    if (v == br) ans = min(ans, x);
  }
  cout << ans << endl;
}
