#include <bits/stdc++.h>
bool FLAG = 1;
using namespace std;
const long long MOD = (long long)1e9 + 7, N = (long long)2e6 + 222;
const long long INF = (long long)1e18;
long long n, k, p[N], a[N], b[N], b1[N];
string s;
void clear() {
  for (long long i = 1; i <= n; ++i) {
    p[i] = a[i];
  }
}
long long m;
void solve() {
  cin >> n >> m;
  long long mx = 0;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }
  for (long long i = 1; i <= n; ++i) {
    cin >> b[i];
  }
  sort(b + 1, b + 1 + n);
  for (long long i = 1; i <= n; ++i) {
    b1[i] = b[i] + m;
  }
  long long mn = INF;
  clear();
  for (long long i = 1; i <= n; ++i) {
    long long d;
    if (b1[i] - mx >= 0) {
      d = b1[i] - mx;
      for (long long j = 1; j <= n; ++j) {
        p[j] += d;
        p[j] %= m;
      }
      sort(p + 1, p + 1 + n);
      bool ok = 0;
      for (long long j = 1; j <= n; ++j) {
        if (p[j] != b[j]) ok = 1;
      }
      if (ok == 0) {
        mn = min(mn, d);
      }
      clear();
    }
    if (b[i] - mx >= 0) {
      d = b[i] - mx;
      for (long long j = 1; j <= n; ++j) {
        p[j] += d;
        p[j] %= m;
      }
      sort(p + 1, p + 1 + n);
      bool ok = 0;
      for (long long j = 1; j <= n; ++j) {
        if (p[j] != b[j]) ok = 1;
      }
      if (ok == 0) {
        mn = min(mn, d);
      }
      clear();
    }
  }
  cout << mn;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
