#include <bits/stdc++.h>
using namespace std;
const int NMAX = 2e3 + 5;
int N, M, A[NMAX], B[NMAX], C[NMAX];
int Min = 0;
static inline void Read() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> M;
  for (int i = 1; i <= N; ++i) cin >> A[i];
  for (int i = 1; i <= N; ++i) cin >> B[i];
  return;
}
static inline void Sort() {
  sort(A + 1, A + N + 1);
  sort(B + 1, B + N + 1);
  return;
}
static inline void Easy_Case() {
  for (int i = 1; i <= N; ++i)
    if (A[i] != B[i]) return;
  cout << 0 << '\n';
  exit(0);
  return;
}
static inline void Check(int D) {
  for (int i = 1; i <= N; ++i) C[i] = (A[i] + D) % M;
  int pos = N;
  for (int i = 1; i < N; ++i)
    if (C[i] > C[i + 1]) {
      pos = i;
      break;
    }
  int j = 0;
  for (int i = pos + 1; i <= N; ++i)
    if (C[i] == B[++j])
      continue;
    else
      return;
  for (int i = 1; i <= pos; ++i)
    if (C[i] == B[++j])
      continue;
    else
      return;
  Min = min(Min, D);
  return;
}
static inline void Solve() {
  Min = 1e9;
  for (int i = 1; i <= N; ++i) {
    int D = B[1] - A[i];
    if (D < 0) D += M;
    Check(D);
  }
  cout << Min << '\n';
  return;
}
int main() {
  Read();
  Sort();
  Easy_Case();
  Solve();
  return 0;
}
