#include <bits/stdc++.h>
using namespace std;
template <typename T>
T getint() {
  T val = 0;
  char c;
  bool neg = false;
  while ((c = getchar()) && !(c >= '0' && c <= '9')) {
    neg |= c == '-';
  }
  do {
    val = (val * 10) + c - '0';
  } while ((c = getchar()) && (c >= '0' && c <= '9'));
  return val * (neg ? -1 : 1);
}
long long n, m;
unordered_map<int, long long> m1, m2;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    ++m1[a];
  }
  int first;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (i == 0) first = a;
    ++m2[a];
  }
  int ans = m + 1;
  for (auto& e : m1) {
    bool ok = true;
    if (e.second != m2[first]) continue;
    int x;
    if (e.first > first)
      x = first + m - e.first;
    else
      x = first - e.first;
    for (auto e2 : m1) {
      if (e2.second != m2[(e2.first + x) % m]) ok = false;
    }
    if (ok) ans = min(ans, x);
  }
  cout << ans << '\n';
  return 0;
}
