#include<bits/stdc++.h>
using namespace std;
using ll=long long;

#define pb push_back
#pragma GCC optimize("O3")

#define N 2020
const ll inf=1e18,mxM=1e9;
ll a[N],b[N],n,m;
ll oa[N],ob[N];

ll dst(ll x,ll y){
    return min(abs(x-y),m-abs(x-y));
}

/*
vector<int>cl1,cl2;
const ll N2=45*N;
ll lc1[N2],rc1[N2],nd1;
struct node{
    ll sum=0;
    ll cnt=0;
};
node st1[N2];

node mrg(node x,node y){
    node ret;
    ret.cnt=x.cnt+y.cnt;
    ret.sum=x.sum+y.sum;
    return ret;
}

void upd1(ll id,ll x,ll i=1,ll l=1,ll r=mxM){
    if(id<=0||id>mxM)return;
    if(id<l||id>r)return;
    cl1.pb(i);
    if(l==r){
        st1[i].cnt+=x;
        st1[i].sum+=x*l;
        return;
    }
    ll md=(l+r)/2;
    if(id<=md){
        if(!lc1[i])lc1[i]=++nd1;
        upd1(id,x,lc1[i],l,md);
    }else{
        if(!rc1[i])rc1[i]=++nd1;
        upd1(id,x,rc1[i],md+1,r);
    }
    st1[i]=mrg(st1[lc1[i]],st1[rc1[i]]);
}

node get1(ll l,ll r,ll i=1,ll tl=1,ll tr=mxM){
    if(l<=0||l>mxM)return{0,0};
    if(r<=0||r>mxM)return{0,0};
    if(l>r)return{0,0};
    if(tl>=l&&tr<=r)return st1[i];
    ll md=(tl+tr)/2;
    node ret={0,0};
    if(md>=l){
        if(lc1[i])ret=mrg(ret,get1(l,r,lc1[i],tl,md));
    }
    if(md+1<=r){
        if(rc1[i])ret=mrg(ret,get1(l,r,rc1[i],md+1,tr));
    }
    return ret;
}

ll lc2[N2],rc2[N2],nd2;
node st2[N2];

void upd2(ll id,ll x,ll i=1,ll l=1,ll r=mxM){
    if(id<=0||id>mxM)return;
    if(id<l||id>r)return;
    cl2.pb(i);
    if(l==r){
        st2[i].cnt+=x;
        st2[i].sum+=x*l;
        return;
    }
    ll md=(l+r)/2;
    if(id<=md){
        if(!lc2[i])lc2[i]=++nd2;
        upd2(id,x,lc2[i],l,md);
    }else{
        if(!rc2[i])rc2[i]=++nd2;
        upd2(id,x,rc2[i],md+1,r);
    }
    st2[i]=mrg(st2[lc2[i]],st2[rc2[i]]);
}

node get2(ll l,ll r,ll i=1,ll tl=1,ll tr=mxM){
    if(l<=0||l>mxM)return{0,0};
    if(r<=0||r>mxM)return{0,0};
    if(l>r)return{0,0};
    if(tl>=l&&tr<=r)return st2[i];
    ll md=(tl+tr)/2;
    node ret={0,0};
    if(md>=l){
        if(lc2[i])ret=mrg(ret,get2(l,r,lc2[i],tl,md));
    }
    if(md+1<=r){
        if(rc2[i])ret=mrg(ret,get2(l,r,rc2[i],md+1,tr));
    }
    return ret;
}

void res1(ll id,ll i=1,ll l=1,ll r=mxM){
    if(l==r){
        st1[i]={0,0};
        return;
    }
    ll md=(l+r)/2;
    if(id<=md)res1(id,lc1[i],l,md);
    else res1(id,rc1[i],md+1,r);
    lc1[i]=rc1[i]=0;
    st1[i]={0,0};
}

void res2(ll id,ll i=1,ll l=1,ll r=mxM){
    if(l==r){
        st2[i]={0,0};
        return;
    }
    ll md=(l+r)/2;
    if(id<=md)res2(id,lc2[i],l,md);
    else res2(id,rc2[i],md+1,r);
    lc2[i]=rc2[i]=0;
    st2[i]={0,0};
}*/

