#include <bits/stdc++.h>
using namespace std;
int finder(int toreserve[], int k, int p) {
  int pos = 1;
  while (pos < p) {
    if (toreserve[2 * pos] >= k) {
      pos = 2 * pos;
    } else {
      k -= toreserve[2 * pos];
      pos = 2 * pos + 1;
    }
  }
  return pos - p;
}
int query(int tree[], int l, int r) {
  int ans = 0;
  while (l < r) {
    if (l % 2) {
      ans += tree[l];
      l++;
    }
    if (r % 2) {
      r--;
      ans += tree[r];
    }
    l /= 2;
    r /= 2;
  }
  return ans;
}
void update(int tree[], int pos, int val) {
  while (pos > 0) {
    tree[pos] += val;
    pos /= 2;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int i, j, tt = 1;
  while (tt--) {
    long long int temp, n, m, diff, flag = 0, ans = 1e9 + 1;
    cin >> n >> m;
    long long int a[n], b[n];
    map<long long int, long long int> m1, m2;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      m1[a[i]]++;
    }
    for (i = 0; i < n; i++) {
      cin >> b[i];
      m2[b[i]]++;
    }
    sort(a, a + n);
    sort(b, b + n);
    temp = -1;
    for (i = 0; i < n; i++) {
      if (temp == b[i])
        continue;
      else {
        temp = b[i];
        diff = (b[i] - a[0] + m) % m;
      }
      flag = 0;
      for (auto x : m1) {
        if (m2.find((x.first + diff) % m) == m2.end()) {
          flag = 1;
          break;
        }
        if (m2[(x.first + diff) % m] != m1[x.first]) {
          flag = 1;
          break;
        }
      }
      if (flag == 0) {
        ans = min(ans, diff);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
