#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  long long int m;
  cin >> n >> m;
  vector<long long int> a;
  for (int i = 0; i < n; i++) {
    a.push_back(0);
    cin >> a[i];
  }
  vector<long long int> b;
  for (int i = 0; i < n; i++) {
    b.push_back(0);
    cin >> b[i];
  }
  vector<vector<long long int> > athing;
  vector<vector<long long int> > bthing;
  vector<vector<long long int> > things;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    athing.push_back(vector<long long int>());
    bthing.push_back(vector<long long int>());
  }
  int p = 0;
  int x = 0;
  while (p < n) {
    x = 1;
    while (p + x < n && a[p] == a[p + x]) {
      x++;
    }
    athing[x - 1].push_back(a[p]);
    p += x;
  }
  p = 0;
  x = 0;
  while (p < n) {
    x = 1;
    while (p + x < n && b[p] == b[p + x]) {
      x++;
    }
    bthing[x - 1].push_back(b[p]);
    p += x;
  }
  for (int i = 0; i < n; i++) {
    if (athing[i].size() != 0 && bthing[i].size() != 0) {
      if (athing[i].size() == 1) {
        things.push_back(vector<long long int>());
        things.back().push_back((m + bthing[i][0] - athing[i][0]) % m);
      } else {
        things.push_back(vector<long long int>());
        long long int best = m;
        long long int thing = (m + bthing[i][0] - athing[i][0]) % m;
        bool good = true;
        for (int j = 0; j < athing[i].size(); j++) {
          athing[i].push_back(athing[i][0]);
          athing[i].erase(athing[i].begin());
          thing = (m + bthing[i][0] - athing[i][0]) % m;
          good = true;
          for (int k = 0; k < athing[i].size(); k++) {
            if ((athing[i][k] + thing) % m != bthing[i][k]) {
              good = false;
              break;
            }
          }
          if (good) {
            best = min(best, thing);
            things.back().push_back(thing);
          }
        }
      }
    }
  }
  map<long long int, long long int> mp;
  for (int i = 0; i < things.size(); i++) {
    for (int j = 0; j < things[i].size(); j++) {
      mp[things[i][j]]++;
    }
  }
  long long int small = m;
  for (auto x : mp) {
    if (x.second == things.size()) {
      small = min(small, x.first);
    }
  }
  cout << small << endl;
  cin.sync();
  cin.get();
  return 0;
}
