#include <bits/stdc++.h>


using namespace std;

#define ll long long

#define rep(i,a,b) for(int i=a;i<b;i+=1)
#define repE(i,a,b) for(int i=a;i<=b;i+=1)

#define pb push_back
const ll MOD = 1e9 + 7;
const ll N = 1e5 + 5, M = 10;
const ll WE = 1e5;
const ll maxV = 1e3;
const ll INF = 1e18;

ll ceil(ll a, ll b) {
    return (a + b - 1) / b;
}

int s(int n) {
    int sum = 0;

    while (n) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}


void solve() {

    int a, b;
    cin >> a >> b;

    int suma = s(a);
    int sumb = s(b);

    cout << max(sumb, suma) << "\n";
}
int main()
{

    ios::sync_with_stdio (0);

    cin.tie (NULL);

    int t = 1;
    // cin >> t;

    // compute();

    // sieve();


    while (t--) {
        solve();

    }



    return 0;
}
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>

using namespace std;

#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>

const ll mod = 1000000007;
const ll INF = 1001001001001001001;

int main(){
    int n, m;
    cin >> n >> m;
    vector<int> h(n);
    vector<int> w(m);
    rep(i, n) cin >> h[i];
    rep(i, m) cin >> w[i];
    sort(h.begin(), h.end());
    vector<ll> l(n/2+1), r(n/2+1);
    l[0] = 0;
    r[0] = 0;
    rep(i, n/2){
        l[i+1] = l[i] + (h[2*i+1] - h[2*i]);
    }
    rep(i, n/2){
        r[i+1] = r[i] + (h[2*i+2] - h[2*i+1]);
    }
    // rep(i, n/2+1){
    //     cout << l[i] << " ";
    // }cout << endl;
    // rep(i, n/2+1){
    //     cout << r[i] << " ";
    // }cout << endl;

    ll ans = INF;
    rep(i, m){
        int idx = upper_bound(h.begin(), h.end(), w[i]) - h.begin();
        ll ans_ = 0;
        if(idx%2 == 1){
            ans_ = l[idx/2] + (w[i] - h[idx-1]) + (r[n/2] - r[idx/2]);
        }else {
            ans_ = l[idx/2] + (h[idx] - w[i]) + (r[n/2] - r[idx/2]);
        }
        ans = min(ans, ans_);
        // cout << ans_ << " " << idx << endl;
    }
    cout << ans << endl;
}