#include <bits/stdc++.h>
#define endl '\n'
#pragma GCC optimize("Ofast")
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair < int, int >;
using pll = pair < ll, ll >;
const ll INF = 2e18;
const ll DIM = 1000007;
const ld PI = 3.1415926535;
const int mod = 999999893;
class convexFunction {
public:
void init() {
C = 0;
L = {}; modL = 0;
R = {}; modR = 0;
}
void expand(ll h) {
if(L.size() + R.size() != 0) {
modL -= h;
modR += h;
}
}
void addAbs(ll p) {
if(L.size() + R.size() == 0) {
L.insert(p);
R.insert(p);
C = 0;
return;
}
if(*L.rbegin() + modL <= p && p <= *R.begin() + modR) {
L.insert(p - modL);
R.insert(p - modR);
}
else if(p > *R.begin() + modR) {
C += (p - (*R.begin() + modR));
L.insert(*R.begin() + modR - modL);
R.extract(*R.begin());
R.insert(p - modR);
R.insert(p - modR);
}
else if(p < *L.rbegin() + modL) {
C += ((*L.rbegin() + modL) - p);
R.insert(*L.rbegin() + modL - modR);
L.extract(*L.rbegin());
L.insert(p - modL);
L.insert(p - modL);
}
}
ll getMi() {
return C;
}
private:
multiset < ll > L, R;
ll modL, modR;
ll C;
};
int main() {
cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
#ifdef IloveCP
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n, h;
cin >> n >> h;
convexFunction f;
f.init();
for(int i = 0; i < n; i++) {
ll x;
cin >> x;
f.expand(h);
f.addAbs(x);
}
cout << f.getMi() << endl;
return 0;
}