#include <bits/stdc++.h>
long long int n, m;
void merging(long long arr[], long long start, long long mid, long long end);
void sort(long long arr[], long long start, long long end) {
  long long mid;
  if (end - start + 1 > 1) {
    mid = (start + end) / 2;
    sort(arr, start, mid);
    sort(arr, mid + 1, end);
    merging(arr, start, mid, end);
  }
}
void merging(long long arr[], long long start, long long mid, long long end) {
  long long len1, len2, i, j, k;
  len1 = mid - start + 1;
  len2 = end - mid - 1 + 1;
  long long right[len2], left[len1];
  for (i = 0; i < len1; i++) {
    left[i] = arr[i + start];
  }
  for (i = 0; i < len2; i++) {
    right[i] = arr[i + mid + 1];
  }
  i = 0;
  j = 0;
  k = start;
  while ((i < len1) && (j < len2)) {
    if (left[i] > right[j]) {
      arr[k] = right[j];
      j++;
      k++;
    } else {
      arr[k] = left[i];
      i++;
      k++;
    }
  }
  if (i == len1) {
    while (j < len2) {
      arr[k] = right[j];
      j++;
      k++;
    }
  } else {
    while (i < len1) {
      arr[k] = left[i];
      i++;
      k++;
    }
  }
}
int main() {
  scanf("%I64lld %I64lld", &n, &m);
  long long a[2005], b[2005], dup[2005];
  for (int i = 0; i < n; i++) {
    scanf("%I64lld", &b[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%I64lld", &a[i]);
    dup[i] = b[i];
  }
  sort(a, 0, n - 1);
  long long int d, l = 0, fin[2005];
  for (int i = 0; i < n; i++) {
    d = a[0] - b[i];
    if (d < 0) d = d + m;
    for (int j = 0; j < n; j++) {
      dup[j] = (dup[j] + d) % m;
    }
    sort(dup, 0, n - 1);
    int flag = 1;
    for (int j = 0; j < n; j++) {
      if (dup[j] != a[j]) {
        flag = -1;
        break;
      }
    }
    if (flag == 1) {
      fin[l] = d;
      l++;
    }
    for (int j = 0; j < n; j++) {
      dup[j] = b[j];
    }
  }
  long long int min = fin[0];
  for (int i = 0; i < l; i++) {
    if (fin[i] < min) min = fin[i];
  }
  printf("%I64lld\n", min);
  return 0;
}
