#include <iostream>

using namespace std;

typedef long long int ll;

struct nod {

    int v;
    bool l;
};

bool ld[20];
int minn = 2000000000;

ll calc(int n, int m, nod x[]) {

    int o[m], i, j;

    for(i = 0; i < m; i++)
        o[i] = 0;

    for(i = 0; i < n; i++) {
        if(x[i].l)
            for(j = 0; j < x[i].v; j++)
                o[j]++;
        else
            for(j = m - x[i].v; j < m; j++)
                o[j]++;
    }

    ll d = 0;
    for(i = 0; i < m; i++)
        d += (o[i] * (n - o[i]));

    return d;
}

int calc2(int a[], int n, int m, bool x[]) {

    int o[m], i, j;

    for(i = 0; i < m; i++)
        o[i] = 0;

    for(i = 0; i < n; i++) {
        if(x[i])
            for(j = 0; j < a[i]; j++)
                o[j]++;
        else
            for(j = m - 1; j >= a[i]; j--)
                o[j]++;
    }

    int d = 0;
    for(i = 0; i < m; i++)
        d += (o[i] * (n - o[i]));

    return d;
}

void f(int a[], int n, int m, int d) {

    if(d == n) {
        int pr = calc2(a, n, m, ld);
        if(pr < minn) {
            minn = pr;
        }
        return;
    }

    ld[d] = true;
    f(a, n, m, d + 1);
    ld[d] = false;
    f(a, n, m, d + 1);
}

void dvaes_bodova(int n, int m) {

    int a[n];
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }

    f(a, n, m, 0);

    cout << minn << endl;
}

int main() {

    int n, m;

    cin >> n >> m;

    if(n <= 15 && m <= 15) {
        dvaes_bodova(n, m);
        return 0;
    }

    // prop'o pokusaj
    nod vl[n], vd[n], ml[n], md[n];

    bool j = false;

    for(int i = 0; i < n; i++) {
        int a;
        cin >> a;
        if(a > m - a) {
            vl[i].v = vd[i].v = a;
            vl[i].l = vd[i].l = true;
            ml[i].v = md[i].v = m - a;
            ml[i].l = md[i].l = false;
        }

        if(a < m - a) {
            vl[i].v = vd[i].v = m - a;
            vl[i].l = vd[i].l = false;
            ml[i].v = md[i].v = a;
            ml[i].l = md[i].l = true;
        }

        if(a == m - a) {
            vl[i].v = a;
            vl[i].l = true;
            vd[i].v = a;
            vd[i].l = false;
            ml[i].v = a;
            ml[i].l = true;
            md[i].v = a;
            md[i].l = false;
            j = true;
        }
    }

    //
    int r = calc(n, m, ml);
    int r2 = calc(n, m, vl);
    if(r2 < r)
        r = r2;
    if(j) {
        r2 = calc(n, m, md);
        if(r2 < r)
            r = r2;
        r2 = calc(n, m, vd);
        if(r2 < r)
            r = r2;
    }

    cout << r << endl;
}