#include <bits/stdc++.h>

using namespace std;

long long Konflikt(int N,int M,int *A,int *B)
{
	int n = N; int m = M;
	vector<int> a = vector<int>(n);
	vector<int> izm = vector<int>(n);
	vector<int> b = vector<int>(n);
	for	(int i = 0; i < n; i++)
	{
		a[i] = A[i + 1];
		b[i] = B[i + 1];
	}
    sort(b.begin(), b.end());
    long long ans = 1e18;
	for(int i = 0; i < m; i++)
	{
		for(int j = 0; j < n; j++)
		{
			long long ttt = a[j] + i;
			izm[j] = ttt % m;
		}
		sort(izm.begin(), izm.end());
		///for	(int iii = 0; iii < n; iii++)
        ///{
        ///    cout << izm[iii] << " ";
        ///}
        ///cout << "\n";
		for(int j = 0; j < n; j++)
		{
			long long s1 = 0;
			for(int jj = 0; jj < n; jj++)
			{
			    int ind = (jj + j) % n;
				s1 += min(abs(b[jj] - izm[ind]), abs(m - (b[jj] - izm[ind])));
                ///cout << jj << " " << ind << "\n";

			}
			///cout << i << " " << j << " " << s1 << "\n";
			if(s1 < ans) ans = s1;

		}

	}
	return ans;
}