#include <bits/stdc++.h>
using namespace std;
const int MAX = 1001 * 100;
int n, m;
vector<long long> h;
vector<long long> p;
bool check(long long t) {
  int i = n - 1, j = m - 1;
  while (i >= 0 && j >= 0) {
    if (h[i] >= p[j]) {
      j = lower_bound(p.begin(), p.end(), h[i] - t) - p.begin();
      j--;
    } else {
      long long dis = p[j] - h[i];
      if (t < dis) return false;
      long long tt = t - dis;
      tt /= 2;
      int jj = lower_bound(p.begin(), p.end(), h[i] - tt) - p.begin();
      jj--;
      long long ttt = t - dis;
      int jjj = lower_bound(p.begin(), p.end(), p[j] - ttt) - p.begin();
      jjj--;
      j = min(min(j, jj), jjj);
    }
    i--;
  }
  if (j > -1) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    long long tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  for (int i = 0; i < m; i++) {
    long long tmp;
    cin >> tmp;
    p.push_back(tmp);
  }
  long long r = 2 * ((1e10) + 1), l = -1;
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r;
}
