#include <bits/stdc++.h>
long long a[2005];
long long b[2005];
long long c[2005];
long long d[2000];
void merge(long long arr[], int l, int m, int r) {
  int i, j, k;
  int n1 = m - l + 1;
  int n2 = r - m;
  int L[n1], R[n2];
  for (i = 0; i < n1; i++) L[i] = arr[l + i];
  for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
  i = 0;
  j = 0;
  k = l;
  while (i < n1 && j < n2) {
    if (L[i] <= R[j]) {
      arr[k] = L[i];
      i++;
    } else {
      arr[k] = R[j];
      j++;
    }
    k++;
  }
  while (i < n1) {
    arr[k] = L[i];
    i++;
    k++;
  }
  while (j < n2) {
    arr[k] = R[j];
    j++;
    k++;
  }
}
void mergeSort(long long arr[], int l, int r) {
  if (l < r) {
    int m = l + (r - l) / 2;
    mergeSort(arr, l, m);
    mergeSort(arr, m + 1, r);
    merge(arr, l, m, r);
  }
}
int main() {
  long long count, n, q, i, j, k, x, y, z, l, ans, min, f, len, m, r, t, max;
  scanf("%lld %lld", &n, &m);
  for (i = 0; i < n; i++) scanf("%lld", &a[i]);
  for (i = 0; i < n; i++) scanf("%lld", &b[i]);
  mergeSort(a, 0, n - 1);
  mergeSort(b, 0, n - 1);
  x = 0;
  for (i = 0; i < n; i++) {
    if (a[i] == b[i]) x = x + 1;
  }
  if (x == n) {
    printf("0\n");
    return 0;
  }
  z = 0;
  for (i = 0; i < n; i++) {
    if (b[0] < a[i]) {
      for (j = 0; j < n; j++) {
        c[j] = (a[j] + m + b[0] - a[i]) % m;
      }
    }
    if (b[0] > a[i]) {
      for (j = 0; j < n; j++) {
        c[j] = (a[j] + b[0] - a[i]) % m;
      }
    }
    mergeSort(c, 0, n - 1);
    x = 0;
    for (k = 0; k < n; k++) {
      if (c[k] == b[k]) x = x + 1;
    }
    if (x == n) {
      if (b[0] > a[i]) {
        d[z] = b[0] - a[i];
        z++;
      }
      if (b[0] < a[i]) {
        d[z] = m + b[0] - a[i];
        z++;
      }
    }
  }
  min = 1000000000000;
  for (i = 0; i < z; i++) {
    if (d[i] < min && d[i] <= 1e9) min = d[i];
  }
  printf("%lld\n", min);
  return 0;
}
