/*
Petar 'PetarV' Velickovic
Task: Zetoni
*/

#include <stdio.h>
#include <queue>
#include <iostream>
#include <string.h>
#define MAX_N 1000005
#define MAX_ID 3000005
#define LIMIT 1000000
using namespace std;

//ideja: cuvanje svih brojeva u prioritetnom redu + neka vrsta lazy propagacije
//pamcenje indeksa intervala koristeci BIT

char buffer[LIMIT];
int poz=LIMIT;

inline char getnext()
{
       if (poz==LIMIT)
       {
         fread(buffer,1,LIMIT,stdin);
         poz=0;
       }
       return (buffer[poz++]);
}

inline int getint()
{
       int ret = 0;
       char cc;
       while (!isdigit(cc = getnext()));
       while (cc >= '0' && cc <= '9')
       {
         ret *= 10;
         ret += cc - '0';
         cc = getnext();
       }
       return ret;
} 

int n,k,xx;
long long sol=0;
bool endpoint[MAX_N];
int l[MAX_ID], r[MAX_ID];
int prop[MAX_ID];
int idm=0;

int tree[MAX_N];

inline void update(int x, int val)
{
     while (x <= MAX_N-1)
     {
           tree[x]+=val;
           x += (x & -x);
     }
}

inline int read(int x)
{
    int ret=0;
    while (x>0)
    {
      ret+=tree[x];
      x -= (x & -x);
    }
    return ret;
}

struct pq_entry
{
       int num;
       int pos;
       bool operator< (const pq_entry &a) const
       {
            if (num!=a.num) return (num<a.num);
            return (pos<a.pos);
       }
};
priority_queue<pq_entry> pq;

int main()
{
    freopen("zetoni.in","r",stdin);
    freopen("zetoni.out","w",stdout);
    //scanf("%d%d",&n,&k);
    n = getint();
    k = getint();
    endpoint[0]=true;
    endpoint[n+1]=true;
    l[0]=1;
    r[0]=n;
    pq_entry hm;
    for (int i=1;i<=n;i++)
    {
        //scanf("%d",&xx);
        xx = getint();
        hm.pos=i;
        hm.num=xx;
        pq.push(hm);
    }
    while (!pq.empty())
    {
        pq_entry pt=pq.top();
        pq.pop();
        int nn=pt.num;
        int pp=pt.pos;
        int id=read(pp);
        //printf("Splitujem na:\n");
        //printf("Pozicija: %d\n",pp);
        //printf("Interval: %d\n",id);
        //printf("------------\n");
        int dsol=k-nn-prop[id];
        if (dsol<0) dsol=0;
        sol+=dsol;
        endpoint[pp]=true;
        if (endpoint[pp-1]) 
        {
          l[id]=pp+1;
          prop[id]+=dsol;
          continue;
        }
        else if (endpoint[pp+1])
        {
          r[id]=pp-1;
          prop[id]+=dsol;
          continue;
        }
        else
        {
          //split
          int left=l[id];
          int right=r[id];
          
          //uklanjanje prethodnog intervala
          update(left,-id);
          update(right+1,id);
          
          //identifikacija novog levog intervala
          update(left,idm+1);
          update(pp,-idm-1);
          
          //identifikacija novog desnog intervala
          update(pp+1,idm+2);
          update(right+1,-idm-2);
                    
          l[idm+1]=left;
          r[idm+1]=pp-1;
          prop[idm+1]=prop[id]+dsol;
          
          l[idm+2]=pp+1;
          r[idm+2]=right;
          prop[idm+2]=prop[id]+dsol;
          
          idm+=2;
        }        
    }
    printf("%lld\n",sol);
    //scanf("%d",&n);
    return 0;
}
