#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<functional>
#include<iostream>
#include<iomanip>
#include<map>
#include<numeric>
#include<optional>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<tuple>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#pragma GCC optimize("Ofast")
namespace gengar
{
	using namespace std;
	#define int long long
	#define itn int
	#define uint unsigned long long
	#define ld long double
	#define vt(tp) vector<tp>
	#define vvt(tp) vector<vector<tp>>
	#define vvt2(nm,tp,h,w,n) vector<vector<tp>>nm((h),vector<tp>(w,n))
	#define P pair<int,int>
	#define hmap unordered_map
	#define hset unordered_set
	#define all(x) x.begin(),x.end()
	#define nsort(x) sort(all(x))
	#define rsort(x) nsort(x);reverse(all(x))
	#define unq(v) v.erase(unique(all(v)),v.end())
	#define l_b(c,x) distance(c.begin(),lower_bound(all(c),(x)))
	#define u_b(c,x) distance(c.begin(),upper_bound(all(c),(x)))
	#define fi first
	#define se second
	#define mp make_pair
	#define pb push_back
	#define eb emplace_back
	#define spc ' '
	#define pass 
	#define _overload3(_1,_2,_3,name,...) name
	#define _rep(i,n) repi(i,0,n)
	#define repi(i,a,b) for(int i=(a),__SIZE##_=(b);i<__SIZE##_;i++)
	#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
	#define _daolrevo3(_1,_2,_3,name,...) name
	#define _per(i,n) peri(i,n,0)
	#define peri(i,a,b) for(int i=(a),__SIZE##_=(b);i>=__SIZE##_;i--)
	#define per(...) _daolrevo3(__VA_ARGS__,peri,_per,)(__VA_ARGS__)
	#define Bit(n) (1LL<<(n))
	#define myceil(a,b) ((a)+((b)-1))/(b)
	#define scale(n) cout<<fixed<<setprecision(n)
	using i64=int64_t;
	using u64=uint64_t;
	template<class T>using PQ=priority_queue<T,vt(T),greater<T>>;
	void in(){}
	template<class Car,class...Cdr>
	void in(Car&&car,Cdr&&...cdr){cin>>car;in(forward<Cdr>(cdr)...);}
	template<class T>void drop(T x){cout<<(x)<<endl;exit(0);}
	void dYes(){puts("Yes");exit(0);}
	void dNo(){puts("No");exit(0);}
	int gcd(int a,int b){return b?gcd(b,a%b):a;}
	int lcm(int a,int b){return a/gcd(a,b)*b;}
	int fact(int n,int m){int f=n;for(int i=n-1;i>=1;i--){f*=i;f%=m;}return f;}
	template<class T>int chmin(T&a,const T&b){if(b<a){a=b;return 1;}return 0;}
	template<class T>int chmax(T&a,const T&b){if(a<b){a=b;return 1;}return 0;}
	const int inf=Bit(60);
	const double pi=acos(-1);
	const int mod=1000000007;//998244353
};
using namespace gengar;

int rui[100010],rui2[100010];
int32_t main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,m;in(n,m);
	vector<int>h(n),w(m);
	rep(i,n)cin>>h[i];
	rep(i,m)cin>>w[i];
	sort(all(h));
	for(int i=0;i<n-1;i+=2)rui[i/2+1]=rui[i/2]+h[i+1]-h[i];
	for(int i=n-2;i>0;i-=2)rui2[i/2]=rui2[i/2+1]+h[i+1]-h[i];
	int ans=inf;
	for(auto p:w)
	{
		int x=l_b(h,p);
		if(x%2)x--;
		chmin(ans,rui[x/2]+rui2[x/2]+abs(h[x]-p));
	}
	cout<<ans<<endl;
	return 0;
}
#include <bits/stdc++.h>
#define int long long 
#define pb push_back
#define pf push_front
#define pipii pair<int,pair<int,int>>
#define pii pair<int,int>
#define mod 1000000007
#define ss second 
#define vi vector<int>
#define vvi vector<vector<int>> 
#define vpii vector<pair<int,int>>  
#define ff first
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend();
#define ld long double
#define inf (int)1e9+7
#define sp fixed<<setprecision(12)
using namespace std; 

// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;

// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;




#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}

int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
char charr[]={'L','R','U','D'};








void solve(int tc){
    //cout<<"Case #"<<tc<<": ";
    
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int ans=INT_MAX;
    for(int i=0;i<(1ll<<n);i++){
        vector<int> v;
        int orr=0;
        for(int j=0;j<n;j++){
            if((1ll<<j)&i){
                orr|=a[j];
                v.pb(orr);
                orr=0;
            }else{
                orr|=a[j];
            }
        }
        if(orr>0){
            v.pb(orr);
        }
        int xorr=0;
        for(int x:v){
            xorr^=x;
        }
        ans=min(ans,xorr);
    }
    cout<<ans;

}


int32_t main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    //double    pi=3.14159265358979323846;
    //precompute();
    int t=1;
    //cin>>t;
    int tc=1;
    while(t--){
        solve(tc);
        tc++;
        
    }
    
    return 0;
    
}//Ardhendu Reja , JGEC

/*
int powmod(int x,int n,int p)
{
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x)%p;
        x=(x*x)%p;
        n=n/2;
    }
    return result;
}


int modmul(int a,int b) {
    return ((a % mod) * (b % mod)) % mod;
}
*/


