#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1000000007LL;

int main(void){
    int N, M;
    cin >> N >> M;
    vector<ll> H(N), W(M);
    for(int i = 0; i < N; i++) cin >> H[i];
    sort(H.begin(), H.end());
    for(int i = 0; i < M; i++) cin >> W[i];
    sort(W.begin(), W.end());
    vector<ll> L(N, 0), R(N, 0);
    for(int i = 1; i < N - 1; i += 2){
        L[i + 1] = L[i - 1] + H[i] - H[i - 1];
    }
    for(int i = N - 2; i > 0; i -= 2){
        R[i - 1] = R[i + 1] + H[i + 1] - H[i];
    }

    // for(int i = 0; i < N; i++) cout << L[i] << "\t";
    // cout << endl;
    // for(int i = 0; i < N; i++) cout << R[i] << "\t";
    // cout << endl;

    ll ans = 1000000000000000000LL;
    for(int i = 0; i < M; i++){
        auto itr = lower_bound(H.begin(), H.end(), W[i]);
        int idx = distance(H.begin(), itr);
        idx = 2 * (idx / 2);
        ll sub = abs(W[i] - H[idx]);
        sub += L[idx] + R[idx];
        ans = min(sub, ans);
        // if(sub == 30) cout << i << endl;
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\n'
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define pll pair<long long, long long>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                      \
    do {                                    \
        ostringstream os;                   \
        os << setw(n) << setfill('0') << s; \
        s = os.str();                       \
    } while (0);
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと
#define INIT                               \
    std::cin.tie(nullptr);                 \
    std::ios_base::sync_with_stdio(false); \
    cout << std::fixed << std::setprecision(10);

template <typename T>
vector<T> make_vec(size_t a, T n) { return vector<T>(a, n); }
template <typename T, typename... Ts>
auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); }
template <class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec)
        is >> x;
    return is;
}

constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

signed main() {
    INIT;
    cin >> N >> W;
    if (N > 40)
        return 0;
    vector<ll> v(N), w(N);
    REP(i, N) {
        cin >> v[i]; // >> w[i];
        w[i] = v[i];
    }
    vector<pair<ll, ll>> wv1(1);
    vector<pair<ll, ll>> wv2(1);
    auto merge = [](vector<pll> &wv, pll a) {
        auto [w, v] = a;
        vector<pll> res;
        int y = 0;
        REP(x, wv.size()) {
            while (wv[y].first + w < wv[x].first) {
                res.push_back({wv[y].first + w, wv[y].second + v});
                y++;
            }
            res.push_back(wv[x]);
        }
        FOR(i, y, wv.size()) {
            res.push_back({wv[i].first + w, wv[i].second + v});
        }
        return res;
    };
    REP(i, N) {
        wv1 = merge(wv1, {w[i], v[i]});
        swap(wv1, wv2);
    }

    auto f = [](vector<pll> &wv) {
        ll now = -1;
        vector<pair<ll, ll>> res;
        REP(i, wv.size()) {
            if (chmax(now, wv[i].second)) {
                res.push_back(wv[i]);
            }
        }
        return res;
    };
    wv1 = f(wv1);
    wv2 = f(wv2);
    dump(wv1, wv2);
    int ind = wv2.size() - 1;
    for (auto [w1, v1] : wv1) {
        if (w1 > W)
            break;
        while (w1 + wv2[ind].first > W) {
            ind--;
        }
        chmax(ans, v1 + wv2[ind].second);
    }
    cout << ans << endl;
}
