#include <bits/stdc++.h>
using namespace std;
long long a[2003];
long long b[2003];
int main() {
  long long n, m;
  scanf("%lld %lld", &n, &m);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &b[i]);
  }
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  long long ans = 0x3f3f3f3f3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    long long x = (b[i] - a[1] + m) % m;
    int top = 2;
    int f = 1;
    for (int j = i + 1; j <= n; j++) {
      long long tmp = (a[top] + x) % m;
      if (tmp != b[j]) {
        f = 0;
      }
      top++;
    }
    for (int j = 1; j < i; j++) {
      long long tmp = (a[top] + x) % m;
      if (tmp != b[j]) {
        f = 0;
      }
      top++;
    }
    if (f == 1) ans = min(ans, x);
  }
  printf("%lld\n", ans);
  return 0;
}
