#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int main() {
  long long n, m, x;
  cin >> n >> m;
  std::vector<long long> a(n), b(n), temp(n);
  for (long long i = 0; i < n; i++) {
    cin >> x;
    a[i] = x;
  }
  for (long long i = 0; i < n; i++) {
    cin >> x;
    b[i] = x;
  }
  sort(b.begin(), b.end());
  set<long long> s;
  for (long long i = 0; i < n; i++) s.insert((b[0] - a[i] + m) % m);
  for (auto it : s) {
    int flag = 0;
    for (long long i = 0; i < n; i++) temp[i] = (a[i] + it) % m;
    sort(temp.begin(), temp.end());
    for (long long i = 0; i < n; i++) {
      if (temp[i] != b[i]) {
        flag = 1;
        break;
      }
    }
    if (flag == 0) {
      cout << it << endl;
      break;
    }
  }
  return 0;
}
