#include <bits/stdc++.h>

using namespace std;

#define INFI 987654321
#define INFL 987654321987654321
#define MOD 1000000007

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T>
void read(T, long long);

template<typename T>
void write(T, long long);

template<typename T>
long long sumFor(T, long long, long long);

template<typename T>
long long sumFor(T, long long);

long long power(long long, long long, long long);
long long power(long long, long long);
void add(long long&, long long&);
long long getRand();
long long max(long long, long long, long long);

/// //MAIN// ///

int a[10005];
long long dp[100005][2];
long long lastrow[10005][2]; ///broj ofarbanih u poslednjem redu za svaku kolonu

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    long long n, m;
    cin >> n >> m;
    read(a, n);

    dp[0][0] = 0;
    dp[0][1] = 0;
    for(long long i = 0; i < m; ++i){
        if(i < a[0]){
            lastrow[i][0] = 1;
        }
    }
    for(long long i = 0; i < m; ++i){
        if(i < m - a[0]){
            lastrow[m - i - 1][1] = 1;
        }
    }

    for(long long i = 1; i < n; ++i){
        ///build dp[i][0]
        long long sum1 = 0;
        long long sum2 = 0;

        for(long long j = 0; j < m; ++j){
            if(j < a[i]){
                sum1 += (lastrow[j][0] + 1) * (i + 1 - lastrow[j][0] - 1);
            }else{
                sum1 += (lastrow[j][0]) * (i + 1 - lastrow[j][0]);
            }
        }
        for(long long j = 0; j < m; ++j){
            if(j < a[i]){
                sum2 += (lastrow[j][1] + 1) * (i + 1 - lastrow[j][1] - 1);
            }else{
                sum2 += (lastrow[j][1]) * (i + 1 - lastrow[j][1]);
            }
        }

        ///endbuild dp[i][0]
        ///build dp[i][1]
        long long sum3 = 0;
        long long sum4 = 0;

        for(long long j = 0; j < m; ++j){
            if(j < m - a[i]){
                sum3 += (lastrow[m - j - 1][0] + 1) * (i + 1 - lastrow[m - j - 1][0] - 1);
            }else{
                sum3 += (lastrow[m - j - 1][0]) * (i + 1 - lastrow[m - j - 1][0]);
            }
        }
        for(long long j = 0; j < m; ++j){
            if(j < m - a[i]){
                sum4 += (lastrow[m - j - 1][1] + 1) * (i + 1 - lastrow[m - j - 1][1] - 1);
            }else{
                sum4 += (lastrow[m - j - 1][1]) * (i + 1 - lastrow[m - j - 1][1]);
            }
        }


        ///endbuild dp[i][1]
        if(sum1 <= sum2){
            dp[i][0] = sum1;
            for(long long j = 0; j < m; ++j){
                if(j < a[i]){
                    lastrow[j][0]++;
                }
            }
        }else{
            dp[i][0] = sum2;
            for(long long j = 0; j < m; ++j){
                if(j < a[i]){
                    lastrow[j][0] = lastrow[j][1] + 1;
                }else{
                    lastrow[j][0] = lastrow[j][1];
                }
            }
        }
        if(sum3 <= sum4){
            dp[i][1] = sum3;
            for(long long j = 0; j < m; ++j){
                if(j < m - a[i]){
                    lastrow[m - j - 1][1] = lastrow[m - j - 1][0] + 1;
                }else{
                    lastrow[m - j - 1][1] = lastrow[m - j - 1][0];
                }
            }
        }else{
            dp[i][1] = sum4;
            for(long long j = 0; j < m; ++j){
                if(j < m - a[i]){
                    lastrow[m - j - 1][1] = lastrow[m - j - 1][1] + 1;
                }else{
                    lastrow[m - j - 1][1] = lastrow[m - j - 1][1];
                }
            }
        }
    }

    cout << min(dp[n-1][1], dp[n-1][0]) << "\n";
    return 0;
}
/*
2 3
1
2
*/

/// /////////////////////////////////////////////

long long max(long long m_a, long long m_b, long long m_c){
    if(m_c >= m_a && m_c >= m_b){
        return m_c;
    }
    return m_a >= m_b ? m_a : m_b;
}

long long getRand(){
    return rng();
}

void add(long long& m_a, long long& m_b){
    m_a = (m_a + m_b) % MOD;
}

template<typename T>
void write(T m_pnter, long long m_size){
    for(long long i = 0; i < m_size; ++i){
        cout << m_pnter[i] << " ";
    }
    cout << "\n";
}

template<typename T>
void read(T m_pnter, long long m_size){
    for(long long i = 0; i < m_size; ++i){
        cin >> m_pnter[i];
    }
}

template<typename T>
long long sumFor(T m_pnter, long long m_l, long long m_r){
    long long m_sum = 0;
    for(long long i = m_l; i < m_r; ++i){
        m_sum += m_pnter[i];
    }
    return m_sum;
}

template<typename T>
long long sumFor(T m_pnter, long long m_r){
    long long m_sum = 0;
    for(long long i = 0; i < m_r; ++i){
        m_sum += m_pnter[i];
    }
    return m_sum;
}

long long power(long long m_a, long long m_b, long long m_mod){
    long long m_res = 1;
    while(m_b > 0){
        if(m_b % 2 == 1){
            m_res = (m_res * m_a) % m_mod;
        }
        m_b /= 2;
        m_a = (m_a * m_a) % m_mod;
    }
    return m_res;
}

long long power(long long m_a, long long m_b){
    long long m_res = 1;
    while(m_b > 0){
        if(m_b % 2 == 1){
            m_res = m_res * m_a;
        }
        m_b /= 2;
        m_a = m_a * m_a;
    }
    return m_res;
}