#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, ad;
  cin >> n >> m;
  int A[n], B[n], C[n], res = m + 1;
  for (i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (i = 0; i < n; i++) {
    cin >> B[i];
  }
  sort(B, B + n);
  for (i = 0; i < n; i++) {
    if (B[i] >= A[0]) {
      ad = B[i] - A[0];
    } else {
      ad = B[i] - A[0] + m;
    }
    for (j = 0; j < n; j++) {
      C[j] = (A[j] + ad) % m;
    }
    sort(C, C + n);
    for (j = 0; j < n; j++) {
      if (C[j] != B[j]) {
        break;
      }
    }
    if (j == n) {
      res = min(res, ad);
    }
  }
  cout << res << endl;
  return 0;
}
