#include <bits/stdc++.h>
int main() {
  long long int n, m, i, j, a[100005] = {0}, b[100005] = {0};
  scanf("%I64d%I64d", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%I64d", &a[i]);
  }
  for (i = 0; i < m; i++) {
    scanf("%I64d", &b[i]);
  }
  long long int low = 0, high = 10000000000000000LL, ans;
  while (low <= high) {
    long long int mid = (low + high) / 2;
    long long int x = 0, y = 0;
    for (i = 0; i < n; i++) {
      if (b[y] >= a[i]) {
        long long int range = a[i] + mid;
        while (y < m && b[y] <= range) {
          y++;
        }
      } else {
        long long int diff = (a[i] - b[y]);
        if (diff > mid) break;
        long long int range = (a[i] + mid - 2 * diff > a[i] + (mid - diff) / 2
                                   ? a[i] + mid - 2 * diff
                                   : a[i] + (mid - diff) / 2);
        while (y < m && b[y] <= range) {
          y++;
        }
      }
    }
    if (y == m) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
