#include <bits/stdc++.h>
using namespace std;
long long INF = 1 << 28;
const double pi = acos(-1.0);
int fx[] = {1, -1, 0, 0};
int fy[] = {0, 0, 1, -1};
int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
int knight[8][2] = {1, 2, 1, -2, 2, 1, 2, -1, -1, 2, -1, -2, -2, 1, -2, -1};
const long double EPS = 1e-7;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
bool cmp(int a, int b) { return a < b; }
int on(int mask, int st) { return mask | (1 << st); }
int off(int mask, int st) { return mask & (~(1 << st)); }
const int mx = 500010;
long long mod = 1000000007;
long long arr[mx];
long long brr[mx];
vector<long long> sizeA[mx], sizeB[mx];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &arr[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", &brr[i]);
  }
  sort(arr, arr + n);
  sort(brr, brr + n);
  int last = arr[0];
  int countt = 1;
  for (int i = 1; i < n; i++) {
    if (last != arr[i]) {
      sizeA[countt].push_back(last);
      countt = 1;
      last = arr[i];
    } else
      countt++;
  }
  sizeA[countt].push_back(last);
  countt = 1;
  last = brr[0];
  for (int i = 1; i < n; i++) {
    if (last != brr[i]) {
      sizeB[countt].push_back(last);
      countt = 1;
      last = brr[i];
    } else
      countt++;
  }
  sizeB[countt].push_back(last);
  int l = -1;
  for (int i = 1; i <= n; i++) {
    if (sizeA[i].size()) {
      l = i;
      break;
    }
  }
  long long x = sizeA[l][0];
  vector<long long> v;
  long long ans = INT_MAX;
  for (int i = 0; i < sizeB[l].size(); i++) {
    long long y = sizeB[l][i];
    long long add = (y - x + m) % m;
    v.clear();
    for (int j = 0; j < n; j++) {
      v.push_back((arr[j] + add) % m);
    }
    sort(v.begin(), v.end());
    int flag = 1;
    for (int j = 0; j < n; j++) {
      if (brr[j] != v[j]) {
        flag = 0;
        break;
      }
    }
    if (flag) {
      ans = min(add, ans);
    }
  }
  cout << ans << endl;
  return 0;
}
