#include <bits/stdc++.h>
using namespace std;
long long h[100005], p[100005];
int flag, n, m;
const long long inf = 1e12;
long long judge(long long tim) {
  long long t = 0;
  for (int i = 0; i < n; i++) {
    long long w = tim;
    if (h[i] >= p[t]) {
      if (tim >= h[i] - p[t]) {
        w = max(w - 2 * (h[i] - p[t]), (w - h[i] + p[t]) / 2);
        if (w < 0) w = 0;
      } else
        return 0;
    }
    t = upper_bound(p, p + m, h[i] + w) - p;
    if (t >= m) return 1;
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  long long ans;
  for (int i = 0; i < n; i++) cin >> h[i];
  for (int i = 0; i < m; i++) cin >> p[i];
  sort(h, h + n);
  sort(p, p + m);
  long long le = 0;
  long long ri;
  long long mid;
  ri = inf;
  mid = (le + ri) >> 1;
  while (ri >= le) {
    if (judge(mid)) {
      ans = mid, ri = mid - 1;
    } else
      le = mid + 1;
    mid = (le + ri) >> 1;
  }
  cout << ans << endl;
  return 0;
}
