#include <bits/stdc++.h>
using namespace std;
struct re {
  int x, y;
};
const int inf = 1e9 + 10;
const int N = 2000 + 10;
int n, m, a[N], b[N], c[N], ans = inf;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) cin >> b[i];
  sort(b + 1, b + 1 + n);
  for (int i = 1; i <= n; ++i) {
    int tg = (b[i] - a[1] + m) % m;
    for (int i = 1; i <= n; ++i) c[i] = (a[i] + tg) % m;
    sort(c + 1, c + 1 + n);
    bool ok = true;
    for (int i = 1; i <= n; ++i)
      if (c[i] != b[i]) {
        ok = false;
        break;
      }
    if (ok) ans = min(ans, tg);
  }
  cout << ans;
  return 0;
}
