#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
bool check(const vector<long long int> &a, const vector<long long int> &b,
           long long int x) {
  int n = a.size();
  int k = b.size();
  vector<pair<int, int> > range(n);
  for (int i = (int)(0); i < (int)(n); i++) {
    if (abs(a[i]) > x) {
      return false;
    }
    long long int lo, hi;
    long long int rem = (x - abs(a[i])) / 2;
    if (a[i] < 0) {
      lo = a[i] - rem;
      hi = rem;
    } else {
      lo = -rem;
      hi = a[i] + rem;
    }
    range[i] = pair<int, int>(lower_bound(b.begin(), b.end(), lo) - b.begin(),
                              upper_bound(b.begin(), b.end(), hi) - b.begin());
  }
  int pos = 0;
  for (int i = (int)(0); i < (int)(n); i++) {
    if (pos >= k) {
      return false;
    }
    pair<int, int> r = range[i];
    if (r.second <= pos) {
      return false;
    }
    if (r.second - r.first <= 0) {
      return false;
    }
    pos = max(pos, r.first) + 1;
  }
  return true;
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  long long int p;
  cin >> n >> k >> p;
  vector<long long int> a(n), b(k);
  for (int i = (int)(0); i < (int)(n); i++) {
    cin >> a[i];
    a[i] -= p;
  }
  for (int i = (int)(0); i < (int)(k); i++) {
    cin >> b[i];
    b[i] -= p;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  long long int lo = -1;
  long long int hi = 1e15;
  while (hi - lo > 1) {
    long long int mid = (hi + lo) / 2;
    if (check(a, b, mid)) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  cout << hi << "\n";
}
