#include <bits/stdc++.h>
using namespace std;
int n, k, x;
vector<int> va, vb;
map<int, int> ma, mb, tmmp;
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> x;
    va.push_back(x);
    ma[x]++;
  }
  for (int i = 0; i < n; i++) {
    cin >> x;
    vb.push_back(x);
    mb[x]++;
  }
  if (ma == mb) {
    cout << 0 << endl;
    exit(0);
  }
  int ans = INT_MAX;
  for (int i = 0; i < n; i++) {
    tmmp.clear();
    int cnt;
    if (vb[i] >= va[0])
      cnt = vb[i] - va[0];
    else
      cnt = k - va[0] + vb[i];
    for (int j = 0; j < n; j++) {
      int lol = va[j] + cnt;
      lol %= k;
      tmmp[lol]++;
    }
    if (tmmp == mb) ans = min(ans, cnt);
  }
  cout << ans << endl;
}
