#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  a = abs(a), b = abs(b);
  if (a < b) swap(a, b);
  return b ? gcd(b, a % b) : a;
}
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
vector<vector<int> > adj;
set<int> st;
int n, m, a[300000 + 5], b[300000 + 5], c[300000 + 5];
map<int, int> m1, m2;
vector<pair<int, int> > v1, v2;
int main(void) {
  ios::sync_with_stdio(false), cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    m1[a[i]] += 1;
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    m2[b[i]] += 1;
  }
  for (auto x : m1) v1.emplace_back(x.second, x.first);
  for (auto x : m2) v2.emplace_back(x.second, x.first);
  sort(v1.rbegin(), v1.rend());
  sort(v2.rbegin(), v2.rend());
  sort(b, b + n);
  int ans = (int)(1e9 + 5), x;
  for (int i = 0; i < (int)(v1).size(); ++i) {
    if (v1[0].first == v2[i].first) {
      x = (v2[i].second - v1[0].second + m) % m;
      for (int j = 0; j < n; ++j) c[j] = (a[j] + x) % m;
      sort(c, c + n);
      bool flg = true;
      for (int j = 0; j < n; ++j)
        if (c[j] != b[j]) {
          flg = false;
        }
      if (flg) ans = min(ans, x);
    }
  }
  cout << ans;
  return 0;
}
