#include <bits/stdc++.h>
using namespace std;
long long fpow(long long a, long long b) {
  long long result = 1;
  while (b) {
    if (b & 1) {
      result = (a * result) % 1000000007;
    }
    a = (a * a) % 1000000007;
    b = b / 2;
  }
  return result;
}
int main() {
  long long n;
  long long m;
  cin >> n >> m;
  long long a[n], b[n], c[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  sort(b, b + n);
  long long ans = 1e18;
  for (int i = 0; i < n; i++) {
    long long f = 0;
    long long x = b[0] - a[i];
    if (x < 0) {
      x += m;
    }
    for (int i = 0; i < n; i++) {
      c[i] = (a[i] + x) % m;
    }
    sort(c, c + n);
    for (int i = 0; i < n; i++) {
      if (c[i] != b[i]) {
        f = 1;
        break;
      }
    }
    if (f == 0) {
      ans = min(ans, x);
    }
  }
  cout << ans << endl;
}
