#include <iostream>

using namespace std;

int main()
{
    int n, m;
    int dosada = 0;
    int dosada_levo = 0;
    int dosada_desno = 0;
    int a = 0;

    cin >> n >> m;

    int x0[m], x1[m];
    int x0_levo[m], x1_levo[m];
    int x0_desno[m], x1_desno[m];

    for (int j = 0; j < m; j++)
    {
        x0[j] = 0;
        x1[j] = 0;
        x0_levo[j] = 0;
        x1_levo[j] = 0;
        x0_desno[j] = 0;
        x1_desno[j] = 0;
    }

    // BE GREEDY
    for (int i = 0; i < n; i++)
    {
        cin >> a;

        dosada_levo = 0;
        dosada_desno = 0;
        for (int j = 0; j < m; j++)
        {
            x1_levo[j] = x1[j];
            x0_levo[j] = x0[j];
            if (j < a)
            {
               x1_levo[j]++;
            }
            else
            {
                x0_levo[j]++;
            }
            dosada_levo += x0_levo[j] * x1_levo[j];

            x1_desno[j] = x1[j];
            x0_desno[j] = x0[j];
            if (a == m)
            {
                x0_desno[j]++;
            }
            else if (j < m - a)
            {
                x0_desno[j]++;
            }
            else
            {
                x1_desno[j]++;
            }
            dosada_desno += x0_desno[j] * x1_desno[j];
        }

        if (dosada_levo <= dosada_desno)
        {
            for (int j = 0; j < m; j++)
            {
                x0[j] = x0_levo[j];
                x1[j] = x1_levo[j];
            }
        }
        else
        {
            for (int j = 0; j < m; j++)
            {
                x0[j] = x0_desno[j];
                x1[j] = x1_desno[j];
            }
        }
    }

    dosada = 0;
    for (int j = 0; j < m; j++)
    {
        dosada += x0[j] * x1[j];
    }

    cout << dosada << endl;
    return 0;
}