#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, n, m, i, j, k = 0, l, x, y, z, ans = INT_MAX, ans1, p = 0,
                               q = 0, r;
  cin >> n >> m;
  long long int a[n];
  vector<long long int> b(n);
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = 0; i < n; i++) cin >> b[i];
  sort(b.begin(), b.end());
  for (i = 0; i < n; i++) {
    if (b[0] - a[i] >= 0)
      x = b[0] - a[i];
    else
      x = m + b[0] - a[i];
    vector<long long int> v(n);
    for (j = 0; j < n; j++) {
      v[j] = (a[j] + x) % m;
    }
    sort(v.begin(), v.end());
    if (v == b) ans = min(ans, x);
  }
  cout << ans;
}
