#include <bits/stdc++.h>
using namespace std;
long long pow(long long a, long long b, long long m = 998244353) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
const long long N = 1000005;
long long modInv(long long a, long long m) {
  a = a % m;
  for (long long x = 1; x < m; x++)
    if ((a * x) % m == 1) return x;
}
long long modinv(long long a, long long m = 998244353) {
  return pow(a, m - 2, m);
}
long long dp[25][1005];
void solve() {
  long long n, m;
  cin >> n >> m;
  long long a[n], b[n];
  map<long long, long long> aa, bb;
  set<long long> s;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
    aa[a[i]]++;
    s.insert(a[i]);
  }
  for (long long i = 0; i < n; ++i) {
    cin >> b[i];
    bb[b[i]]++;
  }
  auto it = s.begin();
  long long cnt = 0;
  for (long long i = 0; i < aa.size(); ++i) {
    if (bb.find(*it) != bb.end() and aa[*it] == bb[*it]) {
      aa.erase(*it);
      bb.erase(*it);
      cnt++;
    }
    it++;
  }
  if (cnt == m) {
    cout << "0";
    return;
  }
  auto p = aa.begin();
  long long key = p->first;
  long long ele = p->second;
  long long ans = INT_MAX;
  for (auto x : bb) {
    if (x.second == ele) {
      multiset<long long> s1, s2;
      long long val = abs((m + x.first - key) % m);
      for (long long i = 0; i < n; ++i) {
        s1.insert((a[i] + val) % m);
        s2.insert((b[i]));
      }
      long long sum = 0;
      auto foo = s1.begin();
      auto foo1 = s2.begin();
      while (foo != s1.end()) {
        sum += abs(*foo - *foo1);
        foo++;
        foo1++;
      }
      if (sum == 0) {
        ans = min(ans, val);
      }
    }
  }
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
