#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<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];
  sort(b.begin(), b.end());
  long long ans = INT_MAX;
  for (long long i = 0; i < n; i++) {
    long long x = (b[i] - a[0] + m) % m;
    vector<long long> c(n);
    for (long long j = 0; j < n; j++) c[j] = (a[j] + x + m) % m;
    sort(c.begin(), c.end());
    if (b == c) ans = min(ans, x);
  }
  cout << ans;
}
