#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<deque>
#include<algorithm>
#include<utility>
#include<stack>
#include<set>
#include<cstring>
#include<vector>
using namespace std;



long long int SOL=0;
int n,k;
int a[123456];





void smart()
{    
     a[0]=0;
     a[n+1]=0;
     int mmax=-1;
     for (int i=1;i<=n;i++) 
     {
         if (a[i]==-2) a[i]=k;
         if (a[i-2]==-2) a[i-2]=k;
         if (a[i-1]==k) a[i-1]=-2;
         if (a[i+1]==k) a[i+1]=-2;
         if (a[i]>mmax) mmax=a[i];
         int TT=max(a[i-1]-a[i],a[i+1]-a[i]);
         if (TT>0) SOL+=TT;
     }
     a[0]=k;
     a[n+1]=k;
     int LAST=0;
     mmax=0;
     for (int i=1;i<=n+1;i++)
     {
         if (a[i]==k)
         {
            SOL+=(k-mmax);
            mmax=0;
            LAST=i;
         }
         if (a[i]>mmax) mmax=a[i];
     }



     
}




void BRUTE(int L,int R,int DIFF)
{
   if (R-L!=1)
   
   {
        int MAX=-1;
        int POZ=-1;
        for (int i=L+1;i<R;i++) a[i]+=DIFF;
        for (int i=L+1;i<R;i++)
   {
        if (a[i]>MAX) 
        {
        POZ=i;
        MAX=a[i];
        }
    } 
    SOL+=(k-MAX);
    DIFF=k-MAX;
    BRUTE(L,POZ,DIFF);
    BRUTE(POZ,R,DIFF);
}
}


int main()
{
    freopen("zetoni.in","r",stdin);
   freopen("zetoni.out","w",stdout);
    scanf("%d %d",&n,&k);
    a[0]=k;
    a[n+1]=k;
 for (int i=1;i<=n;i++)  scanf("%d",&a[i]);
     
     
 
 if (n<=1) BRUTE(1,n+1,0);
 else smart();
 
 cout<<SOL<<endl;

 return 0;   
}
