#include <bits/stdc++.h>
using namespace std;
int a[2011], b[2011], temp[2011];
int n, m;
void input() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  sort(b, b + n + 1);
  sort(a, a + n + 1);
}
bool check(int x) {
  for (int i = 1; i <= n; i++) temp[i] = (a[i] + x) % m;
  sort(temp, temp + n + 1);
  for (int i = 1; i <= n; i++)
    if (b[i] != temp[i]) return false;
  return true;
}
int Giang_Bae() {
  int t = INT_MAX;
  int res = INT_MAX;
  for (int i = 1; i <= n; i++) {
    t = (m + b[1] - a[i]) % m;
    if (check(t) == true) res = min(res, t);
  }
  return res;
}
int main() {
  input();
  cout << Giang_Bae();
}
