#include <bits/stdc++.h>
using namespace std;
const long long MOD2 = 998244353;
const long long MOD = 1e9 + 7;
const long long MAX = 1e5 + 7;
const long long inf = 1e18L + 5;
const double pi = 3.14159265358979323846;
long long fpow(long long n, long long m) {
  long long res = 1;
  while (m > 0) {
    if (m & 1) {
      res *= n;
    }
    m /= 2;
    n *= n;
  }
  return res;
}
const long long nax = 1e5 + 5;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long a[n], b[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n; i++) cin >> b[i];
  sort(a, a + n);
  sort(b, b + n);
  long long x, ans = 1e10;
  for (long long i = 0; i < n; i++) {
    if (a[0] <= b[i])
      x = b[i] - a[0];
    else
      x = m - a[0] + b[i];
    for (long long j = 0; j < n; j++) {
      if ((a[j] + x) % m != b[(j + i) % n]) {
        break;
      }
      if (j == n - 1) {
        ans = min(ans, x);
      }
    }
  }
  if (ans == 1e10)
    cout << 0;
  else
    cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
