#include <bits/stdc++.h>
using namespace std;
int v[2005], w[2005];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> v[i];
  for (int i = 1; i <= n; i++) cin >> w[i];
  sort(v + 1, v + n + 1);
  sort(w + 1, w + n + 1);
  int minX = m;
  for (int i = 1; i <= n; i++) {
    int x = ((long long)w[i] - v[1] + m) % m;
    if (x > minX) continue;
    vector<int> newV;
    for (int j = 1; j <= n; j++) newV.push_back(((long long)v[j] + x) % m);
    sort(newV.begin(), newV.end());
    bool valid = true;
    for (int j = 0; j < n; j++)
      if (newV[j] != w[j + 1]) valid = false;
    if (valid) minX = min(x, minX);
  }
  cout << minX;
  return 0;
}
