#include <bits/stdc++.h>
using namespace std;
bool check(vector<int> &a, vector<int> &b) {
  for (int i = 0; i < a.size(); i++)
    if (a[i] != b[i]) return false;
  return true;
}
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<int> b(n);
  vector<int> t(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 res = INT_MAX;
  for (int i = 0; i < n; i++) {
    t = a;
    int acc = b[0] - a[0];
    if (acc < 0) acc += m;
    for (int i = 0; i < n; i++) t[i] = (t[i] + acc) % m;
    sort(t.begin(), t.end());
    if (check(b, t)) res = min(res, acc);
    int temp = a.back();
    a.pop_back();
    a.insert(a.begin(), temp);
  }
  cout << res << endl;
  return 0;
}
