#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5+12,M = 1e4+14;
ll a[N],kolona[M],n,m;
ll brut(){
    ll sol = 1000000000000000;
    for(long mask = 0; mask < (1 << n); mask++){
        ll sum = 0;
        for(long j = 1; j <= m; j++){
            kolona[j] = 0;
        }
        for(long j = 0; j < n; j++){
            if(mask & (1 << j)){
                for(long h = 1; h <= a[j+1]; h++){
                    kolona[h]++;
                }
            }
            else{
                for(long h = a[j+1]+1; h <= m; h++){
                    kolona[h]++;
                }
            }
        }
        for(long j = 1; j <= m; j++){
            sum += kolona[j]*(n-kolona[j]);
        }
        sol = min(sol,sum);
    }
    return sol;
}
ll dp[N][2],beli0[M],crni0[M],beli1[M],crni1[M],pomc0[M],pomb0[M];
ll getb0(ll l,ll r){
    ll ret = 0;
    for(long i = l; i <= r; i++){
        ret += beli0[i];
    }
    return ret;
}
ll getb1(ll l,ll r){
    ll ret = 0;
    for(long i = l; i <= r; i++){
        ret += beli1[i];
    }
    return ret;
}
ll getc0(ll l,ll r){
    ll ret = 0;
    for(long i = l; i <= r; i++){
        ret += crni0[i];
    }
    return ret;
}
ll getc1(ll l,ll r){
    ll ret = 0;
    for(long i = l; i <= r; i++){
        ret += crni1[i];
    }
    return ret;
}
ll nesto(){
     for(long i = 1; i <= n; i++){
        dp[i][0] = dp[i][1] = 0;
     }
     for(long i = 1; i <= m; i++){
        beli0[i] = beli1[i] = crni0[i] = crni1[i] = 0;
     }
     for(long i = 1; i <= m; i++){
         if(i <= a[1]){
             crni0[i] = 1;
         }
         else{
             beli0[i] = 1;
         }
     }
     for(long i = 1; i <= m; i++){
         if(i <= a[1]){
             beli1[i] = 1;
         }
         else{
             crni1[i] = 1;
         }
     }
     /*cout << endl;
     for(long j = 1; j <= m; j++){
         cout << beli0[j] <<  " ";
     }
     cout << endl;
     for(long j = 1; j <= m; j++){
          cout << crni0[j] <<  " ";
     }
     cout << endl;
     for(long j = 1; j <= m; j++){
         cout << beli1[j] <<  " ";
     }
     cout << endl;
     for(long j = 1; j <= m; j++){
         cout << crni1[j] <<  " ";
     }
     cout << endl << endl;*/
     for(long i = 2; i <= n; i++){
         ll t1 = dp[i-1][0]+getb0(1,a[i])+getc0(a[i]+1,m),t2 = dp[i-1][1]+getb1(1,a[i])+getc1(a[i]+1,m);
         ll t3 = dp[i-1][0]+getc0(1,a[i])+getb0(a[i]+1,m),t4 = dp[i-1][1]+getc1(1,a[i])+getb1(a[i]+1,m);
         //cout << t1 << " " << t2 << " " << t3 << " " << t4 << endl;
         dp[i][0] = min(t1,t2);
         dp[i][1] = min(t3,t4);
         if(t2 <= t1){/// (i-1)-ti je bolje da bojimo desno
            for(long j = 1; j <= m; j++){
                /*beli0[j] = beli1[j];
                crni0[j] = crni1[j];*/
                pomb0[j] = beli1[j];
                pomc0[j] = crni1[j];
                if(j <= a[i]){
                    //crni0[j]++;
                    pomc0[j]++;
                }
                else{
                    //beli0[j]++;
                    pomb0[j]++;
                }
            }
         }
         else{///(i-1)-ti je bolje da bojimo levo
            for(long j = 1; j <= m; j++){
                pomc0[j] = crni0[j];
                pomb0[j] = beli0[j];
                if(j <= a[i]){
                    //crni0[j]++;
                    pomc0[j]++;
                }
                else{
                    //beli0[j]++;
                    pomb0[j]++;
                }
            }
         }
         if(t4 <= t3){///bolje desno
            for(long j = 1; j <= m; j++){
                if(j <= a[i]){
                    beli1[j]++;
                }
                else{
                    crni1[j]++;
                }
            }

         }
         else{///bolje levo
            for(long j = 1; j <= m; j++){
                beli1[j] = beli0[j];
                crni1[j] = crni0[j];
                if(j <= a[i]){
                    beli1[j]++;
                }
                else{
                    crni1[j]++;
                }
            }
         }
         for(long j = 1; j <= m; j++){
            beli0[j] = pomb0[j];
            crni0[j] = pomc0[j];
         }
         /*cout << dp[i][0] << " " << dp[i][1] << endl;
         for(long j = 1; j <= m; j++){
             cout << beli0[j] <<  " ";
         }
         cout << endl;
         for(long j = 1; j <= m; j++){
             cout << crni0[j] <<  " ";
         }
         cout << endl;
         for(long j = 1; j <= m; j++){
             cout << beli1[j] <<  " ";
         }
         cout << endl;
         for(long j = 1; j <= m; j++){
             cout << crni1[j] <<  " ";
         }
         cout << endl << endl;*/
     }
     return min(dp[n][0],dp[n][1]);
}
void read(){
    cin >> n >> m;
    for(long i = 1; i <= n; i++){
        cin >> a[i];
    }
}
void maketest(){
    ll n1 = (rand()%4)+1,m1 = (rand()%4)+1;
    n = n1,m = m1;
    for(long i = 1; i <= n1; i++){
        a[i] = rand()%(m1+1);
    }
}
void stres(ll kolko){
    while(kolko--){
        maketest();
        cout << "novi" << endl;
        ll moj = nesto(),tacan = brut();
        if(moj != tacan){
            cout << "WA " << moj << " " << tacan << endl;
            cout << n << " " << m << endl;
            for(long i = 1; i <= n; i++){
                cout << a[i] << endl;
            }
        }
        cout << "OK" << endl;
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    read();
    if(n <= 15 && m <= 15){
        cout << brut() << endl;
        return 0;
    }
    cout << nesto() << endl;
}
/*
3 4
3
4
1

3 1
1
1
1

4 3
2
0
2
0

4 3
2
0
1
1
!!!!!!!!!!

4 3
2
1
3
0
!!!!!!!!!!
*/