#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <algorithm>

#define MAXN 1000000

using namespace std;

int n,k,a[MAXN+1];

long long solve(int first,int last)
{
     
  //   for(int i=first;i<=last;i++)
   // printf("%d ",a[i]);
  //  printf("\n"); 
       if(last-first>1)
       {
                       long long potezi=0;
                       int min=a[first],ff=first;
     
                       for(int i=first;i<=last;i++)
                               if(a[i]<min) min=a[i];
     
                       for(int i=first;i<=last;i++)
                       {
                               a[i]-=min;
                               if(a[i]==0) 
                               { 
                                   potezi+=solve(ff,i-1);
                                   ff=i+1;
                               }
                               else if(i==last)
                                    potezi+=solve(ff,i);
                       }
                       potezi+=min;
                        //printf("firstlast: %d %d potezi:%d",first,last,potezi);
                        //printf("\n"); 
                       return potezi;
     }
     else if(last-first==1) return max(a[last],a[first]);
     else return a[first];
    
}

int main(int argc, char *argv[])
{
     freopen("zetoni.in","r",stdin);
    freopen("zetoni.out","w",stdout);
    scanf("%d %d",&n,&k);
    for(int i=0;i<=n-1;i++)
    {
            scanf("%d",&a[i]);
            a[i]=k-a[i];
    }
    long long ret=solve(0,n-1);
    printf("%I64lld",ret);
    //system("PAUSE");
    return 0;
}
