#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, ans = INT_MAX;
  ;
  cin >> n >> m;
  int a[n], b[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  multiset<int> sb;
  for (int i = 0; i < n; i++) cin >> b[i], sb.insert(b[i]);
  for (int j = 0; j < n; j++) {
    int x = (b[0] - a[j] + m) % m;
    multiset<int> sa;
    for (int i = 0; i < n; i++) sa.insert((a[i] + x) % m);
    if (sa == sb) ans = min(ans, x);
  }
  cout << ans;
}
