#include <bits/stdc++.h>
using namespace std;
template <typename... T>
void see(T&... args) {
  ((cin >> args), ...);
}
template <typename... T>
void put(T&&... args) {
  ((cout << args << " "), ...);
}
template <typename... T>
void putl(T&&... args) {
  ((cout << args << " "), ...);
  cout << '\n';
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << "=" << a << ", ";
  err(++it, args...);
}
const long long inf = INT_MAX;
const long double ep = 0.0000001;
const long double pi = acos(-1.0);
const long long md = 1000000007;
void solve() {
  long long n, m;
  see(n, m);
  long long a[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  };
  long long b[n];
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
  };
  map<long long, long long> mp, mp2;
  for (long long i = 0; i < n; ++i) {
    mp[b[i]]++;
    mp2[a[i]]++;
  }
  vector<long long> v;
  long long x = mp.begin()->second, y = mp.begin()->first;
  for (auto [i, j] : mp2) {
    if (j == x) v.push_back(i);
  }
  long long mn = inf;
  for (auto k : v) {
    long long z = (y - k) % m;
    while (z < 0) z += m;
    map<long long, long long> mp3;
    for (long long i = 0; i < n; ++i) {
      mp3[(a[i] + z) % m]++;
    }
    long long f = 0;
    for (auto [i, j] : mp3) {
      if (mp.find(i) == mp.end() || mp[i] != j) {
        f = 1;
        break;
      }
    }
    if (f == 0) mn = min(mn, z);
  }
  put(mn);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  while (t--) {
    solve();
  }
}
