#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, a[2005], b[2005], c[2005];
int check() {
  for (int i = 0; i < n; ++i)
    if (b[i] != c[i]) return 0;
  return 1;
}
int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  for (int i = 0; i < n; ++i) scanf("%d", &b[i]);
  sort(a, a + n);
  sort(b, b + n);
  int mn = 1e9;
  for (int i = 0; i < n; ++i) {
    int t = (b[i] - a[0] + m) % m;
    for (int j = 0; j < n; ++j) c[j] = (a[j] + t) % m;
    sort(c, c + n);
    if (check()) mn = min(mn, t);
  }
  printf("%d\n", mn);
}