long long Konflikt(int _n,int _m,int *A,int *B){
    //if(_n>2000)return 0;
    /*nd2=1;
    upd2(2,1);
    upd2(2,1);
    upd2(4,1);
    upd2(4,1);
    upd2(4,-1);
    node g2=get2(2,4);
    cout<<g2.cnt<<' '<<g2.sum<<endl;
    res2(2);
    res2(4);
    return 0;*/

    n=_n;
    m=_m;
    for(int i=1;i<=n;++i)a[i]=A[i];
    for(int i=1;i<=n;++i)b[i]=B[i];
    sort(a+1,a+n+1);
    sort(b+1,b+n+1);
    for(int i=1;i<=n;++i)
        oa[i]=a[i];
    for(int i=1;i<=n;++i)
        ob[i]=b[i];

    ll ans=inf;
    for(int _i=1;_i<=n;++_i){
        ll pnt=0;
        for(int ii=_i;ii<=n;++ii)
            a[++pnt]=oa[ii];
        for(int ii=1;ii<_i;++ii)
            a[++pnt]=oa[ii];
        for(int i=1;i<=n;++i)
            b[i]=ob[i];
        for(int i=1;i<=n;++i){
            ll dod=m-a[i];
            a[i]=(a[i]+dod)%m;
            b[i]=(b[i]+dod)%m;
        }
        sort(b+1,b+n+1);
        vector<ll>cand;
        for(int j=1;j<=n;++j){
            ll x=(b[j]-a[j]+2*m)%m;
            cand.pb(x);
        }
        sort(cand.begin(),cand.end());
        ll plf=0,prt=0,pmd=0;
        ll c1=0,s1=0;
        ll c2=0,s2=0;
        ll c3=0,s3=0;
        ll c4=0,s4=0;
        for(int i=1;i<=n;++i){
            ++c4;
            s4+=b[i];
        }
        for(ll x:cand){
            while(prt+1<=n&&b[prt+1]<=(2*x+m)/2){
                ++prt;
                s4-=b[prt];--c4;
                s3+=b[prt];++c3;
            }
            while(pmd+1<=n&&b[pmd+1]<=x){
                ++pmd;
                s3-=b[pmd];--c3;
                s2+=b[pmd];++c2;
            }
            while(plf+1<=n&&b[plf+1]<=(2*x-m)/2){
                ++plf;
                s2-=b[plf];--c2;
                s1+=b[plf];++c1;
            }
            ll cur=0;
            cur+=c1*(m-x)+s1;
            cur+=c2*x-s2;
            cur+=s3-c3*x;
            cur+=c4*(m+x)-s4;
            ans=min(ans,cur);
        }
    }

    //1)
    //b[i]<=x
    //1.1) x-b[i]<m-(x-b[i])
    //x-b[i]<m-x+b[i]
    //2x-m<2b[i]
    //(2x-m)/2<b[i] (mogu floor jer je < ne <=)
    //onda je to suma cnt*x-sumB
    //1.2) za ostale je to cnt*(m-x)+sumB
    //2)
    //b[i]>x
    //2.1) b[i]-x<=m-(b[i]-x)
    //b[i]-x<=m-b[i]+x
    //2b[i]<=2x+m
    //odnsono b[i]<=(2x+m)/2
    //onda dodajem sumB-cnt*x
    //2.2) sad dodajem cnt*(m+x)-sumB

    //drzim 3 pokazivaca
    //1 za <=b[i]
    //drugi za <=(2x-m)/2

    //dodajem po najmanjem x
    //drzim pokazivace na b
    //ako drzim implicitno
    //samo mi treba pnt upd i

    return ans;
}

#undef N

/*
int main()
{
    int N,M;
    cin >> N >> M;
    int* A = new int[N+1];
    int* B = new int[N+1];

    for (int i=1; i<=N; i++) cin >> A[i];
    for (int i=1; i<=N; i++) cin >> B[i];

    cout << Konflikt(N,M,A,B) <<endl;

    delete[] A;
    delete[] B;
    return 0;
}

*/