#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(NULL);
  long long n, m;
  cin >> n >> m;
  long long a[n], b[n];
  for (int j = 0; j < n; j++) cin >> a[j];
  for (int j = 0; j < n; j++) cin >> b[j];
  bool flag = 0;
  long long ans = INT_MAX;
  while (flag == 0) {
    for (int i = 0; i < n; i++) {
      long long c[n], d[n];
      for (int j = 0; j < n; j++) {
        c[j] = a[j];
        d[j] = b[j];
      }
      long long start = b[0], end = a[i];
      while (start < end) {
        start = start + m;
      }
      long long x = start - end;
      for (int j = 0; j < n; j++) c[j] = (c[j] + x) % m;
      sort(c, c + n);
      sort(d, d + n);
      int k;
      for (k = 0; k < n; k++) {
        if (c[k] == d[k])
          continue;
        else
          break;
      }
      if (k == n) {
        ans = min(ans, x);
      }
    }
    flag = 1;
  }
  cout << ans;
  return 0;
}
