#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long md = 1000000007) {
  long long res = 1;
  x %= md;
  while (y > 0) {
    if (y & 1) res = (res * x) % md;
    x = (x * x) % md;
    y = y >> 1;
  }
  return res % md;
}
bool check(vector<long long> a, vector<long long> b) {
  for (long long i = 0; i < a.size(); i++) {
    if (a[i] != b[i]) return false;
  }
  return true;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  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(a.begin(), a.end());
  sort(b.begin(), b.end());
  long long x, ans = INT_MAX;
  vector<long long> temp(n);
  for (long long i = 0; i < n; i++) {
    if (a[i] > b[0])
      x = m - a[i] + b[0];
    else
      x = b[0] - a[i];
    for (long long j = 0; j < n; j++) temp[j] = (a[j] + x) % m;
    sort(temp.begin(), temp.end());
    if (check(temp, b)) ans = min(ans, x);
  }
  cout << ans << "\n";
}
