#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  long n, m;
  long a[3000], b[3000];
  long ha[3000], hb[3000];
  long x = 0, min = 1000000003;
  bool flag;
  for (int i = 0; i < 3000; i++) {
    a[i] = 0;
    b[i] = 0;
  }
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  sort(a, a + n);
  sort(b, b + n);
  for (int i = 0; i < n - 1; i++) {
    ha[i] = a[i + 1] - a[i];
    hb[i] = b[i + 1] - b[i];
  }
  ha[n - 1] = a[0] - a[n - 1];
  if (ha[n - 1] < 0) ha[n - 1] += m;
  hb[n - 1] = b[0] - b[n - 1];
  if (hb[n - 1] < 0) hb[n - 1] += m;
  for (int i = 0; i < n; i++) {
    flag = true;
    for (int j = 0; j < n; j++) {
      if (ha[j] != hb[(i + j) % n]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      x = b[i] - a[0];
      if (x < 0) x += m;
      if (x < min) min = x;
    }
  }
  cout << min << endl;
  return 0;
}
