#include <bits/stdc++.h>
const double PI = 3.1415926;
const int KL = 1e6;
using namespace std;
long long q, x, y, z, n, m, a[KL], b[KL], mn = 1e9, mx = -1e9, ans;
vector<long long> vec;
set<long long> s, t, e;
map<long long, bool> mp;
map<long long, long long> cnt, cn;
int main() {
  scanf("%lld", &n);
  scanf("%lld", &m);
  for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
  for (int i = 0; i < n; i++) scanf("%lld", &b[i]);
  for (int i = 0; i < n; i++) s.insert(a[i]);
  for (int i = 0; i < n; i++) {
    t.insert(b[i]);
    cnt[b[i]]++;
  }
  auto it = s.begin();
  while (it != s.end()) {
    x = *(it);
    auto ot = t.begin();
    while (ot != t.end()) {
      y = *(ot);
      if (x <= y) {
        z = y - x;
      } else {
        z = y - x + m;
      }
      if (!mp[z]) {
        vec.push_back(z);
        mp[z] = true;
      }
      ot++;
    }
    break;
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < vec.size(); i++) {
    x = vec[i];
    for (int j = 0; j < n; j++) {
      q = (a[j] + x) % m;
      e.insert(q);
      cn[q]++;
    }
    if (e.size() == t.size()) {
      auto ie = e.begin();
      auto ot = t.begin();
      bool ok = true;
      int k = 0;
      while (ie != e.end()) {
        y = *(ie);
        z = *(ot);
        if (y == z && cnt[z] == cn[y])
          k++;
        else {
          ok = false;
          break;
        }
        ie++;
        ot++;
      }
      if (ok) {
        cout << x;
        break;
      }
    }
    e.clear();
    cn.clear();
  }
  return 0;
}
