#include <bits/stdc++.h>
using namespace std;
int main() {
  int n = 0, m = 0;
  cin >> n >> m;
  vector<int> A(n);
  vector<int> B(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> B[i];
  }
  vector<int> AA = A;
  vector<int> BB = B;
  sort(BB.begin(), BB.end());
  if (AA == BB) {
    cout << 0;
    return 0;
  }
  vector<int> ans;
  vector<int> temp(n);
  int x;
  for (int i = 0; i < n; i++) {
    x = (BB[0] - A[i] + m) % m;
    for (int j = 0; j < n; j++) {
      temp[j] = (A[j] + x) % m;
    }
    sort(temp.begin(), temp.end());
    if (temp == BB) {
      ans.push_back(x);
    }
  }
  sort(ans.begin(), ans.end());
  cout << ans[0] << endl;
  return 0;
}
