#include <bits/stdc++.h>
using namespace std;
int a[5005], b[5005];
long long Konflikt(int N,int M,int *A,int *B){
    long long best = 7777777, tren = 0;
    for(int i = 1; i<2; i++){
    for(int j = 1; j<=N; j++){
        a[j] = (A[j]+i)%M;
        b[j] = B[j];
    }
    for(int j = 1; j<=N; j++){
        int mn = 67676767, k1;
        for(int k = 1; k<=N; k++){
            if(b[k] == -1) continue;
            if(min(abs(a[j]-b[k]), (a[j]+b[k]+2)%M)<mn){
              //  cout << j << " " << k << " " << abs(a[j]-b[k]) << " " << (a[j]+b[k]+2)%M << endl;
                mn = min(abs(a[j]-b[k]), (a[j]+b[k]+2)%M);
                //cout << mn << " ";
                k1 = k;
            }
        }
        b[k1] = -1;
        //cout << abs(a[1]-b[2]) << endl;
        //cout << mn << " " << j << " " << k1 << endl;
       // cout << "Mn:" << mn << endl;
        tren+=mn;
    }
   // cout << endl << tren << endl;
    if(tren<best) best = tren;
    tren = 0;
    }
    return best;
}