#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long debt[N], lend[N];
void each_case(int t) {
  int n, m, i, j, k, tp;
  cin >> n >> m;
  int a[n], b[n];
  for (i = 0; i <= n - 1; i++) cin >> a[i];
  for (i = 0; i <= n - 1; i++) cin >> b[i];
  sort(a, a + n);
  sort(b, b + n);
  int mn = 1e9 + 5;
  for (i = 0; i <= n - 1; i++) {
    int eq = (b[0] - a[i] + m) % m;
    k = (i + 1) % n;
    bool f = 1;
    for (j = 1; j <= n - 1; j++) {
      if (eq != (b[j] - a[k] + m) % m) {
        f = 0;
        break;
      }
      k = (k + 1) % n;
    }
    if (f) mn = min(mn, eq);
  }
  cout << mn << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1, i;
  for (i = 1; i <= t; i++) each_case(i);
  return 0;
}
