#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long inf = 1e12;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long flag = 0, flag2 = 0;
  map<long long, long long> mm;
  set<long long> s;
  vector<long long> v;
  set<long long>::iterator itr;
  long long n, m;
  cin >> n >> m;
  long long a[n], b[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
  }
  long long j = 0;
  sort(a, a + n);
  sort(b, b + n);
  long long x = 0;
  long long ans = inf;
  for (long long j = 0; j < n; j++) {
    flag2 = 0;
    map<long long, long long> xx;
    for (long long i = 0; i < n; i++) {
      x = (b[(i + j) % n] - a[i] + m) % m;
      xx[x]++;
    }
    if (xx.size() == 1) {
      auto itr = xx.begin();
      ans = min(ans, itr->first);
    }
  }
  cout << ans << endl;
}
