#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000100;
const int INF = 1e9 + 10;
int n, m;
long long a[maxn], b[maxn];
bool check(long long k) {
  int now = 1;
  long long r = b[now] - 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] - b[now] > k) return 0;
    if (b[now] >= a[i])
      r = max(r, a[i] + k);
    else {
      r = max(r, a[i] + (k - 2 * (a[i] - b[now])));
      r = max(r, a[i] + (k - (a[i] - b[now])) / 2);
    }
    while (now <= m && b[now] <= r) now++;
    if (now > m) break;
  }
  return now > m;
}
long long bin(long long l, long long r) {
  long long res = r;
  while (l <= r) {
    long long m = (l + r) >> 1;
    if (check(m))
      res = min(res, m), r = m - 1;
    else
      l = m + 1;
  }
  return res;
}
int main() {
  while (cin >> n >> m) {
    for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
    for (int i = 1; i <= m; i++) scanf("%I64d", &b[i]);
    sort(a + 1, a + n + 1);
    sort(b + 1, b + m + 1);
    cout << bin(0, 1LL * INF * INF) << endl;
  }
  return 0;
}
