#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  int ans = 1e9;
  for (int i = 0; i < n; i++) {
    int x;
    if (b[i] >= a[0]) {
      x = b[i] - a[0];
    } else {
      x = m + b[i] - a[0];
    }
    map<int, int> mp;
    for (int j = 0; j < n; j++) {
      mp[b[j]]++;
    }
    for (int j = 0; j < n; j++) {
      mp[(a[j] + x) % m]--;
    }
    int flag = 1;
    for (auto x : mp) {
      flag = flag & (x.second == 0);
    }
    if (flag) {
      ans = min(ans, x);
    }
  }
  cout << ans << endl;
}
int main() {
  int t = 1;
  for (int i = 0; i < t; i++) {
    solve();
  }
}
