#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;

#define INF 1000000001
#define ll long long
#define MaxN 1000010

int stack[MaxN];
int pstack;

int n,k;
int a[MaxN];
int l[MaxN];
int d[MaxN];
int val, MIN;

ll ret;

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",&a[i]);
    }

    // prvi veci ili jednak levo
    pstack = 0;
    for (int i = 0; i < n; ++i) {
        val = a[i];

        while ( pstack > 0 && stack[ pstack-1 ] <= val ) {
            pstack--;
        }

        if ( pstack > 0 ) {
            l[i] = stack[ pstack-1 ];
        }
        else {
            l[i] = INF;
        }

        stack[ pstack++ ] = val;
    }

    // prvi veci desno
    pstack = 0;
    for (int i = n-1; i >= 0; --i) {
        val = a[i];

        while ( pstack > 0 && stack[ pstack-1 ] < val ) {
            pstack--;
        }

        if ( pstack > 0 ) {
            d[i] = stack[ pstack-1 ];
        }
        else {
            d[i] = INF;
        }

        stack[ pstack++ ] = val;
    }

    ret = 0LL;
    for (int i = 0; i < n; ++i) {
        val = a[i];
        MIN = min( l[i], d[i] );

        if ( MIN == INF ) {
            ret += (ll)( k - val );
        }
        else {
            if ( l[i] != val ) {
                ret += (ll)( MIN - val );
            }
        }
    }

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

    return 0;
}
