#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n, 0), b(n, 0);
  for (int i = 0; i < n; i++) {
    int aux;
    scanf("%d", &aux);
    a[i] = aux;
  }
  for (int i = 0; i < n; i++) {
    int aux;
    scanf("%d", &aux);
    b[i] = aux;
  }
  sort((a).begin(), (a).end());
  sort((b).begin(), (b).end());
  int ans = 1000000000;
  for (int i = 0; i < n; i++) {
    int x = b[0] - a[i];
    x = (x + m) % m;
    bool deuruim = false;
    for (int d = 0; d < n; d++) {
      int j = (i + d) % n;
      if ((a[j] + x) % m != b[d]) {
        deuruim = true;
        break;
      }
    }
    if (!deuruim) {
      ans = min(ans, x);
    }
  }
  printf("%d\n", ans);
  return 0;
}
