#include <bits/stdc++.h>
using namespace std;
long long n, a[2005], b[2005], c[2005], m;
vector<long long> x;
int main() {
  cin >> n >> m;
  for (long i = long(1); i <= long(n); i++) cin >> a[i];
  for (long i = long(1); i <= long(n); i++) cin >> b[i];
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n);
  a[0] = -1;
  for (long i = long(1); i <= long(n); i++)
    if (a[i] != a[i - 1]) x.push_back((b[1] - a[i] + m) % m);
  sort(x.begin(), x.end());
  for (long i = long(0); i <= long(x.size()); i++) {
    for (long j = long(1); j <= long(n); j++) c[j] = (a[j] + x[i] + m) % m;
    sort(c + 1, c + 1 + n);
    for (long j = long(1); j <= long(n); j++)
      if (c[j] != b[j]) {
        goto en;
      }
    cout << x[i];
    return 0;
  en:
    continue;
  }
  return 0;
}
