#include <bits/stdc++.h>

using namespace std;

#define MAXN 5001

vector<long long> a,b;
long long nisovac[MAXN*2];
long long marsovac[MAXN];
vector<pair<long long,long long>> values;

long long Konflikt(int N,int M,int *A,int *B)
{
    if (N<=10 and M<=20)
    {
        long long answer=LLONG_MAX;
        for (int x=0;x<M;x++)
        {
            for (int i=1;i<=N;i++) a.push_back((A[i]+x)%M);
            for (int i=1;i<=N;i++) b.push_back(B[i]);
            sort(a.begin(),a.end());sort(b.begin(),b.end());
            long long solution=0;for (int i=0;i<N;i++) solution+=min(abs(a[i]-b[i]),M-abs(a[i]-b[i]));
            while (next_permutation(b.begin(),b.end()))
            {
                long long sol=0;for (int i=0;i<N;i++) sol+=min(abs(a[i]-b[i]),M-abs(a[i]-b[i]));
                solution=min(sol,solution);
            }
            answer=min(answer,solution);a.clear();b.clear();
        }
        return answer;
    }
    if (N<=500 and M<=500)
    {
        long long answer=LLONG_MAX;
        for (int i=1;i<=N;i++) nisovac[i]=B[i];
        sort(nisovac+1,nisovac+N+1);for (int i=N+1;i<=2*N;i++) nisovac[i]=nisovac[i-N];
        for (int x=0;x<M;x++)
        {
            for (int i=1;i<=N;i++) a.push_back((A[i]+x)%M);
            sort(a.begin(),a.end());long long solution=LLONG_MAX;
            for (int pocetak=1;pocetak<=N;pocetak++)
            {
                long long sol=0;int par=pocetak;
                for (int val:a) {sol+=min(abs(val-nisovac[par]),M-abs(val-nisovac[par]));par++;}
                solution=min(solution,sol);
            }
            answer=min(answer,solution);a.clear();
        }
        return answer;
    }
    /*1161504*/
    if (N<=500)
    {
        long long answer=LLONG_MAX;
        for (int i=1;i<=N;i++) b.push_back(B[i]);
        sort(b.begin(),b.end());sort(A+1,A+N+1);
        for (int i=1;i<=N;i++)
        {
            for (int j=1;j<=N;j++)
            {
                if (A[i]<=B[j]) values.push_back({B[j]-A[i],i});
                else values.push_back({B[j]+M-A[i],i});
            }
        }
        sort(values.begin(),values.end());values.erase(unique(values.begin(),values.end()),values.end());
        for (pair<long long,long long> pomeraj:values)
        {
            long long x=pomeraj.first,mars=pomeraj.second;
            for (int i=1;i<=N;i++) marsovac[i]=(A[i]+x)%M;
            auto it=lower_bound(b.begin(),b.end(),marsovac[mars]);long long pocetak=it-b.begin();
            while (b[pocetak]==marsovac[mars])
            {
                long long pointer=pocetak;long long solution=0;
                for (int pos=mars;pos<=N;pos++)
                {
                    long long sol=min(abs(b[pointer]-marsovac[pos]),M-abs(b[pointer]-marsovac[pos]));
                    solution+=sol;pointer++;if (pointer==(int)b.size()) pointer=0;
                }
                for (int pos=1;pos<mars;pos++)
                {
                    long long sol=min(abs(b[pointer]-marsovac[pos]),M-abs(b[pointer]-marsovac[pos]));
                    solution+=sol;pointer++;if (pointer==(int)b.size()) pointer=0;
                }
                answer=min(answer,solution);pocetak++;
                if (pocetak==(int)b.size()) break;
            }
        }
        return answer;
    }
};