#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

const int MAX_N = 1 << 20;
const int INF = 1000000005;

int n;
int k;
int a[MAX_N];
int delta[MAX_N];
int maxIdLeft[MAX_N];
int maxIdRight[MAX_N];
int lessIdLeft[MAX_N];
int lessIdRight[MAX_N];

int sz;
int stk[MAX_N];
queue<int> q;

int main() {
    freopen("zetoni.in", "r", stdin);
    freopen("zetoni.out", "w", stdout);
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    a[0] = INF;
    a[n + 1] = INF;
    sz = 0;
    stk[sz++] = 0;
    memset(maxIdLeft, -1, sizeof(maxIdLeft));
    memset(maxIdRight, -1, sizeof(maxIdRight));
    for (int i = 1; i <= n + 1; ++i) {
        int last = -1;
        int last2 = -1;
        while (sz > 1 && a[stk[sz - 1]] <= a[i]) last2 = last, last = stk[sz - 1], --sz;
        maxIdLeft[i] = last;
        lessIdLeft[i] = last2;
        stk[sz++] = i;
    }
    sz = 0;
    stk[sz++] = n + 1;
    for (int i = n; i >= 0; --i) {
        int last = -1;
        int last2 = -1;
        while (sz > 1 && a[stk[sz - 1]] <= a[i]) last2 = last, last = stk[sz - 1], --sz;
        maxIdRight[i] = last;
        lessIdRight[i] = last2;
        stk[sz++] = i;
    }
    while (!q.empty()) q.pop();
    q.push(0);
    q.push(n + 1);
    long long res = 0;
    memset(delta, 0, sizeof(delta));
    while (!q.empty()) {
        int sta = q.front(); q.pop();
        int end = q.front(); q.pop();
        int added = delta[sta];
        if (sta > end) swap(sta, end);
        if (end <= sta + 1) continue;
        int p = maxIdRight[sta];
        if (p == -1 || p >= end || (maxIdLeft[end] != -1 && maxIdLeft[end] > sta && a[p] < a[maxIdLeft[end]])) p = maxIdLeft[end];
        if (p != -1 && p != sta && p != end) {
            q.push(p);
            q.push(sta);
            q.push(p);
            q.push(end);
            int add = k - a[p] - added;
            delta[p] += add + added;
            res += (long long)add;
        } else if (a[sta] == a[end]) {
            int p = lessIdRight[sta];
            if (p == -1 || p >= end || (lessIdLeft[end] != -1 && lessIdLeft[end] > sta && a[p] < a[lessIdLeft[end]])) p = lessIdLeft[end];
            if (p != -1 && p != sta && p != end) {
                q.push(p);
                q.push(sta);
                q.push(p);
                q.push(end);
                int add = k - a[p] - added;
                delta[p] += add + added;
                res += (long long)add;
            }
        }
    }
    printf("%lld\n", res);
    return 0;
}
