#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char *argv[]) {
  int n, m;
  scanf("%d %d", &n, &m);
  int a[n], b[n];
  map<int, int> mp_a;
  map<int, int> mp_b;
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    mp_b[b[i]]++;
  }
  sort(a, a + n);
  int k = a[0];
  long long ans = 2000000000000000 + 5;
  for (int i = 0; i < n; i++) {
    int x;
    if (b[i] == k)
      x = 0;
    else if (b[i] > k)
      x = b[i] - k;
    else
      x = m - k + b[i];
    for (int j = 0; j < n; j++) {
      mp_a[(a[j] + x) % m]++;
    }
    bool f = true;
    for (auto &it : mp_a) {
      if (mp_b.find(it.first) != mp_b.end() && mp_b[it.first] == it.second)
        continue;
      else
        f = false;
    }
    if (f) ans = min(ans, (long long)x);
    mp_a.clear();
  }
  printf("%lld\n", ans);
  return 0;
}
