/*
    Aleksandar "Al3kSaNdaR" Ivanovic

    zetoni
*/
#include <cstdio>
#include <queue>

using namespace std;

typedef long long int lld;

const int MaxN = 1000000 + 5;

int N, K, Cnt[MaxN];

priority_queue < int > PQ, __PQ;

int main ( void )
{
    freopen ( "zetoni.in", "r", stdin );
    //freopen ( "zetoni.out", "w", stdout );

    scanf ( "%d %d", &N, &K );

    for ( int i = 0; i < N; i++ ) scanf ( "%d", &Cnt[i] );

    PQ.push ( Cnt[0] );

    lld Sol = K - Cnt[0];

    for ( int i = 1; i < N; i++ )
    {
        int Need = K - Cnt[i];

        int Have = K - PQ.top ( );

        PQ.push ( Cnt[i] );

        if ( Have < Need ) Sol += Need - Have;
    }

    __PQ.push ( Cnt[N - 1] );

    lld __Sol = K - Cnt[N - 1];

    for ( int i = N - 2; i >= 0; i-- )
    {
        int Need = K - Cnt[i];

        int Have = K - __PQ.top ( );

        __PQ.push ( Cnt[i] );

        if ( Have < Need ) __Sol += Need - Have;
    }

    if ( __Sol < Sol ) Sol = __Sol;

    printf ( "%lld\n", Sol );

    return 0;
}
