#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
const long long mod = 1e9 + 7;
bool is(long long n) { return (ceil(log2(n)) == floor(log2(n))); }
bool q(const pair<long long, long long> &a,
       const pair<long long, long long> &b) {
  if (a.second == b.second) return a.first < b.first;
  return a.second < b.second;
}
bool q2(const pair<long long, long long> &a,
        const pair<long long, long long> &b) {
  if (a.first == b.first) return a.second > b.second;
  return a.first > b.first;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1, n, m;
  while (t--) {
    cin >> n >> m;
    long long a[n], b[n];
    map<long long, long long> m1, m2;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      m1[a[i]]++;
    }
    for (long long i = 0; i < n; i++) {
      cin >> b[i];
      m2[b[i]]++;
    }
    sort(a, a + n);
    sort(b, b + n);
    long long x1 = INT_MAX, x2 = INT_MAX;
    vector<long long> ans;
    for (long long j = 0; j < n; j++) {
      if (m1[a[0]] == m2[b[j]]) {
        if (a[0] <= b[j])
          x1 = b[j] - a[0] % m;
        else
          x1 = m + b[j] - a[0];
        ans.push_back(x1);
      }
    }
    sort(ans.begin(), ans.end());
    long long a2[n];
    for (auto x : ans) {
      bool first = true;
      for (int i = 0; i < n; i++) {
        a2[i] = (a[i] % m + x % m) % m;
      }
      sort(a2, a2 + n);
      for (int i = 0; i < n; i++) {
        if (a2[i] != b[i]) {
          first = false;
          break;
        }
      }
      if (first) {
        cout << x << endl;
        return 0;
      }
    }
    x1 = min(x1, x2);
    cout << x1 << endl;
  }
  return 0;
}
