#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
int n,m;
vector<int> in;
struct segTree{
    vector<ll> lazy;
    vector<ll> sum;
    vector<ll> nxt;
    void build(int l,int r,int i)
    {
        for(int j=l;j<=r;j++)
        {
            sum[i]+=(ll)in[j]*(n-in[j]);
            nxt[i]+=(ll)(in[j]+1)*(n-in[j]-1)-(ll)in[j]*(n-in[j]);
        }
        //printf("%i %i %i %lld %lld\n",l,r,i,sum[i],nxt[i]);
        if(l==r)
            return;
        int mi=(l+r)>>1;
        build(l,mi,2*i);
        build(mi+1,r,2*i+1);
    }
    void init()
    {
        sum.resize(4*m);
        lazy.resize(4*m);
        nxt.resize(4*m);
        build(0,m-1,1);
    }
    void prop(int i,int l,int r)
    {
        //printf("Prop %i %i %i %i\n",i,l,r,lazy[i]);
        int m=(l+r)>>1;
        lazy[2*i]+=lazy[i];
        lazy[2*i+1]+=lazy[i];
        if(lazy[i]>0)
        {
            sum[2*i]+=nxt[2*i]*lazy[i];
            sum[2*i]-=(ll)((ll)lazy[i]*(lazy[i]-1))*(m-l+1);
            nxt[2*i]-=(ll)2*lazy[i]*(m-l+1);

            sum[2*i+1]+=nxt[2*i+1]*lazy[i];
            sum[2*i+1]-=(ll)((ll)lazy[i]*(lazy[i]-1))*(r-m);
            nxt[2*i+1]-=(ll)2*lazy[i]*(r-m);
        }
        if(lazy[i]<0)
        {
            lazy[i]*=-1;
            nxt[2*i]+=2*(m-l+1);
            sum[2*i]-=nxt[2*i]*lazy[i];
            sum[2*i]-=(ll)((ll)lazy[i]*(lazy[i]-1))*(m-l+1);
            nxt[2*i]+=2*(m-l+1)*(lazy[i]-1);

            nxt[2*i+1]+=2*(r-m);
            sum[2*i+1]-=nxt[2*i+1]*lazy[i];
            sum[2*i+1]-=(ll)((ll)lazy[i]*(lazy[i]-1))*(r-m);
            nxt[2*i+1]+=2*(r-m)*(lazy[i]-1);
        }
        lazy[i]=0;
    }
    void update(int i)
    {
        sum[i]=sum[2*i]+sum[2*i+1];
        nxt[i]=nxt[2*i]+nxt[2*i+1];
    }
    void inc(int qs,int qe,int l=0,int r=m-1,int i=1)
    {
        if(qs>qe)
            return;
        if(qs>r||qe<l)
            return;
        //printf("Inc %i %i  %i-%i  %i\n",qs,qe,l,r,i);
        if(qs<=l&&qe>=r)
        {
            lazy[i]++;
            sum[i]+=nxt[i];
            nxt[i]-=(ll)2*(r-l+1);
            //printf("Usao !\n");
            return;
        }
        prop(i,l,r);
        int mi=(l+r)>>1;
        inc(qs,qe,l,mi,2*i);
        inc(qs,qe,mi+1,r,2*i+1);
        update(i);
    }
    void dec(int qs,int qe,int l=0,int r=m-1,int i=1)
    {
        if(qs>qe)
            return;
        if(qs>r||qe<l)
            return;
        //printf("Dec %i %i  %i-%i  %i\n",qs,qe,l,r,i);
        if(qs<=l&&qe>=r)
        {
            //printf("%i %lld %lld %lld\n",i,lazy[i],nxt[i],sum[i]);
            lazy[i]--;
            nxt[i]+=2*(r-l+1);
            sum[i]-=nxt[i];
            return;
        }
        prop(i,l,r);
        int mi=(l+r)>>1;
        dec(qs,qe,l,mi,2*i);
        dec(qs,qe,mi+1,r,2*i+1);
        update(i);
    }
    void print(int l=0,int r=m-1,int i=1)
    {
        printf("%i-%i: %i: %lld %lld\n",l,r,i,sum[i],nxt[i]);
        if(l==r)
            return;
        int mi=(l+r)>>1;
        prop(i,l,r);
        print(l,mi,2*i);
        print(mi+1,r,2*i+1);
        update(i);
    }
}st;

int main()
{
	scanf("%i %i",&n,&m);
	vector<int> delta(m+1);
	vector<int> niz(n);
	for(int i=0;i<n;i++)
        scanf("%i",&niz[i]),delta[0]++,delta[niz[i]]--;
    sort(all(niz));
    in.resize(m);
    int tr=0;
    for(int i=0;i<m;i++)
    {
        tr+=delta[i];
        in[i]=tr;
    }
    st.init();
    ll sol=st.sum[1];
    for(int i=0;i<n;i++)
    {
        //printf("%i %i %lld %lld\n",i,niz[i],sol,st.sum[1]);
        st.dec(0,niz[i]-1);
        //printf("%i %i %lld\n",0,niz[i]-1,st.sum[1]);
        //st.print();
        st.inc(niz[i],m-1);
        //printf("%i %i %lld\n",niz[i],m-1,st.sum[1]);
        //st.print();
        sol=min(sol,st.sum[1]);
    }
    printf("%lld\n",sol);
    return 0;
}