#include <bits/stdc++.h>
using namespace std;
int i, n, x, m, a[3000], c[3000], b[3000];
int main() {
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    cin >> b[i];
  }
  sort(a, a + n);
  sort(b, b + n);
  int f, ans = 1e9 + 1, tempx;
  for (int i = 0; i < n; i++) {
    if (a[0] == b[i]) continue;
    if (a[0] < b[i])
      x = b[i] - a[0];
    else
      x = b[i] - a[0] + m;
    for (int j = 0; j < n; j++) c[j] = (a[j] + x) % m;
    sort(c, c + n);
    f = 0;
    for (int j = 0; j < n; j++)
      if (c[j] != b[j]) {
        f = 1;
        break;
      }
    if (f == 0) ans = min(ans, x);
  }
  if (ans == 1e9 + 1)
    cout << 0;
  else
    cout << ans;
  return 0;
}
