n,D = map(int,input().split())
Time = list(map(int,input().split()))
x = Time[0] #買進時間點1的股票的價格
Q = True #有無股票
k=0 #賺
for i in range(1,len(Time)):
if Q:
if Time[i] >= x+D:
k += (Time[i]-x)
x = Time[i]
Q = False
elif not Q:
if Time[i] <= x-D:
Q = True
x = Time[i]
print(k)