#include <bits/stdc++.h>
using namespace std;
long long int t, test, temp;
long long int n, m, k, kount;
long long int a, b, c, ans, ara[2020], bra[2020], cra[2020];
long long int x, y, z;
int main() {
  while (scanf("%lld %lld", &n, &m) == 2) {
    for (long long int i = 0; i < n; i++) scanf("%lld", &ara[i]);
    for (long long int i = 0; i < n; i++) scanf("%lld", &bra[i]);
    sort(bra, bra + n);
    ans = LONG_LONG_MAX;
    for (long long int i = 0; i < n; i++) {
      x = bra[i] - ara[0];
      if (x < 0)
        x += m;
      else
        x %= m;
      for (long long int j = 0; j < n; j++) cra[j] = (ara[j] + x) % m;
      sort(cra, cra + n);
      bool ok = true;
      for (long long int j = 0; j < n; j++)
        if (cra[j] != bra[j]) ok = false;
      if (ok) ans = min(ans, x);
    }
    printf("%lld\n", ans);
  }
}
