#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
const int INF = 2e9 + 5;
int n, m, a[N], b[N], ans = INF;
int Dif(int p, int q) {
  if (p <= q) {
    return (q - p);
  } else {
    return (q + m - p);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
  }
  sort(b + 1, b + n + 1);
  for (int sta = 1; sta <= n; ++sta) {
    int i = sta + 1;
    if (i == n + 1) i = 1;
    int should = Dif(a[sta], b[1]);
    bool ok = 1;
    for (int j = 2; j <= n; ++j) {
      if (Dif(a[i], b[j]) != should) {
        ok = 0;
        break;
      }
      ++i;
      if (i == n + 1) i = 1;
    }
    if (ok) {
      ans = min(ans, should);
    }
  }
  cout << ans;
  return 0;
}
