#include <bits/stdc++.h>
using namespace std;
int A[2005], B[2005], C[2005];
int FindMinimumValue(int N, int m) {
  sort(B + 1, B + N + 1);
  sort(A + 1, A + N + 1);
  int i, j, x, Ans = m;
  for (i = 1; i <= N; ++i) {
    if (A[i] != B[i]) break;
  }
  if (i == N + 1) return 0;
  for (i = 1; i <= N; ++i) {
    if (B[i] >= A[1])
      x = B[i] - A[1];
    else
      x = m - (A[1] - B[i]);
    for (j = 1; j <= N; ++j) C[j] = (A[j] + x) % m;
    sort(C + 1, C + N + 1);
    for (j = 1; j <= N; ++j) {
      if (B[j] != C[j]) break;
    }
    if (j == N + 1) Ans = min(Ans, x);
  }
  return Ans;
}
int main() {
  int N, m, i;
  scanf("%d%d", &N, &m);
  for (i = 1; i <= N; ++i) scanf("%d", A + i);
  for (i = 1; i <= N; ++i) scanf("%d", B + i);
  printf("%d\n", FindMinimumValue(N, m));
  return 0;
}
