#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;

#define PB push_back

int N, M;
vi col, limits;

void input(){
    scanf("%d %d", &N, &M);
    for(int i = 0; i < N; i++){
        int cur;
        scanf("%d", &cur);
        limits.PB(cur);
    }
    for(int i = 0; i < M; i++){
        col.PB(0);
    }
}


int extremesFromStart(int limit, vi &vct, int res){
    for(int i = 0; i < limit; i++){
        res += N - 1 - 2 * vct[i];
        vct[i]++;
    }
    return res;
}

int extremesFromEnd(int limit, vi &vct, int res){
    int i = 0;
    int idx = M - 1;

    while(i < M - limit){
        res += N - 1 - 2 * vct[idx];
        vct[idx]++; i++; idx--;
    }
    return res;
}

bool whichCommand(int l, vi &cs, vi &ce, int &res){
    int extremes_left, extremes_right;
    extremes_left = extremesFromStart(l, cs, res);
    extremes_right = extremesFromEnd(l, ce, res);

    res = extremes_right;

    if(extremes_left < extremes_right){
        res = extremes_left;
        return true;
    }
    if(extremes_right < extremes_left){
        return false;
    }

    if((M - l) >= l){
        // extremes_left equals extremes_right
        return true;
    }

    return false;
}


void Solve(){
    vi col_start, col_end;
    int res = 0;
    for(int i = 0; i < N; i++){
        col_start = col;
        col_end = col;

        if(whichCommand(limits[i], col_start, col_end, res)){
            col = col_start;
        } else {
            col = col_end;
        }
    }

    printf("%d", res);
}

int main()
{
    input();
    Solve();
    return 0;
}