#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int KINF = 0x3f3f3f3f;
const int N = 2010;
int a[N], b[N], c[N];
int n, m;
bool check(int x) {
  for (int i = (int)0; i < (int)n; ++i) c[i] = (a[i] + x) % m;
  sort(c, c + n);
  for (int i = (int)0; i < (int)n; ++i)
    if (c[i] != b[i]) return false;
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = (int)0; i < (int)n; ++i) cin >> a[i];
  for (int i = (int)0; i < (int)n; ++i) cin >> b[i];
  int ans = KINF;
  sort(b, b + n);
  for (int i = (int)0; i < (int)n; ++i) {
    int x = (b[0] - a[i] + m) % m;
    if (check(x)) ans = min(ans, x);
  }
  cout << ans << endl;
  return 0;
}
