#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int m, n;
  cin >> n >> m;
  long long int a[n], b[n];
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long int i = 0; i < n; i++) {
    cin >> b[i];
  }
  long long int min = 1000000007;
  sort(a, a + n);
  sort(b, b + n);
  bool ans = true;
  long long int temp[n];
  for (long long int i = 0; i < n; i++) {
    ans = true;
    long long int inc;
    if (b[0] - a[i] >= 0)
      inc = b[0] - a[i];
    else
      inc = (m + b[0] - a[i]);
    for (long long int j = 0; j < n; j++) {
      temp[j] = (a[j] + inc) % m;
    }
    sort(temp, temp + n);
    for (long long int j = 0; j < n; j++) {
      if (b[j] != temp[j]) {
        ans = false;
        break;
      }
    }
    if (ans == true) {
      if (min > inc) {
        min = inc;
      }
    }
  }
  cout << min;
}
