#include <bits/stdc++.h>
using namespace std;
void solve() {
  long int n, m;
  cin >> n >> m;
  vector<long int> a(n), b(n), c(n), d(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) c[i] = a[i];
  for (int i = 0; i < n; i++) d[i] = b[i];
  long int temp = a[0] % m;
  long int x;
  for (int i = 0; i < n; i++) {
    for (int i = 0; i < n; i++) c[i] = a[i];
    d[i] %= m;
    if (d[i] >= temp)
      x = d[i] - temp;
    else
      x = (m - temp) + d[i];
    for (int i = 0; i < n; i++) {
      c[i] = (c[i] + x) % m;
    }
    sort(c.begin(), c.end());
    int flag = 1;
    for (int i = 0; i < n; i++) {
      if (c[i] != b[i]) flag = 0;
    }
    if (flag == 1) {
      cout << x << endl;
      return;
    }
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
