#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& os, vector<int>& a) {
  for (int i = 0; i < a.size(); ++i) cout << a[i] << ' ';
  return os;
}
ostream& operator<<(ostream& os, vector<long long>& a) {
  for (int i = 0; i < a.size(); ++i) cout << a[i] << ' ';
  return os;
}
const double EPS = 1e-9;
const double PI = 3.1415926535897;
int n, m;
vector<long long> h, p;
bool can(long long t) {
  int cur = 0;
  for (int i = 0; i < (int)(n); ++i) {
    if (cur >= m) return true;
    if (p[cur] > h[i]) {
      while (cur < m && p[cur] <= t + h[i]) cur++;
    } else {
      if (t < h[i] - p[cur]) return false;
      long long x = max((t - (h[i] - p[cur])) / 2, t - 2 * (h[i] - p[cur]));
      while (cur < m && p[cur] <= x + h[i]) cur++;
    }
  }
  return (cur >= m);
}
long long solve() {
  long long l = 0, r = LLONG_MAX;
  while (r - l > 1) {
    long long mid = l + (r - l) / 2;
    if (can(mid))
      r = mid;
    else
      l = mid;
  }
  for (long long i = l; i <= r; ++i)
    if (can(i)) return i;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  h.resize(n);
  for (int i = 0; i < (int)(n); ++i) cin >> h[i];
  p.resize(m);
  for (int i = 0; i < (int)(m); ++i) cin >> p[i];
  cout << solve() << endl;
  return 0;
}
