#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 5;
void solve() {
  long long int n, m, i;
  cin >> n >> m;
  vector<long long int> a(n), b(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    cin >> b[i];
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  long long int ans = LLONG_MAX;
  for (i = 0; i < n; i++) {
    long long int x;
    if (b[i] >= a[0]) {
      x = b[i] - a[0];
    } else {
      x = m - a[0] + b[i];
    }
    vector<long long int> c(n);
    for (long long int j = 0; j < n; j++) {
      c[j] = (a[j] + x) % m;
    }
    sort(c.begin(), c.end());
    if (b == c) {
      ans = min(ans, x);
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  long long int t = 1;
  while (t--) {
    solve();
  }
}
