#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b);
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int modulo(long long int a, long long int b) {
  long long int r = a % b;
  return r < 0 ? r + b : r;
}
unsigned long long int ncr(unsigned long long int n, unsigned long long int r) {
  if (r > n - r) r = n - r;
  unsigned long long int ans = 1;
  unsigned long long int i;
  for (i = 1; i <= r; i++) {
    ans *= n - r + i;
    ans /= i;
  }
  return ans;
}
class abc {
 public:
  long long int val;
  long long int ind;
};
bool fun1(abc x, abc y) { return x.val > y.val; }
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int modInverse(long long int n, long long int p) {
  return power(n, p - 2, p);
}
long long int fact[100000 + 2];
long long int ncrfast(long long int n, long long int r, long long int p) {
  if (r == 0) return 1;
  return (fact[n] * modInverse(fact[r], p) % p * modInverse(fact[n - r], p) %
          p) %
         p;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n >> m;
  long long int a[n];
  long long int b[n];
  long long int mina = 99999999999999;
  long long int minb = 99999999999999;
  long long int maxa = 0;
  long long int maxb = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<long long int> v;
  sort(a, a + n);
  sort(b, b + n);
  for (long long int i = 0; i < n; i++) {
    long long int x = b[0] - a[i];
    if (x < 0) x = x + m;
    long long int aa[n];
    for (long long int j = 0; j < n; j++) {
      aa[j] = (a[j] + x) % m;
    }
    sort(aa, aa + n);
    long long int flag = 0;
    for (long long int k = 0; k < n; k++) {
      if (aa[k] != b[k]) {
        flag = 1;
        break;
      }
    }
    if (flag == 0) v.push_back(x);
  }
  long long int ans = 9999999999999;
  for (long long int i = 0; i < v.size(); i++) {
    ans = min(ans, v[i]);
  }
  cout << ans << endl;
}
