#include <bits/stdc++.h>
using namespace std;
bool possible(long long t, set<long long> s, vector<long long> &p) {
  long long x = t;
  for (long long i = 0; i < (long long)(p).size() && !s.empty(); i++) {
    long long pos = p[i];
    long long star = *s.begin();
    if (star >= pos) {
      while (!s.empty() && *s.begin() <= pos + x) {
        s.erase(s.begin());
      }
    } else {
      if (pos - star > t) return false;
      long long r1 = star;
      r1 += (t - (pos - star));
      r1 = max(r1, pos);
      long long r2 = (t - (pos - star)) / 2 + pos;
      long long right = max(r1, r2);
      while (!s.empty() && *s.begin() <= right) {
        s.erase(s.begin());
      }
    }
  }
  if (s.empty()) return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  long long low = 0;
  set<long long> st;
  vector<long long> p(n);
  for (long long i = 0; i < n; i++) cin >> p[i];
  for (long long i = 0; i < m; i++) {
    long long x;
    cin >> x;
    st.insert(x);
  }
  long long high = 30ll * 1000000007ll;
  while (low < high) {
    long long mid = (low + high) / 2;
    if (possible(mid, st, p)) {
      if (possible(mid - 1, st, p)) {
        high = mid - 1;
      } else
        low = high = mid;
    } else
      low = mid + 1;
  }
  cout << low << endl;
}
