#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5+12,M = 1e4+14;
ll a[N],crni[M],beli[M],n,m;
ll brut(){
    ll sol = 1000000000000000;
    for(long mask = 0; mask < (1 << n); mask++){
        ll sum = 0;
        for(long j = 1; j <= n; j++){
            beli[j] = crni[j] = 0;
        }
        for(long j = 0; j < n; j++){
            if(mask & (1 << j)){
                for(long h = 1; h <= a[j+1]; h++){
                    sum += beli[h];
                    crni[h]++;
                }
                for(long h = a[j+1]+1; h <= m; h++){
                    sum += crni[h];
                    beli[h]++;
                }
            }
            else{
                for(long h = a[j+1]+1; h <= m; h++){
                    sum += beli[h];
                    crni[h]++;
                }
                for(long h = 1; h <= a[j+1]; h++){
                    sum += crni[h];
                    beli[h]++;
                }
            }
        }
        sol = min(sol,sum);
    }
    return sol;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin >> n >> m;
    for(long i = 1; i <= n; i++){
        cin >> a[i];
    }
    if(n <= 15){
        cout << brut() << endl;
        return 0;
    }
}