#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 1e9 + 10;
template <typename T>
T mod(T n, T p, T m) {
  n %= m;
  T res = 1;
  while (p > 0) {
    if (p & 1) res = ((res % m) * (n % m)) % m;
    n = ((n % m) * (n % m)) % m;
    p >>= 1;
  }
  return res;
}
template <typename T>
T phi(T n) {
  long long int result = n;
  for (long long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n /= i;
      result -= result / i;
    }
  }
  if (n > 1) result -= result / n;
  return result;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n >> m;
  long long int a[n], b[n], c[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  sort(b, b + n);
  long long int ans = maxn;
  for (int i = 0; i < n; i++) {
    long long int x = (b[i] - a[0] + m) % m;
    for (int j = 0; j < n; j++) c[j] = (a[j] + x) % m;
    sort(c, c + n);
    bool ok = 1;
    for (int j = 0; j < n; j++) {
      if (c[j] != b[j]) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      ans = min(x, ans);
    }
  }
  cout << ans << endl;
  return 0;
}
