#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

const int MaxN = 1e6 + 16;

int n, k, x, grupe = 1;
bool mark[ MaxN ];
unsigned long long ret;
vector< pair< int, int > > niz;

bool compare( pair< int, int > A, pair< int, int > B )
{
  if ( A.first != B.first ) return A.first > B.first;
  return A.second < B.second;
}

int main()
{
  freopen( "zetoni.in", "r", stdin );
  freopen( "zetoni.out", "w", stdout );
  scanf( "%d %d", &n, &k );
  for ( int i = 0; i < n; i++ )
  {
    scanf( "%d", &x );
    niz.push_back( make_pair( x, i + 1 ) );
  }
  mark[ 0 ] = mark[ n + 1 ] = true;
  sort( niz.begin(), niz.end(), compare );
  for ( int i = 0; i < n; i++ )
  {
    ret += 1LL * grupe * ( k - niz[ i ].first );
    if ( mark[ niz[ i ].second - 1 ] && mark[ niz[ i ].second + 1 ] ) grupe--;
    if ( !mark[ niz[ i ].second - 1 ] && !mark[ niz[ i ].second + 1 ] ) grupe++;
    mark[ niz[ i ].second ] = true;
    k = niz[ i ].first;
  }
  cout << ret << endl;
  return 0;
}
