#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;
}

void whichCommand(int l, int &res){
    vi col_start = col;
    vi col_end = col;

    int extremes_left, extremes_right;
    extremes_left = extremesFromStart(l, col_start, res);
    extremes_right = extremesFromEnd(l, col_end, res);

    res = extremes_right;

    if(extremes_left < extremes_right){
        res = extremes_left;
        col = col_start;
        return;
    }
    if(extremes_right < extremes_left){
        col = col_end;
        return;
    }

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

    col = col_end;
    return;
}


void Solve(){
    int res = 0;
    for(int i = 0; i < N; i++){
        whichCommand(limits[i], res);
    }

    printf("%d", res);
}

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