#include <bits/stdc++.h>
#define NMAX 100008
#define MMAX 10008
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
#define forn(a,b,c) for(int a=b;a<c;a++)
#define ford(a,b,c) for(int a=b;a>=c;a--)
int n,m;
int k[NMAX], a[NMAX];
ll ans;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    forn(i,0,m)
        k[i] = 0;
    forn(i,0,n)
        cin >> a[i];
    forn(i,0,a[0])
        k[i]++;
    ans = 0;
    forn(i,1,n)
    {
        ll ans1 = 0;
        ll ans2 = 0;
        forn(j,0,m)
        {
            if(j < a[i])
            {
                ans1 += (ll)(k[j] + 1) * (ll)(i - k[j]);
                ans2 += (ll)k[j] * (ll)(i + 1 - k[j]);
            }
            else
            {
                ans2 += (ll)(k[j] + 1) * (ll)(i - k[j]);
                ans1 += (ll)k[j] * (ll)(i + 1 - k[j]);
            }
        }
        ans = min(ans1,ans2);
        if(ans1 <= ans2)
        {
            forn(j,0,a[i])
                k[j]++;

        }
        else
        {
            forn(j,a[i],m)
                k[j]++;

        }
    }
    cout << ans;
    return 0;
}