#include <bits/stdc++.h>
using namespace std;
bool equals(vector<int> &s1, vector<int> &s2) {
  sort(s1.begin(), s1.end());
  sort(s2.begin(), s2.end());
  for (int i = 0; i < s1.size(); i++)
    if (s1[i] != s2[i]) return false;
  return true;
}
int main() {
  int n, m, a[2003], b[2003];
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) scanf("%d", &b[i]);
  vector<int> s1, s2;
  for (int i = 0; i < n; i++) {
    s1.push_back((m + a[i] - a[0]) % m);
  }
  int MIN = 1e9 + 1;
  for (int i = 0; i < n; i++) {
    int p = b[i];
    s2.clear();
    for (int j = 0; j < n; j++) {
      s2.push_back((m + b[j] - b[i]) % m);
    }
    if (equals(s1, s2)) {
      MIN = min(MIN, (m + b[i] - a[0]) % m);
    }
  }
  printf("%d", MIN);
  return 0;
}
