#include <bits/stdc++.h>
using namespace std;
bool check(int x, const vector<int> &a, const vector<int> &b)
{
unsigned int last = 0;
for (int p : a)
{
int l = p, r = p;
while(last < b.size())
{
l = min(b[last], l);
r = max(b[last], r);
int res = r - l + min(r - p, p - l);
if (res > x)
{
break;
}
++last;
}
}
return last == b.size();
}
int main()
{
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
for (int i = 0; i < m; ++i)
{
cin >> b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int l = 0, r = 1e9 + 7, ans = 1e9 + 7;
while(l <= r)
{
int mid = (l + r) / 2;
if (check(mid, a, b))
{
r = mid - 1;
ans = mid;
}
else
{
l = mid + 1;
}
}
cout << ans << "\n";
return 0;
}