#include <bits/stdc++.h>
using namespace std;
const int mx = 100001;
int n, m;
long long int heads[mx];
long long int cells[mx];
bool simulate(long long int tm) {
  int cptr = 0;
  int hptr = 0;
  for (; hptr < n; hptr++) {
    if (cptr >= m) return true;
    long long int ctm = 0;
    long long int cpos = heads[hptr];
    if (cpos > cells[cptr]) {
      if (cpos - cells[cptr] > tm) return false;
      cpos = max(max(cpos, cpos + tm - 2 * (cpos - cells[cptr])),
                 cpos + (tm - cpos + cells[cptr]) / 2);
      while (cells[cptr] <= cpos and cptr < m) cptr++;
    } else {
      cpos += tm - ctm;
      while (cells[cptr] <= cpos and cptr < m) cptr++;
    }
  }
  if (cptr >= m)
    return true;
  else
    return false;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%lld", &heads[i]);
  for (int i = 0; i < m; i++) scanf("%lld", &cells[i]);
  sort(heads, heads + n);
  sort(cells, cells + m);
  long long int low = 0, high = 1e11;
  while (high - low > 1) {
    long long int m = (low + high) / 2;
    if (simulate(m))
      high = m;
    else
      low = m;
  }
  if (simulate(low))
    cout << low << endl;
  else if (simulate(high))
    cout << high << endl;
  else
    assert(false);
}
