#include <iostream>

using namespace std;

int main()
{
    int n,m,lslsuma=0,lsdsuma=0,dslsuma=0,dsdsuma=0,farba1,farba2;
    cin >> n >> m;
    int a[n];
    int zid1[n][m];
    int zid2[n][m];
    int zid3[n][m];
    int zid4[n][m];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        farba1 = a[i];
        farba2 = a[i];
        if(a[i] == 0)
            farba1 = m;
        if(a[i] == m)
            farba2 = 0;
        for(int j = 0; j < m; j++){
            if(j < farba1){
                zid1[i][j] = 1;
                zid3[i][j] = 0;
            }else{
                zid1[i][j] = 0;
                zid3[i][j] = 1;
            }
            if(j < farba2){
                zid2[i][j] = 1;
                zid4[i][j] = 0;
            }else{
                zid2[i][j] = 0;
                zid4[i][j] = 1;
            }
        }
    }
    int br10,br11,br20,br21,br30,br31,br40,br41,suma1=0,suma2=0,suma3=0,suma4=0;
    for(int j = 0; j < m; j++){
        br10 = 0;
        br11 = 0;
        br20 = 0;
        br21 = 0;
        br30 = 0;
        br31 = 0;
        br40 = 0;
        br41 = 0;
        for(int i = 0; i < n; i++){
            if(zid1[i][j] == 0)
                br10++;
            else
                br11++;
            if(zid2[i][j] == 0)
                br20++;
            else
                br21++;
            if(zid3[i][j] == 0)
                br30++;
            else
                br31++;
            if(zid4[i][j] == 0)
                br40++;
            else
                br41++;
        }
        suma1 += br10*br11;
        suma2 += br20*br21;
        suma3 += br30*br31;
        suma4 += br40*br41;
    }
    cout << min(suma1,min(suma2,min(suma3,suma4)));
}