#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long mx = 1e5 + 10;
vector<long long> pos;
vector<long long> target;
long long n, m;
bool check(long long t) {
  long long idx = 0;
  for (long long i = 0; i < (n); ++i) {
    if (target[idx] < pos[i]) {
      auto it1 = upper_bound(target.begin() + idx, target.end(), pos[i]);
      if (it1 == target.end()) {
        long long temp = pos[i] - target[idx];
        if (temp > t) return false;
        return true;
      }
      long long in1 = it1 - target.begin();
      long long in2 = -1;
      long long in3 = -1;
      long long timeRight = pos[i] - target[idx];
      timeRight = t - timeRight;
      if (timeRight < 0)
        return false;
      else if (timeRight <= 1) {
        in2 = in1 - 1;
      }
      if (timeRight > 1) {
        timeRight /= 2;
        auto it2 =
            upper_bound(target.begin() + idx, target.end(), pos[i] + timeRight);
        it2--;
        in2 = it2 - target.begin();
      }
      long long timeLeft = pos[i] - target[idx];
      timeLeft *= 2;
      timeLeft = t - timeLeft;
      if (timeLeft > 0) {
        auto it2 =
            upper_bound(target.begin() + idx, target.end(), pos[i] + timeLeft);
        it2--;
        in3 = it2 - target.begin();
      }
      idx = max(in2, in3);
      idx++;
    } else {
      auto it1 = upper_bound(target.begin() + idx, target.end(), pos[i] + t);
      if (it1 != target.begin()) {
        it1--;
        long long in1 = it1 - target.begin();
        if (target[in1] >= pos[i]) {
          idx = in1 + 1;
        }
      }
    }
    if (idx == m) return true;
  }
  return false;
}
void solve() {
  cin >> n >> m;
  pos.clear();
  target.clear();
  pos.resize(n);
  target.resize(m);
  for (long long i = 0; i < (n); ++i) cin >> pos[i];
  for (long long i = 0; i < (m); ++i) cin >> target[i];
  long long l = -1, r = 2e10;
  while (l < r - 1) {
    long long mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt = 1;
  while (tt--) solve();
}
