#include #include #include using namespace std; int main(){ int n,m;cin>>n>>m; vector A(n),B(m); for(int i = 0; n > i; i++)cin>>A[i]; for(int i = 0; m > i; i++)cin>>B[i]; sort(A.begin(), A.end()); sort(B.begin(), B.end()); A.push_back(100000000001LL); vector C[A.size()]; int nw = 0; for(int i = 0; m > i; i++){ while(B[i]>A[nw]){ nw++; } C[nw].push_back(B[i]); } long long ans = 0; for(int i = 0; n >= i; i++){ if(C[i].size() == 0)continue; if(!i){ ans += A[i]-C[i][0]; }else if(i == n){ ans += C[i][C[i].size()-1]-A[i-1]; }else{ long long mx = max(C[i][0]-A[i-1], A[i]-C[i][C[i].size()-1]); for(int j = 1; C[i].size() > j; j++){ mx = max(mx,C[i][j]-C[i][j-1]); } ans += A[i]-A[i-1]-mx; } } cout << ans << endl; }