#include <bits/stdc++.h>
using namespace std;
int n, m;
long long h[100100], p[100100];
bool solve(long long step) {
  int j = 0;
  for (int i = 0; i < n; i++) {
    long long y = h[i] - p[j];
    long long pos = h[i];
    if (y < 0)
      pos += step;
    else if (y > step)
      pos = p[j] - 1;
    else if (pos > p[j])
      pos += ((step - y) / 2 > step - y - y ? (step - y) / 2 : step - y - y);
    else
      pos += step;
    if (pos >= p[m - 1]) {
      j = m;
      break;
    }
    while (pos >= p[j]) j++;
  }
  if (j >= m)
    return true;
  else
    return false;
}
int main() {
  long long l, r, mid;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> h[i];
  for (int i = 0; i < m; i++) cin >> p[i];
  l = 0;
  r = 2 * (p[m - 1] > h[n - 1] ? p[m - 1] : h[n - 1]);
  mid = (l + r) / 2;
  while (l < r) {
    if (solve(mid))
      r = mid - 1;
    else
      l = mid + 1;
    mid = (r + l) / 2;
  }
  if (solve(mid))
    cout << mid;
  else
    cout << mid + 1;
  return 0;
}
