#include <bits/stdc++.h>
using namespace std;
long long ara[2003];
long long ara2[2003];
unordered_map<long long, long long> m1;
unordered_map<long long, long long> m2;
unordered_map<long long, long long>::iterator it;
int main() {
  long long n, m;
  scanf("%lld", &n);
  scanf("%lld", &m);
  for (long long i = 0; i < n; i++) scanf("%lld", &ara[i]);
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &ara2[i]);
    long long x = ara2[i];
    m1[x]++;
  }
  long long ans = 9000000000000000;
  for (long long i = 0; i < n; i++) {
    long long a = (ara2[i] - ara[0] + m) % m;
    for (long long j = 0; j < n; j++) m2[(ara[j] + a) % m]++;
    long long flag = 0;
    for (it = m2.begin(); it != m2.end(); it++) {
      if (it->second != m1[it->first]) {
        flag = 1;
        break;
      }
    }
    if (!flag) ans = min(a, ans);
    m2.clear();
  }
  cout << ans;
}
