#include <bits/stdc++.h>
const int maxn = 1e9;
const int N = 2020;
const int INF = 0x3f3f3f3f;
using namespace std;
int main() {
  vector<int> a, b;
  int n, m;
  scanf("%d%d", &n, &m);
  a.resize(n), b.resize(n);
  for (int i = 0; i <= n - 1; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i <= n - 1; i++) {
    scanf("%d", &b[i]);
  }
  sort(b.begin(), b.end());
  int minn = INF;
  for (int i = 0; i <= n - 1; i++) {
    int x;
    if (a[i] <= b[0])
      x = b[0] - a[i];
    else
      x = m + b[0] - a[i];
    vector<int> c(a);
    for (int j = 0; j <= n - 1; j++) {
      c[j] = (c[j] + x) % m;
    }
    sort(c.begin(), c.end());
    if (c == b) minn = min(minn, x);
  }
  printf("%d\n", minn);
  return 0;
}
