#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pi pair<int, int>
#define pl pair<ll, ll>
#define f first
#define s second
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>
#define vpl vector<pl>
#define vpi vector<pi>
#define sz(x) x.size()
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define F0R(i,a) FOR(i,0,(a))
#define ROF(i,a,b) for(ll i=(b)-1;i>=(a);i--)
#define R0F(i,a) ROF(i,0,(a))
#define trav(el, v) for(auto &el : v)
#define nl endl
#define end(v) v.end()
#define bg(v) v.begin()
#define pb push_back
#define all(v) bg(v),end(v)
#define sor(v) sort(all(v))

ll INF = 1e18;

ll sol = INF;
vl a(5005), b(5005);
int n, m;

void solve(){
    vl p, ne, sufp, sufne, prep, prene;
    ll cnt = 0, cur = 0, mn;
    FOR(i, 1, n + 1){
        ll x = abs(a[i] - b[i]), y = m - x;
        if(a[i] > b[i]){
            if(x < y)ne.pb(x);
            else p.pb(y);
        }
        else if(a[i] < b[i]){
            if(x < y)p.pb(x);
            else ne.pb(y);
        }
        else cnt++;
        cur += min(x, y);
    }
    mn = cur;
    sor(p);
    sor(ne);
    /*trav(el, p)cerr << el << " ";
    cerr << nl;
    trav(el, ne)cerr << el << " ";
    cerr << nl;*/
    sufp.resize(sz(p));
    prep.resize(sz(p));
    //cerr << "0\n";
    sufne.resize(sz(ne));
    prene.resize(sz(ne));
    if(sz(p)){
        sufp.back() = p.back();
        prep[0] = p[0];
    }
    if(sz(ne)){
        sufne.back() = ne.back();
        prene[0] = ne[0];
    }
    R0F(i, sz(p) - 1)sufp[i] = sufp[i + 1] + p[i];
    R0F(i, sz(ne) - 1)sufne[i] = sufne[i + 1] + ne[i];
    FOR(i, 1, sz(p))prep[i] = prep[i - 1] + p[i];
    FOR(i, 1, sz(ne))prene[i] = prene[i - 1] + ne[i];
    //cerr << "1\n";
    F0R(i, sz(p)){
        ll el = p[i];
        ll x = cur;
        x -= (sz(p) - i) * el;
        if(i)x -= 2 * prep[i - 1];
        x += i * el;
        x += (sz(ne) + cnt) * el;
        ll g = (m / 2) - el + 1;
        ll ind = lower_bound(all(ne), g) - bg(ne);
        if(ind != sz(ne)){
            ll kolko = sz(ne) - ind;
            x -= 2 * kolko * (sufne[ind] + el);
            x += kolko * m;
        }
        //cerr << x << " ";
        mn = min(mn, x);
    }
    //cerr << "2\n";
    F0R(i, sz(ne)){
        ll el = ne[i];
        ll x = cur;
        x -= (sz(ne) - i) * el;
        if(i)x -= 2 * prene[i - 1];
        x += i * el;
        x += (sz(p) + cnt) * el;
        ll g = (m / 2) - el + 1;
        ll ind = lower_bound(all(p), g) - bg(p);
        if(ind != sz(p)){
            ll kolko = sz(p) - ind;
            x -= 2 * kolko * (sufp[ind] + el);
            x += kolko * m;
        }
       // cerr << x << " ";
        mn = min(mn, x);
    }
    //cerr << "3\n";
    //cerr << nl;
    sol = min(sol, mn);
}

long long Konflikt(int N,int M,int *A,int *B){
    n = N;
    m = M;
    FOR(i, 1, n + 1)a[i] = A[i];
    sort(bg(a) + 1, bg(a) + n + 1);
    FOR(i, 1, n + 1)b[i] = B[i];
    sort(bg(b) + 1, bg(b) + n + 1);
    int d = b[1];
    FOR(i, 1, n + 1)b[i] -= d;
    if(n <= 10 && m <= 20){
        //cerr << "tu\n";
        ll mn = INF;
        F0R(i, m){
            vl ac(n + 1);
            FOR(j, 1, n + 1)ac[j] = (a[j] + i) % m;
            sort(bg(ac) + 1, end(ac));
            F0R(k, n){
                ll cur = 0;
                FOR(j, 1, n + 1){
                    ll x = abs(ac[j] - b[j]), y = m - x;
                    cur += min(x, y);
                }
                mn = min(mn, cur);
                ac[0] = ac[n];
                ROF(j, 1, n + 1)ac[j] = ac[j - 1];
                //trav(el, ac)cerr << el << " ";
                //cerr << nl;
            }
        }
        return mn;
    }
    FOR(i, 1, n + 1){
        int d2 = m - a[n];
        ROF(j, 1, n)a[j + 1] = a[j] + d2;
        a[1] = 0;
        /*FOR(j, 1, n + 1)cerr << a[j] << " ";
        cerr << nl;
        FOR(j, 1, n + 1)cerr << b[j] << " ";
        cerr << nl;*/
        solve();
    }
    //cerr << "hi";
    //solve();
    //cerr << sol << nl;
    return sol;
}


/*
3 10
4 5 6
1 2 3

5 42
0 13 18 20 35
0 5 11 29 39

3 20
0 6 2
5 19 7

4 100
1 3 5 7
4 15 16 24
*/