#include <bits/stdc++.h>
using namespace std;
long long P[100005], H[100005];
int n, m;
bool ok(long long x) {
  int i = 0, j = 0;
  long long left = 1, cur;
  while (i < n && j < m) {
    if (P[j] < left) {
      j++;
      continue;
    }
    left = P[j];
    if (H[i] > left) {
      if (H[i] - left > x) return false;
      long long left1, left2;
      cur = x - 2 * (H[i] - left);
      left1 = H[i] + 1;
      if (cur > 0) left1 += cur;
      cur = x - (H[i] - left);
      left2 = H[i] + 1 + cur / 2;
      left = max(left1, left2);
    } else if (H[i] <= left) {
      left = max(left, H[i] + x + 1);
    }
    i++;
  }
  while (P[j] < left && j < m) j++;
  return j == m;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> H[i];
  for (int i = 0; i < m; i++) cin >> P[i];
  long long lo = 0, hi = 1e11, mid;
  mid = (lo + hi) / 2;
  long long res = -1;
  while (lo <= hi) {
    if (ok(mid))
      res = mid, hi = mid - 1;
    else
      lo = mid + 1;
    mid = (lo + hi) / 2;
  }
  cout << res << "\n";
}
