#include <bits/stdc++.h>

using namespace std;

#define MAXN 5001

vector<int> a,b;

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