#include <iostream>
#include <cstdio>

// ~1.7s za najgori primer
// valjda je ideja dobra

using namespace std;

const int N = 1 << 20;
const int INF = 1 << 25;
int a[N];
int n, k;

// ----------
// min-segtree

int tree[2 * N];
int treep[2 * N];

void init()
{
    for(int i = 0; i <= N; i++) { tree[i + N] = a[i]; treep[i + N] = i; }
    for(int i = N - 1; i > 0; i--)
        if(tree[2 * i] < tree[2 * i + 1])
            { tree[i] = tree[2 * i]; treep[i] = treep[2 * i]; }
        else
            { tree[i] = tree[2 * i + 1]; treep[i] = treep[2 * i + 1]; }
}

int query(int i, int j, int &pos)
{
    i += N; j += N;
    int res;
    if(tree[i] < tree[j]) { res = tree[i]; pos = treep[i]; }
    else { res = tree[j]; pos = treep[j]; }
    while(i / 2 != j / 2)
    {
        if(i % 2 == 0)
            if(tree[i + 1] < res || (tree[i + 1] == res && treep[i + 1] < pos))
            {
                res = tree[i + 1];
                pos = treep[i + 1];
            }
        if(j % 2 == 1)
            if(tree[j - 1] < res || (tree[j - 1] == res && treep[j - 1] < pos))
            {
                res = tree[j - 1];
                pos = treep[j - 1];
            }
        i /= 2; j /= 2;
    }
    return res;
}

// ----------
// stack, STL se rusi

struct call { int i, j, dk; } ;
call stack[N * 20]; // Valjda je NlogN dosta, ako nije nece ni vreme proci.
int stack_n;

inline void push(call c) { stack[stack_n++] = c; }
inline void pop() { stack_n--; }
inline call top() { return stack[stack_n - 1]; }
inline bool empty() { return stack_n == 0; }

// ----------

long long res;

inline void set_call(call &c, int i, int j, int dk)
{
    c.i = i; c.j = j; c.dk = dk;
}

inline void solve(int i, int j, int dk) // resenje za segment [i,j], vec stavili dk zetona
{
    if(i == j) { res += a[i] - dk; return; }
    if(i > j) return;
    int p;
    int m = query(i,j, p);

    res += m - dk;
    call c;
    set_call(c, i, p - 1, m); push(c);
    set_call(c, p + 1, j, m); push(c);
}

int main()
{
    freopen("zetoni.in","r",stdin);
    freopen("zetoni.out","w",stdout);

    for(int i = 0; i < N; i++) a[i] = INF;
    scanf("%i %i",&n,&k);
    for(int i = 0; i < n; i++)
    {
        scanf("%i",&a[i]);
        a[i] = k - a[i];
    }
    init();

    call c;
    set_call(c, 0, n - 1, 0);
    push(c);
    while(!empty())
    {
        call curr = top();
        pop();
        solve(curr.i, curr.j, curr.dk);
    }

    cout << res << '\n';



    return 0;
}
