#include <iostream>
#include<bits/stdc++.h>
#define For(a,b,c) for(int a=b;a<=c;a++)
#define Ford(a,b,c) for(int a=b;a>=c;a--)
#define ll long long
#define pb push_back
const int MAX=1e5+2;
using namespace std;

int n,m,a[MAX],b1[MAX],c1[MAX];
ll rez=(ll)0,glob=1e18,t=(ll)0,tmp,rez1[MAX],rez2[MAX],sum1=0,sum2=0,seg[4*MAX+4],lazy[4*MAX+4];


void Update(int l,int r,int start,int kraj,int node,int val)
{
    if (lazy[node]!=0)
    {
        seg[node]+=(kraj-start+1)*lazy[node];
        if (start!=kraj)
        lazy[2*node]+=lazy[node],lazy[2*node+1]+=lazy[node];
        lazy[node]=0;
    }

    if (start>kraj || l>kraj || r<start) return;

    int mid=(start+kraj)/2;

    if (start>=l && kraj<=r)
    {
        seg[node]+=(ll)(kraj-start+1)*val;
        if (start!=kraj)
        lazy[2*node]+=(ll)val,lazy[2*node+1]+=val;
    }
    else
    {
        Update(l,r,start,mid,2*node,val);
        Update(l,r,mid+1,kraj,2*node+1,val);
        seg[node]=seg[2*node]+seg[2*node+1];
    }

}

ll Query(int l,int r,int start,int kraj,int node)
{
    if (lazy[node]!=(ll)0)
    {
        seg[node]+=(ll)(kraj-start+1)*lazy[node];
        if (start!=kraj)
        lazy[2*node]+=lazy[node],lazy[2*node+1]+=lazy[node];
        lazy[node]=(ll)0;
    }
    if (start>kraj || l>kraj || r<start) return (ll)0;

    int mid=(start+kraj)/2;
    if (start>=l && kraj<=r)
    {
        return seg[node];
    }
    return Query(l,r,start,mid,2*node)+Query(l,r,mid+1,kraj,2*node+1);

}

int main()
{

    cin>>n>>m;


    For(i,1,m)
    {
        c1[i]=b1[i]=0;
    }
    For(i,1,n)
    {
        scanf("%i",&a[i]);
        b1[a[i]]+=1;
    }
      if (m<=15 && n<=15)
    {
        int t=0;
        For(i,0,(1<<n-1))
        {   rez=0;
            For(j,1,m)
            {
               t=0;
               For(k,1,n)
               if ((i&(1<<(k-1)) && j<=a[k]) || (!(i&(1<<(k-1))) && j>a[k]))
                    t++;
                rez+=t*(n-t);
            }
            glob=min(glob,rez);
        }
        cout<<glob;
        return 0;
    }

    sort(a+1,a+n+1);
    c1[m]=b1[m];
    Ford(i,m-1,1)
    {
        c1[i]=c1[i+1]+b1[i];

    }
   // cout<<c1[1]<<" "<<c1[2]<<endl;
    For(i,1,m)
    {
        sum1+=(ll)c1[i]*n;
    }
    For(i,1,m)
    {
        sum2+=(ll)c1[i]*c1[i];
    }
    rez1[n+1]=sum1-sum2;
    rez2[n+1]=0;

    For(i,1,m)
    Update(i,i,1,m,1,c1[i]);

    Ford(i,n,1)
    {
        sum2-=((ll)2*Query(1,a[i],1,m,1)-(ll)a[i]);
        Update(1,a[i],1,m,1,-1);
        sum1-=(ll)n*a[i];

        rez1[i]=sum1-sum2;
    }

    For(i,1,4*MAX+1)
    seg[i]=0,lazy[i]=0;
    sum1=sum2=0;

    Ford(i,n,1)
    {

        sum2+=((ll)2*Query(1,m-a[i],1,m,1)+(ll)(m-a[i]));
        Update(1,m-a[i],1,m,1,1);
        sum1+=(ll)n*(m-a[i]);

        rez2[i]=sum1-sum2;
    }
    /*For(i,1,n+1)
    printf("%I64d %I64d\n",rez1[i],rez2[i]);*/
    rez=rez1[1]+rez2[1];
    For(i,1,n+1)
    rez=min(rez,rez1[i]+rez2[i]);
    cout<<rez;

    return 0;
}