#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int arr[n], brr[n], crr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) cin >> brr[i];
  sort(brr, brr + n);
  int x;
  int cnt, mn = 2000000000;
  for (int i = 0; i < n; i++) {
    cnt = 0;
    x = (m + brr[i] - arr[0]) % m;
    for (int j = 0; j < n; j++) {
      crr[j] = (arr[j] + x) % m;
    }
    sort(crr, crr + n);
    for (int k = 0; k < n; k++) {
      if (crr[k] == brr[k]) cnt++;
    }
    if (cnt == n) {
      mn = min(mn, x);
    }
  }
  cout << mn;
}
