#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long INF = 1e18;
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long nCr(long long n, long long r) {
  long long res = 1;
  if (n < r) return -1;
  r = min(r, n - r);
  for (long long i = 0; i < r; i++) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}
long long nPr(long long n, long long r) {
  long long res = 1;
  if (n < r) return -1;
  for (long long i = 0; i < r; i++) res *= (n - i);
  return res;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<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(b.begin(), b.end());
  long long ans = INF;
  for (long long i = 0; i < n; i++) {
    long long x = (b[i] - a[0] + m) % m;
    vector<long long> v(n);
    for (long long j = 0; j < n; j++) v[j] = (a[j] + x) % m;
    sort(v.begin(), v.end());
    if (v == b) ans = min(ans, x);
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) solve();
  return 0;
}
