#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
	int N, M;
	std::cin >> N >> M;
	std::vector<int> A(N);
	for (int i = 0; i < N; i++) std::cin >> A[i];
	std::sort(A.begin(), A.end());
	unsigned long long d1, d2;
	std::vector<int> zid(M, 0);
	for (int i = 0; i < A[0]; i++) zid[i]++;
	for (int i = 1; i < N; i++)
	{
		d1 = 0;
		d2 = 0;
		for (int j = 0; j < A[i]; j++) d1 += (N - zid[j] - 1)*(zid[j] + 1);
		for (int j = A[i]; j < M; j++) d1 += (N - zid[j])*zid[j];
		for (int j = 0; j < A[i]; j++) d2 += (N - zid[j])*zid[j];
		for (int j = A[i]; j < M; j++) d2 += (N - zid[j] - 1)*(zid[j] + 1);
		if (d1 < d2) for (int j = 0; j < A[i]; j++) zid[j]++;
		else for (int j = A[i]; j < M; j++) zid[j]++;
	}
	d1 = 0;
	for (int i = 0; i < M; i++) d1 += (N - zid[i])*zid[i];
	std::cout << d1;
	return 0;
}