#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, k, p, i, j;
  cin >> n >> k >> p;
  long long a[n], b[k];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (j = 0; j < k; j++) {
    cin >> b[j];
  }
  long long start = 0, end = (long long)3000000000, ans;
  while (start <= end) {
    long long mid = (start + end) / 2;
    set<long long> s;
    for (i = 0; i < k; i++) {
      s.insert(b[i]);
    }
    for (i = 0; i < n; i++) {
      long long x = a[i], o = p;
      if (x <= o) {
        if (x + o > mid || 1) {
          long long y = (x + o - mid) / 2 + (x + o - mid) % 2;
          auto val = s.lower_bound(y);
          if (val != s.end() && abs(x - *val) + abs(o - *val) <= mid)
            s.erase(s.lower_bound(y));
          else
            break;
        } else {
          auto val = s.lower_bound(x);
          if (val != s.end() && abs(x - *val) + abs(o - *val) <= mid)
            s.erase(s.lower_bound(x));
          else
            break;
        }
      } else {
        if (x + o > mid || 1) {
          long long y = (x + o - mid) / 2 + (x + o - mid) % 2;
          auto val = s.lower_bound(y);
          if (val != s.end() && abs(x - *val) + abs(o - *val) <= mid)
            s.erase(s.lower_bound(y));
          else
            break;
        } else {
          auto val = s.lower_bound(o);
          if (val != s.end() && abs(x - *val) + abs(o - *val) <= mid)
            s.erase(s.lower_bound(o));
          else
            break;
        }
      }
    }
    if (i == n) {
      ans = mid;
      end = mid - 1;
    } else {
      start = mid + 1;
    }
  }
  cout << ans;
}
