#include <bits/stdc++.h>
using namespace std;
int n, m;
long long int header[100007], track[100007];
int check(long long int Time) {
  int mark[100007];
  memset(mark, 0, sizeof mark);
  int trackpos = 1;
  for (int i = 1; i <= n; i++) {
    if (trackpos > m) break;
    if (track[trackpos] <= header[i]) {
      long long int need = header[i] - track[trackpos];
      if (need <= Time) {
        mark[trackpos] = 1;
        long long int A = max(0LL, Time - 2 * need);
        long long int B = (Time - need) / 2;
        long long int left = header[i] + max(A, B);
        trackpos++;
        while (1) {
          if (trackpos > m) break;
          if (track[trackpos] <= left) {
            mark[trackpos] = 1;
            trackpos++;
          } else
            break;
        }
      }
    } else {
      long long int left = header[i] + Time;
      while (1) {
        if (trackpos > m) break;
        if (track[trackpos] <= left) {
          mark[trackpos] = 1;
          trackpos++;
        } else
          break;
      }
    }
  }
  for (int i = 1; i <= m; i++)
    if (!mark[i]) return 0;
  return 1;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lld", &header[i]);
  for (int i = 1; i <= m; i++) scanf("%lld", &track[i]);
  sort(header + 1, header + n + 1);
  sort(track + 1, track + m + 1);
  long long int low = 0, high = 100000000000000;
  long long int ans = INT_MAX;
  while (low <= high) {
    long long int mid = (low + high) / 2;
    if (check(mid)) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  printf("%lld\n", ans);
  return 0;
}
