#include <bits/stdc++.h>
using namespace std;

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

const string YES = "Yes";

const string NO = "No";

ll multiplecount(ll A, ll B, ll X) {
    ll countA = (A - 1) / X;
    ll countB = B / X;
    ll ans = countB - countA;
    return ans;
}

int main() {
    ll A, B;
    cin >> A >> B;

    ll X = B - A;
    ll ans;

    for (ll i = X; i > 0; i--) {
        if (multiplecount(A, B, i) >= 2) {
            ans = i;
            break;
        }
    }

    cout << ans << endl;

    return 0;
}
#include<bits/stdc++.h>
#define LL long long
using namespace std;
int n,m;
LL a[200010],b[200010];
LL A[200010],B[200010],ans=1e18;
inline LL read()
{
	LL x=0,w=0;char ch=0;
	while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return w?-x:x;
}
LL Find(LL x)
{
	int L=1,R=n,M,T=1;
	while(L<=R){
		M=(L+R)>>1;
		if(a[M]<=x)T=M,L=M+1;
		else R=M-1;
	}
	return T;
}
int main()
{
	n=read();m=read();
	for(int i=1;i<=n;(i++)[a]=read());
	for(int i=1;i<=m;*(b+i++)=read());
	sort(a+1,a+1+n);
	//for(int i=1;i<=n;i++)
	//	cout<<a[i]<<' ';
	//cout<<endl;
	for(int i=2;i<=n;i+=2){
		B[i]=B[i-2];
		A[i+1]=A[i-1];
		B[i]+=a[i]-a[i-1];
		A[i+1]+=a[i+1]-a[i];
	}
	//for(int i=1;i<=n;i++){
	//	if(i&1)printf("%d ",A[i]);
	//	else printf("%d ",B[i]);
	//}
	//cout<<endl;
	for(int i=1;i<=m;i++){
		int pos=Find(b[i]);
		//cout<<b[i]<<','<<pos<<' ';
		if(pos&1){
			ans=min(ans,B[pos-1]+abs(a[pos]-b[i])+A[n]-A[pos]);
			ans=min(ans,B[pos-1]+abs(a[pos+1]-b[i])+abs(a[pos]-a[pos+2])+A[n]-A[pos+2]);
		}else{
			ans=min(ans,B[pos-2]+A[n]-A[pos+1]+abs(b[i]-a[pos])+abs(a[pos-1]-a[pos+1]));
			ans=min(ans,B[pos]+A[n]-A[pos+1]+abs(a[pos+1]-b[i]));
		}
		//cout<<ans<<'\n';
	}
	cout<<ans<<'\n';
}