#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const long long INF = 1e15 + 7;
int n, m, pre[N], suf[N];
long long h[N], p[N];
bool check(long long t) {
  int j = 0;
  for (int i = (0); i < (n); ++i) {
    if (abs(h[i] - p[j]) > t) continue;
    long long r;
    if (p[j] < h[i]) {
      long long x1 = max(h[i], h[i] + t - 2 * (h[i] - p[j]));
      long long x2 = h[i] + (t - h[i] + p[j]) / 2;
      r = max(x1, x2);
    } else {
      r = h[i] + t;
    }
    while (j < m && p[j] <= r) ++j;
  }
  return j >= m;
}
long long solve() {
  pre[0] = 0, suf[n + 1] = m + 1;
  long long l = 0, r = INF;
  while (l + 1 < r) {
    long long z = (l + r) >> 1;
    check(z) ? r = z : l = z;
  }
  return check(l) ? l : r;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (n); ++i) scanf("%lld", &h[i]);
  for (int i = (0); i < (m); ++i) scanf("%lld", &p[i]);
  printf("%lld", solve());
  return 0;
}
