#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline bool chkmin(T1 &first, const T2 &second) {
  if (first > second) {
    first = second;
    return 1ll;
  } else {
    return 0ll;
  }
}
template <typename T1, typename T2>
inline bool chkmax(T1 &first, const T2 &second) {
  if (first < second) {
    first = second;
    return 1ll;
  } else {
    return 0ll;
  }
}
istream &operator>>(istream &insert, vector<long long> &a) {
  for (long long &now : a) {
    insert >> now;
  }
  return insert;
}
ostream &operator<<(ostream &out, vector<long long> &a) {
  for (long long &now : a) {
    out << now << ' ';
  }
  out << "\n";
  return out;
}
long long n, mod;
long long solve(long long st, vector<long long> a, vector<long long> b) {
  long long res = b[0] - st;
  for (long long &now : a) {
    now = (now + res + mod) % mod;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  return a == b;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> mod;
  vector<long long> a(n), b(n);
  cin >> a >> b;
  long long ans = 1e18;
  for (long long &now : a) {
    if (solve(now, a, b)) {
      chkmin(ans, (b[0] - now + mod) % mod);
    }
  }
  cout << ans;
}
