#include <bits/stdc++.h>
using namespace std;
int main()
{ vector<int>v;
int n,num,sum=0,have=1,start;
cin>>n>>num;
for(int i=0;i<n;++i){
int temp;
cin>>temp;
v.push_back(temp);
}
start=v[0];
for(int i=1;i<n;++i){
if(have==1&&v[i]>=start+num){
sum=sum+v[i]-start;
have=0;
start=v[i];
}
if(have==0&&(v[i]<=start-num)){
have=1;
start=v[i];
}
}
cout <<sum;
return 0;
}