#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){

    /*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());
        cl1.clear();
        cl2.clear();
        nd1=nd2=1;
        for(int i=1;i<=n;++i)
            upd2(b[i],1);
        pnt=0;
        for(ll x:cand){
            while(pnt+1<=n&&b[pnt+1]<=x){
                ++pnt;
                upd2(b[pnt],-1);
                upd1(b[pnt],1);
            }
            ll cur=0;
            node jj=get1((2*x-m)/2+1,mxM);
            node jd=get1(1,(2*x-m)/2);
            node dj=get2(1,(2*x+m)/2);
            node dd=get2((2*x+m)/2+1,mxM);
            cur+=jj.cnt*x-jj.sum;
            cur+=jd.cnt*(m-x)+jj.sum;
            cur+=dj.sum-dj.cnt*x;
            cur+=dd.cnt*(m+x)-dd.sum;
            //cout<<_i<<' '<<x<<' '<<cur<<endl;
            ans=min(ans,cur);
        }
        for(ll x:cl1){
            st1[x]={0,0};
            lc1[x]=rc1[x]=0;
        }
        for(ll x:cl2){
            st2[x]={0,0};
            lc2[x]=rc2[x]=0;
        }
    }

    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;
}
*/