Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad.

The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions $a$, $b$ and $c$ respectively. At the end of the performance, the distance between each pair of ropewalkers was at least $d$.

Ropewalkers can walk on the rope. In one second, only one ropewalker can change his position. Every ropewalker can change his position exactly by $1$ (i. e. shift by $1$ to the left or right direction on the rope). Agafon, Boniface and Konrad can not move at the same time (Only one of them can move at each moment). Ropewalkers can be at the same positions at the same time and can "walk past each other".

You should find the minimum duration (in seconds) of the performance. In other words, find the minimum number of seconds needed so that the distance between each pair of ropewalkers can be greater or equal to $d$.

Ropewalkers can walk to negative coordinates, due to the rope is infinite to both sides.


-----Input-----

The only line of the input contains four integers $a$, $b$, $c$, $d$ ($1 \le a, b, c, d \le 10^9$). It is possible that any two (or all three) ropewalkers are in the same position at the beginning of the performance.


-----Output-----

Output one integer — the minimum duration (in seconds) of the performance.


-----Examples-----
Input
5 2 6 3

Output
2

Input
3 1 5 6

Output
8

Input
8 3 3 2

Output
2

Input
2 3 10 4

Output
3



-----Note-----

In the first example: in the first two seconds Konrad moves for 2 positions to the right (to the position $8$), while Agafon and Boniface stay at their positions. Thus, the distance between Agafon and Boniface will be $|5 - 2| = 3$, the distance between Boniface and Konrad will be $|2 - 8| = 6$ and the distance between Agafon and Konrad will be $|5 - 8| = 3$. Therefore, all three pairwise distances will be at least $d=3$, so the performance could be finished within 2 seconds.

Difficulty: EASY

Solutions:
(a, b, c, d) = map(int, input().split())
s = sorted([a, b, c])
ans = 0
if s[2] - s[1] < d:
	ans += d - (s[2] - s[1])
if s[1] - s[0] < d:
	ans += d - (s[1] - s[0])
print(ans)


(a, b, c, d) = map(int, input().split())
a = [a, b, c]
a.sort()
ans = 0
if a[2] - a[1] < d:
	ans += d - (a[2] - a[1])
if a[1] - a[0] < d:
	ans += d - (a[1] - a[0])
print(ans)


(a, b, c, d) = map(int, input().split())
total = 0
s = []
s.append(a)
s.append(b)
s.append(c)
s.sort()
a = s[0]
b = s[1]
c = s[2]
a = min(s)
c = max(s)
distance_max = abs(c - b)
distance_min = abs(a - b)
if distance_max < d:
	total += d - distance_max
if distance_min < d:
	total += d - distance_min
print(total)


(a, b, c, d) = list(map(int, input().split()))
x = []
x.append(a)
x.append(b)
x.append(c)
x.sort()
z = 0
if x[1] - x[0] <= d:
	z += d - (x[1] - x[0])
if x[2] - x[1] <= d:
	z += d - (x[2] - x[1])
print(z)


(a, b, c, d) = map(int, input().split())
lst = [a, b, c]
lst.sort()
s = 0
if lst[2] - lst[1] < d:
	s += d - lst[2] + lst[1]
if lst[1] - lst[0] < d:
	s += d - lst[1] + lst[0]
print(s)


(a, b, c, d) = [int(x) for x in input().split()]
mini = min(a, b, c)
maxi = max(a, b, c)
middle = a + b + c - mini - maxi
total = max(0, d - (middle - mini)) + max(0, d - (maxi - middle))
print(total)


import sys, math, string
input = sys.stdin.readline
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
(a, b, c, d) = M()
l = [a, b, c]
l.sort()
x = 0
if l[1] - l[0] < d:
	x += d - (l[1] - l[0])
if l[2] - l[1] < d:
	x += d - (l[2] - l[1])
print(x)


(a, b, c, d) = map(int, input().split())
pos = sorted((a, b, c))
print(max(0, d - pos[2] + pos[1]) + max(0, d - pos[1] + pos[0]))


(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted([a, b, c])
print(max(0, d - b + a) + max(0, d - c + b))


inp = list(map(int, input().split()))
d = inp[3]
inp.remove(d)
inp.sort()
a = inp[1] - inp[0]
b = inp[2] - inp[1]
count = 0
if a < d:
	count = count + (d - a)
if b < d:
	count = count + (d - b)
print(count)


(a, b, c, d) = map(int, input().split())
sol = 0
ar = []
ar.append(a)
ar.append(b)
ar.append(c)
ar.sort()
d1 = ar[1] - ar[0]
d2 = ar[2] - ar[1]
if d1 < d:
	sol += d - d1
if d2 < d:
	sol += d - d2
print(sol)


(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
d1 = arr[1] - arr[0]
d2 = arr[2] - arr[1]
if d1 >= d and d2 >= d:
	print(0)
elif d1 >= d:
	print(d - d2)
elif d2 >= d:
	print(d - d1)
else:
	val = 2 * d - (d1 + d2)
	print(val)


data = list(map(int, input().split(' ')))
d = data.pop()
data.sort()
ans = 0
if data[1] - data[0] < d:
	ans += d - (data[1] - data[0])
if data[2] - data[1] < d:
	ans += d - (data[2] - data[1])
print(ans)


(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
ans = 0
ans += max(0, d - (l[1] - l[0]))
ans += max(0, d - (l[2] - l[1]))
print(ans)


arr = [int(x) for x in input().split()]
a = arr[0]
b = arr[1]
c = arr[2]
d = arr[3]
arr2 = [0 for i in range(3)]
arr2[0] = a
arr2[1] = b
arr2[2] = c
arr2.sort()
d1 = arr2[1] - arr2[0]
d2 = arr2[2] - arr2[1]
suma = 0
if d1 < d:
	suma += d - d1
if d2 < d:
	suma += d - d2
print(suma)


(a, b, c, d) = map(int, input().split())
r = [a, b, c]
move = 0
r.sort()
move += d - abs(r[0] - r[1]) if d - abs(r[0] - r[1]) > 0 else 0
move += d - abs(r[1] - r[2]) if d - abs(r[1] - r[2]) > 0 else 0
print(move)


l = list(map(int, input().split()))
d = l[-1]
l.remove(d)
l = sorted(l)
p = l[1] - l[0]
q = l[2] - l[1]
c = 0
if p < d:
	c += d - p
if q < d:
	c += d - q
print(c)


(a, b, c, k) = input().split()
k = int(k)
(a, b, c) = sorted([int(a), int(b), int(c)])
s = 0
if b - a < k:
	s += abs(b - a - k)
if c - b < k:
	s += abs(c - b - k)
print(s)


a = list(map(int, input().split()))
arr = a[0:3]
arr.sort()
dist = a[3]
count = 0
if abs(arr[1] - arr[0]) >= dist:
	pass
else:
	count += dist - abs(arr[1] - arr[0])
if abs(arr[2] - arr[1]) >= dist:
	pass
else:
	count += dist - abs(arr[2] - arr[1])
print(count)


import math
(a, b, c, d) = [int(x) for x in input().split()]
ls = [a, b, c]
ls.sort()
left = ls[1] - ls[0]
right = ls[2] - ls[1]
seconds = 0
if left < d:
	seconds = seconds + (d - left)
if right < d:
	seconds = seconds + (d - right)
print(seconds)


(a, b, c, d) = [int(x) for x in input().split()]
m = [a, b, c]
(time, time2) = (0, 0)
m.sort()
if m[1] - m[0] < d:
	time = d - (m[1] - m[0])
if m[2] - m[1] < d:
	time2 = d - (m[2] - m[1])
total = time + time2
print(total)


[a, b, c, d] = list(map(int, input().split(' ')))
if a > b:
	[a, b] = [b, a]
if b > c:
	[b, c] = [c, b]
if a > b:
	[b, a] = [a, b]
t = 0
if b + d > c:
	t = t + b + d - c
if b < d + a:
	t = t + d + a - b
print(t)


abcd = input().split(' ')
arr = []
for i in range(3):
	arr.append(int(abcd[i]))
d = int(abcd[3])
arr = sorted(arr)
cnt = 0
if arr[2] - arr[1] < d:
	cnt += d - (arr[2] - arr[1])
if arr[1] - arr[0] < d:
	cnt += d - (arr[1] - arr[0])
print(cnt)


X = list(map(int, input().split()))
POS = sorted(X[:3])
print(max(X[3] - (POS[1] - POS[0]), 0) + max(X[3] - (POS[2] - POS[1]), 0))


(a, b, c, d) = map(int, input().split(' '))
l = [a, b, c]
l.sort()
t = 0
if d - (l[1] - l[0]) > 0:
	t += d - (l[1] - l[0])
if d - (l[2] - l[1]) > 0:
	t += d - (l[2] - l[1])
print(t)


(a, b, c, d) = list(map(int, input().split()))
(a, b, c) = sorted([a, b, c])
res = 0
if a > b - d:
	res += a - (b - d)
if c < b + d:
	res += b + d - c
print(res)


def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def si():
	return input()
(a, b, c, d) = mi()
x = [a, b, c]
x.sort()
c1 = x[1] - x[0]
c2 = x[2] - x[1]
c1 = d - c1
c2 = d - c2
ans = 0
if c1 > 0:
	ans += c1
if c2 > 0:
	ans += c2
print(ans)


from sys import stdin

def main():
	nums = list(map(int, stdin.readline().split()))
	d = nums[3]
	nums = nums[:3]
	nums.sort()
	seconds = 0
	if nums[2] - nums[1] < d:
		seconds += d - (nums[2] - nums[1])
	if nums[1] - nums[0] < d:
		seconds += d - (nums[1] - nums[0])
	print(seconds)
main()


p = input().rstrip().split(' ')
f = p[0:3]
X = 0
d = int(p[3])
f.sort(key=int)
A = abs(int(f[1]) - int(f[2]))
if A < d:
	X += d - A
B = abs(int(f[1]) - int(f[0]))
if B < d:
	X += d - B
print(X)


q = 1
for _ in range(q):
	l = list(map(int, input().split()))
	d = l.pop()
	l.sort()
	x = 0
	if l[1] - l[0] < d:
		x += l[0] - (l[1] - d)
	if l[2] - l[1] < d:
		x += l[1] + d - l[2]
	print(x)


(a, b, c, d) = map(int, input().split())
e = []
e.append(a)
e.append(b)
e.append(c)
e.sort()
s = 0
if e[2] - e[1] < d:
	s = e[1] + d - e[2]
if e[1] - e[0] < d:
	s = s + e[0] + d - e[1]
print(s)


(a, b, c, d) = map(int, input().split())
rslt = 0
z = max(a, b, c)
x = min(a, b, c)
y = a + b + c - x - z
if y - x < d:
	rslt += d - y + x
if z - y < d:
	rslt += d - z + y
print(rslt)


l = list(map(int, input().split()))
d = l[3]
l.pop(3)
l.sort()
t = 0
if abs(l[0] - l[1]) < d:
	t += d - abs(l[0] - l[1])
if abs(l[1] - l[2]) < d:
	t += d - abs(l[1] - l[2])
print(t)


a = list(map(int, input().split(' ')))
d = a[3]
a.pop()
a.sort()
b = []
count = 0
b.append(abs(a[0] - a[1]))
b.append(abs(a[1] - a[2]))
b.append(abs(a[0] - a[2]))
while min(b) < d:
	if b[2] < d:
		count = count + (d - b[2])
		b[1] = b[1] + (d - b[2])
		b[2] = b[2] + (d - b[2])
	elif b[1] < d:
		count = count + (d - b[1])
		b[2] = b[2] + (d - b[1])
		b[1] = b[1] + (d - b[1])
	elif b[0] < d:
		count = count + (d - b[0])
		b[0] = d + 1
print(count)


try:
	t = []
	t = list(map(int, input().split()))
	d = t[3]
	t.pop()
	t.sort()
	l = t[1] - t[0]
	r = t[2] - t[1]
	tm = 0
	if l < d:
		tm = tm + d - l
	if r < d:
		tm = tm + d - r
	print(tm)
except Exception:
	pass


(a, b, c, d) = map(int, input().split())
s1 = 0
if b >= a and c >= b:
	dif1 = abs(a - b)
	if dif1 < d:
		a = a - (d - dif1)
		s1 = s1 + (d - dif1)
	dif3 = abs(c - b)
	if dif3 < d:
		s1 = s1 + (d - dif3)
	print(s1)
elif c >= a and b >= c:
	dif1 = abs(a - c)
	if dif1 < d:
		a = a - (d - dif1)
		s1 = s1 + (d - dif1)
	dif3 = abs(c - b)
	if dif3 < d:
		s1 = s1 + (d - dif3)
	print(s1)
elif b >= c and a >= b:
	dif1 = abs(c - b)
	if dif1 < d:
		c = c - (d - dif1)
		s1 = s1 + (d - dif1)
	dif3 = abs(b - a)
	if dif3 < d:
		s1 = s1 + (d - dif3)
	print(s1)
elif a >= b and c >= a:
	dif1 = abs(a - b)
	if dif1 < d:
		b = b - (d - dif1)
		s1 = s1 + (d - dif1)
	dif3 = abs(c - a)
	if dif3 < d:
		s1 = s1 + (d - dif3)
	print(s1)
elif a >= c and b >= a:
	dif1 = abs(a - c)
	if dif1 < d:
		c = c - (d - dif1)
		s1 = s1 + (d - dif1)
	dif3 = abs(b - a)
	if dif3 < d:
		s1 = s1 + (d - dif3)
	print(s1)
elif c >= b and a >= c:
	dif1 = abs(b - c)
	if dif1 < d:
		b = b - (d - dif1)
		s1 = s1 + (d - dif1)
	dif3 = abs(c - a)
	if dif3 < d:
		s1 = s1 + (d - dif3)
	print(s1)


(a, b, c, d) = input().split()
a = int(a)
b = int(b)
c = int(c)
d = int(d)
li = [a, b, c]
li.sort()
a = li[0]
b = li[1]
c = li[2]
st = 0
if b - a < d:
	st += d - b + a
if c - b < d:
	st += d - c + b
print(st)


a = list(map(int, input().split()))
d = a[3]
a = a[:3]
a.sort()
x = 0
if a[1] - a[0] < d:
	x += d - a[1] + a[0]
if a[2] - a[1] < d:
	x += d - a[2] + a[1]
print(x)


(a, b, c, d) = map(int, input().split())
qw = [a, b, c]
qw.sort()
(a, b, c) = (qw[0], qw[1], qw[2])
y = c - b
x = b - a
if c - a >= 2 * d:
	q = max(b - c + d, a + d - b)
	if q < 0:
		q = 0
else:
	q = d - b + a
	if q < 0:
		q = 0
	w = d - c + b
	if w < 0:
		w = 0
	q = q + w
print(q)


l = [int(i) for i in input().split()]
d = l.pop(-1)
(a, b, c) = sorted(l)
result = 0
if b - a < d:
	result += d - (b - a)
if c - b < d:
	result += d - (c - b)
print(result)


(a, b, c, d) = map(int, input().split())
List = [a, b, c]
maxx = max(a, b, c)
del List[List.index(maxx)]
minn = min(a, b, c)
del List[List.index(minn)]
count = 0
if maxx - List[0] < d:
	count = count + d - maxx + List[0]
if List[0] - minn < d:
	count = count + d - List[0] + minn
print(count)


l = list(map(int, input().split()))
d = l[-1]
l.pop()
l.sort()
x = 0
if l[1] - l[0] < d:
	x += d - l[1] + l[0]
if l[2] - l[1] < d:
	x += d - l[2] + l[1]
print(x)


(a, b, c, d) = map(int, input().split())
x = []
x.append(a)
x.append(b)
x.append(c)
x.sort()
count = 0
if x[1] - x[0] < d:
	count = d - (x[1] - x[0])
if x[2] - x[1] < d:
	count += d - (x[2] - x[1])
print(count)


a = [i for i in map(int, input().split())]
b = sorted(a[0:3])
n = (a[3] - b[1] + b[0], 0)
m = (a[3] - b[2] + b[1], 0)
sol = max(n) + max(m)
print(sol)


nums = input().split(' ')
d = int(nums[3])
positions = [int(x) for x in (nums[0], nums[1], nums[2])]
positions.sort()
perf_time = 0
if positions[1] - positions[0] < d:
	perf_time += d - (positions[1] - positions[0])
if positions[2] - positions[1] < d:
	perf_time += d - (positions[2] - positions[1])
print(perf_time)


(a, b, c, d) = map(int, input().split())
x = [a, b, c]
x.sort()
a = x[0]
b = x[1]
c = x[2]
ans = max(0, a + d - b) + d + max(max(b, a + d) - c, 0)
vl = max(0, a - (b - d)) + max(b + d - c, 0)
ans = min(ans, vl)
vl = max(0, b - (c - d))
pos = min(b, c - d)
vl += max(0, a - (pos - d))
ans = min(ans, vl)
print(ans)


(a, b, c, d) = map(int, input().split())
l = sorted([a, b, c])
if l[2] - l[1] >= d:
	step1 = 0
else:
	step1 = d - l[2] + l[1]
if l[1] - l[0] >= d:
	step2 = 0
else:
	step2 = d - l[1] + l[0]
print(step1 + step2)


(a, b, c, d) = map(int, input().split())
[a, b, c] = sorted([a, b, c])
ans = 0
d1 = b - a
if d1 - d < 0:
	ans = abs(d1 - d)
d2 = c - b
if d2 - d < 0:
	ans += abs(d2 - d)
print(ans)


(a, b, c, d) = [int(a) for a in input().split()]
(a, b, c) = sorted([a, b, c])
print(max(d - (b - a), 0) + max(d - (c - b), 0))


(a, b, c, d) = map(int, input().split())
if ((abs(a - b) >= d and abs(b - c) >= d) and abs(a - c)) >= d:
	print(0)
elif a == b and a == c:
	print(2 * d)
else:
	nu = [a, b, c]
	nu.sort()
	count = 0
	moda = nu[1] - nu[0]
	modb = nu[2] - nu[1]
	ls = [moda, modb]
	for i in ls:
		if i < d:
			count = count + (d - i)
	print(count)


(a, b, c, d) = map(int, input().split(' '))
(a, b, c) = sorted((a, b, c))
x = 0
if b - a < d:
	x += d - b + a
if c - b < d:
	x += d - c + b
print(x)


(a, b, c, d) = [int(x) for x in input().split()]
aa = [a, b, c]
aa.sort()
ans = max(0, d - (aa[1] - aa[0])) + max(0, d - (aa[2] - aa[1]))
print(ans)


from sys import stdin, stdout
import math
import collections
(a, b, c, d) = list(map(int, stdin.readline().split()))
arr = [a, b, c]
arr.sort()
(a, b, c) = arr
ans = 0
if abs(a - b) < d:
	ans = d - abs(a - b)
if abs(c - b) < d:
	ans += d - abs(c - b)
print(ans)


a = list(map(int, input().split()))
b = a[0:3]
c = a[3]
b.sort()
ans = 0
for i in range(0, 2):
	x = b[i + 1] - b[i]
	ans += max(0, c - x)
print(ans)


R = lambda : map(int, input().split())
(a, b, c, d) = R()
(a, b, c) = sorted([a, b, c])
if c - a >= 2 * d:
	p = 10 ** 18
	if a + d > b:
		p = min(p, a + d - b)
	if c - d < b:
		p = min(p, b - c + d)
	if p == 10 ** 18:
		p = 0
	print(p)
else:
	p = 0
	if b - d < a:
		p += a - (b - d)
	if b + d > c:
		p += b + d - c
	print(p)


(a, b, c, k) = list(map(int, input().split()))
l = list((a, b, c))
l.sort()
c = 0
for i in range(2):
	if abs(l[i] - l[i + 1]) < k:
		c += k - abs(l[i] - l[i + 1])
print(c)


(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted([a, b, c])
s = 2 * d - min(d, b - a) - min(d, c - b)
print(s)


(a, b, c, d) = map(int, input().split())
l = list()
l.append(a)
l.append(b)
l.append(c)
l.sort()
a = l[0]
b = l[1]
c = l[2]
bc = c - b
ab = b - a
ans = 0
if bc < d:
	ans += d - bc
if ab < d:
	ans += d - ab
print(ans)


l = []
l = list(map(int, input().split()))
d = l[3]
l.pop()
l.sort()
count = 0
for i in range(2):
	if abs(l[i + 1] - l[i]) < d:
		count += d - abs(l[i + 1] - l[i])
	else:
		continue
print(count)


(a, b, c, d) = list(map(int, input().split()))
l = [a, b, c]
l.sort()
(a, b, c) = l
ans = 0
if b - a < d:
	diff = b - a
	ans += d - diff
if c - b < d:
	diff = c - b
	ans += d - diff
print(ans)


ints = [int(x) for x in input().split()]
locations = ints[:3]
locations.sort()
print(max(0, -locations[1] + locations[0] + ints[3]) + max(0, -locations[2] + locations[1] + ints[3]))


a = list(map(int, input().split(' ')))
d = a[3]
a.pop()
a.sort()
s = 0
if abs(a[0] - a[1]) < d:
	s += d - abs(a[0] - a[1])
if abs(a[1] - a[2]) < d:
	s += d - abs(a[1] - a[2])
print(s)


(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
a = l[0]
b = l[1]
c = l[2]
x = d - b + a
y = d - c + b
if x >= 0 and y >= 0:
	print(x + y)
elif x < 0 and y >= 0:
	print(y)
elif y < 0 and x >= 0:
	print(x)
elif x < 0 and y < 0:
	print(0)


(a, b, c, d) = map(int, input().split())
x = list(sorted([a, b, c]))
print(sum([d - (x[i + 1] - x[i]) for i in range(2) if x[i + 1] - x[i] < d]))


l = []
(a, b, c, d) = list(map(int, input().split()))
l.append(a)
l.append(b)
l.append(c)
l.sort()
l.reverse()
t = 0
if l[1] - l[2] < d:
	t = t + d - l[1] + l[2]
if l[0] - l[1] < d:
	t = t + d - l[0] + l[1]
print(t)


(a, b, c, d) = [int(i) for i in input().split()]
arr = []
arr.append(a)
arr.append(b)
arr.append(c)
arr.sort()
ans = 0
if arr[1] - arr[0] < d:
	ans += d - (arr[1] - arr[0])
if arr[2] - arr[1] < d:
	ans += d - (arr[2] - arr[1])
print(ans)


a = list(map(int, input().split()))
b = a[0:3]
b.sort()
d = a[3]
a1 = b[1] - b[0]
if a1 < d:
	a1 = d - a1
else:
	a1 = 0
a2 = b[2] - b[1]
if a2 < d:
	a2 = d - a2
else:
	a2 = 0
print(a1 + a2)


(a, b, c, d) = map(int, input().split())
q = []
res = 0
res1 = 0
q.append(a)
q.append(b)
q.append(c)
q = sorted(q)
if abs(q[1] - q[0]) <= d:
	res = d - abs(q[1] - q[0])
if abs(q[1] - q[2]) <= d:
	res1 = d - abs(q[1] - q[2])
print(res + res1)


from collections import Counter
import math
from collections import Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter
(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
a = l[0]
b = l[1]
c = l[2]
val1 = max(0, d - (b - a))
val2 = max(0, d - (c - b))
print(val1 + val2)


(a, b, c, d) = map(int, input().split())
s = []
s.append(a)
s.append(b)
s.append(c)
s = sorted(s)
import math
x = 0
y = 0
if math.fabs(s[0] - s[1]) < d:
	x = d - math.fabs(s[0] - s[1])
if math.fabs(s[1] - s[2]) < d:
	y = d - math.fabs(s[1] - s[2])
print(int(x + y))


(a, b, c, d) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
if b > c:
	(b, c) = (c, b)
if a > b:
	(a, b) = (b, a)
if b > c:
	(b, c) = (c, b)
if a > b:
	(a, b) = (b, a)
if b > c:
	(b, c) = (c, b)
if a > b:
	(a, b) = (b, a)
if b > c:
	(b, c) = (c, b)
if a > b:
	(a, b) = (b, a)
if b > c:
	(b, c) = (c, b)
if a > b:
	(a, b) = (b, a)
if b > c:
	(b, c) = (c, b)
print(max(d - (b - a), 0) + max(d - (c - b), 0))


(a, b, c, d) = map(int, input().split())
A = [a, b, c]
A.sort()
t = 0
x = d - (A[1] - A[0])
y = d - (A[2] - A[1])
if x < 0:
	x = 0
if y < 0:
	y = 0
t = x + y
print(t)


ans = 0
x = list(map(int, input().split()))
d = x[3]
x.remove(d)
x.sort()
a = x[0]
b = x[1]
c = x[2]
if abs(a - b) < d:
	ans += d - abs(a - b)
if abs(b - c) < d:
	ans += d - abs(b - c)
print(ans)


from __future__ import division, print_function
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
from math import ceil, floor, log, sqrt, factorial, pow, pi

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p1
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph():
	d = {}
	(v, e) = mi()
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(l, n):
	d = {}
	for i in range(1, n + 1):
		if i not in d.keys():
			d[i] = l[i]
		else:
			d[i].append(l[i])
	return d
(a, b, c, d) = mi()
l = sorted([a, b, c])
a = l[1] - l[0]
b = l[2] - l[1]
if a < d:
	a = d - a
else:
	a = 0
if b < d:
	b = d - b
else:
	b = 0
print(a + b)


(a, b, c, d) = list(map(int, input().split()))
(a, b, c) = sorted([a, b, c])
i = 0
t = d - abs(a - b)
if t <= 0:
	t = 0
i += t
t = d - abs(c - b)
if t <= 0:
	t = 0
i += t
print(i)


(a, b, c, d) = [int(i) for i in input().split(' ')]
x = [a, b, c]
x.sort()
print(max(0, d - x[1] + x[0]) + max(0, d - x[2] + x[1]))


(a, b, c, d) = [int(s) for s in input().split()]
z = sorted([a, b, c])
st = 0
if z[1] - z[0] < d:
	st += d - (z[1] - z[0])
if z[2] - z[1] < d:
	st += d - (z[2] - z[1])
print(st)


a = list(map(int, input().split()))
b = a[:3]
d = a[3]
b.sort()
ans = 0
for i in range(1, len(b)):
	if b[i] - b[i - 1] <= d:
		ans += d - (b[i] - b[i - 1])
print(ans)


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def int1():
	return int(input())
import string
import math
from itertools import combinations

def main():
	a = get_array()
	d = a[-1]
	a = a[:len(a) - 1]
	a.sort()
	z = 0
	if a[1] - a[0] < d:
		z = z + d - (a[1] - a[0])
	if a[2] - a[1] < d:
		z = z + d - (a[2] - a[1])
	print(z)
main()


p = [0, 0, 0]
(p[0], p[1], p[2], d) = list((int(a) for a in input().split()))
p.sort()
c = 0
if p[1] - p[0] < d:
	c += d - (p[1] - p[0])
if p[2] - p[1] < d:
	c += d - (p[2] - p[1])
print(c)


(a, b, c, d) = tuple(map(int, input().split()))
M = max(a, b, c)
m = min(a, b, c)
c = a + b + c - (M + m)
d1 = c - m
d2 = M - c
r = max(d - d1, 0)
r += max(d - d2, 0)
print(r)


(a, b, c, d) = map(int, input().split())
li = list()
li.append(a)
li.append(b)
li.append(c)
li.sort()
d1 = li[1] - li[0]
d2 = li[2] - li[1]
if d1 >= d and d2 >= d:
	print(0)
elif d1 >= d and d2 >= d:
	print(0)
elif d1 >= d and d2 < d:
	x = li[1] + d
	diff = abs(x - li[2])
	print(diff)
elif d1 < d and d2 >= d:
	x = li[1] - d
	diff = abs(li[0] - x)
	print(diff)
else:
	flag = 0
	if flag == 1:
		if d1 < d2:
			x = li[0] - d
			diff = abs(li[1] - x)
			print(diff)
		else:
			x = li[2] + d
			diff = abs(li[1] - x)
			print(diff)
	else:
		x1 = li[1] - d
		diff1 = abs(li[0] - x1)
		x2 = li[1] + d
		diff2 = abs(li[2] - x2)
		su = diff1 + diff2
		print(su)


a = list(map(int, input().split()))
d = a[3]
a.remove(a[3])
a.sort()
l = a[0]
md = a[1]
h = a[2]
d1 = h - md
d2 = md - l
if d1 >= d:
	t1 = 0
else:
	t1 = d - d1
if d2 >= d:
	t2 = 0
else:
	t2 = d - d2
print(t1 + t2)


(a, b, c, d) = input().split(' ')
(a, b, c, d) = [int(a), int(b), int(c), int(d)]
x = max(a, b, c)
y = min(a, b, c)
z = [a, b, c]
z.sort()
acc = 0
if x - z[1] <= d:
	acc += d - (x - z[1])
if z[1] - y <= d:
	acc += d - (z[1] - y)
print(acc)


(*a, k) = map(int, input().split())
(a, b, c) = sorted(a)
print(max(0, k - b + a) + max(0, k - c + b))


(a, b, c, d) = map(int, input().split())
s = [a, b, c]
s.sort()
first = d - (s[1] - s[0])
second = d - (s[2] - s[1])
k = 0
if first > 0:
	k += first
if second > 0:
	k += second
print(k)


from math import *
from itertools import *
from collections import *
from bisect import *
(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
res = 0
if l[1] - l[0] < d:
	res += l[0] - l[1] + d
if l[2] - l[1] < d:
	res += l[1] + d - l[2]
print(res)


def main():
	a = list(map(int, input().split(' ')))
	s = a[3]
	a.pop()
	a.sort()
	ans = 0
	if abs(a[2] - a[1]) < s:
		ans += abs(s - abs(a[2] - a[1]))
	if abs(a[1] - a[0]) < s:
		ans += abs(s - abs(a[1] - a[0]))
	print(ans)
main()


x = list(map(int, input().split()))
d = x[3]
y = x[0:3]
y.sort()
g = 0
if y[1] - y[0] < d:
	g = g + (d - (y[1] - y[0]))
if y[2] - y[1] < d:
	g = g + (d - (y[2] - y[1]))
print(g)


[a, b, c, d] = [int(x) for x in input().split()]
[x, y, z] = sorted([a, b, c])
print(max(0, d - (y - x)) + max(0, d - (z - y)))


(a, b, c, d) = map(int, input().split())
ar = []
ar.append(a)
ar.append(b)
ar.append(c)
ar.sort()
sum = 0
if d - (ar[1] - ar[0]) > 0:
	sum += d - (ar[1] - ar[0])
if d - (ar[2] - ar[1]) > 0:
	sum += d - (ar[2] - ar[1])
print(sum)


arr = list(map(int, input().split()))
d = arr.pop(3)
arr = sorted(arr)
temp = arr[1] - arr[0]
temp1 = arr[2] - arr[1]
count = 0
if temp < d:
	count += d - temp
if temp1 < d:
	count += d - temp1
print(count)


(*n, d) = list(map(int, input().split()))
n.sort()
a = 0
if n[2] - n[1] < d:
	a += d + n[1] - n[2]
if n[1] - n[0] < d:
	a += d + n[0] - n[1]
print(a)


(*s, d) = map(int, input().split())
s = sorted(s)
c = 0
if s[1] - s[0] < d:
	c = c + d - (s[1] - s[0])
if s[2] - s[1] < d:
	c = c + d - (s[2] - s[1])
print(c)


(a, b, c, d) = map(int, input().split())
nums = list()
nums.append(a)
nums.append(b)
nums.append(c)
nums.sort()
a = nums[0]
b = nums[1]
c = nums[2]
move = 0
if b - a < d:
	move += d - (b - a)
if c - b < d:
	move += d - (c - b)
print(move)


(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
mid = arr[1]
left = arr[0]
right = arr[2]
ans = 0
if mid - left < d:
	ans += d - (mid - left)
if right - mid < d:
	ans += d - (right - mid)
print(ans)


(a, b, c, d) = map(int, input().split())
lis = [[a, 1], [b, 2], [c, 3]]
lis = sorted(lis)
s = d - (lis[1][0] - lis[0][0])
s2 = d - (lis[2][0] - lis[1][0])
print(max(s2, 0) + max(s, 0))


def fcuk(l, d):
	a = l[0]
	b = l[1]
	c = l[2]
	t = m = 0
	t = d - (b - a)
	m = d - (c - b)
	if t < 0:
		t = 0
	if m < 0:
		m = 0
	return t + m
x = list(map(int, input().split()))
print(fcuk(sorted(x[:3]), x[3]))


(a, b, c, d) = list(map(int, input().split()))
l = [a, b, c]
l.sort()
(a, b, c) = l
print(max(d - (b - a), 0) + max(d - (c - b), 0))


def solve(a, b, c, d):
	return max(0, d - (b - a)) + max(0, d - (c - b))
(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted((a, b, c))
print(solve(a, b, c, d))


from math import *
i = 0
(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
if l[2] - l[1] < d:
	i = i + d - (l[2] - l[1])
if l[1] - l[0] < d:
	i = i + d - (l[1] - l[0])
print(i)


k = list(map(int, input().split()))
p = k[:-1]
p.sort()
cnt = 0
d = k[3]
if p[2] - p[1] < d:
	cnt += -p[2] + p[1] + d
if p[1] - p[0] < d:
	cnt += -p[1] + p[0] + d
print(cnt)


(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
count = 0
for i in range(2):
	if arr[i + 1] - arr[i] < d:
		count += d - (arr[i + 1] - arr[i])
print(count)


(*a, r) = map(int, input().split())
(a, b, c) = sorted(a)
print(max(0, r - b + a) + max(0, r - c + b))


def gns():
	return list(map(int, input().split()))
(a, b, c, d) = gns()
abc = [a, b, c]
abc.sort()
d1 = abc[1] - abc[0]
d2 = abc[2] - abc[1]
d1 = max(d - d1, 0)
d2 = max(d - d2, 0)
print(d1 + d2)


(a, b, c, d) = [int(x) for x in input().split()]
inp = [a, b, c]
inp.sort()
disA = abs(inp[0] - inp[1])
disC = abs(inp[2] - inp[1])
ans = 0
if disA < d:
	ans += d - disA
if disC < d:
	ans += d - disC
print(ans)


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations

def li():
	return list(map(int, input().split()))

def num():
	return map(int, input().split())

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	gg = find_gcd(x, y)
	return x * y // gg
mm = 1000000007
yp = 0

def solve():
	t = 1
	for tt in range(t):
		(a, b, c, d) = num()
		l = [a, b, c]
		l.sort()
		a = l[0]
		b = l[1]
		c = l[2]
		pp = 0
		if b - a < d:
			op = d - (b - a)
			pp += op
		if c - b < d:
			op = d - (c - b)
			pp += op
		print(pp)
solve()


(a, b, c, d) = map(int, input().split())
t = [a, b, c]
t.sort()
ans = 0
a = t[0]
b = t[1]
c = t[2]
if b - a < d and c - b < d:
	ans = d - (b - a) + d - (c - b)
elif b - a >= d and c - b < d:
	ans += d - (c - b)
elif b - a < d and c - b >= d:
	ans += d - (b - a)
else:
	ans = 0
print(ans)


(a, b, c, d) = map(int, input().split())
l = []
l.append(a)
l.append(b)
l.append(c)
l.sort()
x = l[1] - l[0]
y = l[2] - l[1]
sum = 0
if x < d:
	sum = sum + (d - x)
if y < d:
	sum = sum + (d - y)
print(sum)


for _ in range(1):
	(a, b, c, d) = map(int, input().split())
	x = sorted([a, b, c])
	print(max(0, d - x[1] + x[0]) + max(0, d - x[2] + x[1]))


z = list(map(int, input().split()))
b = z[3]
del z[3]
z.sort()
g = 0
g += max(0, b - z[2] + z[1])
g += max(0, b - z[1] + z[0])
print(g)


(*vals, d) = map(int, input().split())
vals.sort()
(a, b, c) = vals
del vals
res = 0
if abs(a - b) < d:
	res = d - abs(a - b)
if abs(b - c) < d:
	res += d - abs(b - c)
print(res)


l = list(map(int, input().split()))
x = l[3]
l = [l[0], l[1], l[2]]
l.sort()
print(max(x - l[1] + l[0], 0) + max(x - l[2] + l[1], 0))


import os.path
from math import gcd, floor, ceil
from collections import *
import sys
mod = 1000000007
INF = float('inf')

def st():
	return list(sys.stdin.readline().strip())

def li():
	return list(map(int, sys.stdin.readline().split()))

def mp():
	return map(int, sys.stdin.readline().split())

def inp():
	return int(sys.stdin.readline())

def pr(n):
	return sys.stdout.write(str(n) + '\n')

def prl(n):
	return sys.stdout.write(str(n) + ' ')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def solve():
	l = li()
	d = l.pop()
	l.sort()
	ans = 0
	if l[1] - d < l[0]:
		ans += l[0] - (l[1] - d)
	if l[1] + d > l[2]:
		ans += l[1] + d - l[2]
	pr(ans)
for _ in range(1):
	solve()


(a, b, c, d) = map(int, input().split())
ans = 0
l = sorted([a, b, c])
if d > abs(l[0] - l[1]):
	ans += d - abs(l[0] - l[1])
if d > abs(l[1] - l[2]):
	ans += d - abs(l[1] - l[2])
print(ans)


(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
left = d - (l[1] - l[0])
right = d - (l[2] - l[1])
left = left if left > 0 else 0
right = right if right > 0 else 0
print(left + right)


(a, b, c, d) = map(int, input().split())
kmax = max(a, b, c)
kmin = min(a, b, c)
i = 0
if kmax == a and kmin == b or (kmax == b and kmin == a):
	ks = c
elif kmax == b and kmin == c or (kmax == c and kmin == b):
	ks = a
else:
	ks = b
if abs(kmax - ks) < d:
	i += abs(abs(kmax - ks) - d)
if abs(kmin - ks) < d:
	i += abs(abs(kmin - ks) - d)
print(i)


abcd = list(map(int, input().split()))
actors = []
for k in range(3):
	actors.append(abcd[k])
actors.sort()
time = 0
if actors[2] - actors[1] < abcd[3]:
	time += abcd[3] - (actors[2] - actors[1])
if actors[1] - actors[0] < abcd[3]:
	time += abcd[3] - (actors[1] - actors[0])
print(time)


(a, b, c, d) = (int(i) for i in input().split())
(a, b, c) = sorted([a, b, c])
sum_ = (d - (b - a) if d - (b - a) > 0 else 0) + (d - (c - b) if d - (c - b) > 0 else 0)
print(sum_)


input_string = input()
parts = input_string.split(' ')
a = int(parts[0])
b = int(parts[1])
c = int(parts[2])
d = int(parts[3])
arr = [a, b, c]
arr.sort()
ans = max(0, arr[0] - (arr[1] - d))
ans += max(0, arr[1] + d - arr[2])
print(ans)


(a, b, c, d) = map(int, input().split())
li = [a, b, c]
li.sort()
res = 0
for i in range(2):
	if abs(li[i] - li[i + 1]) < d:
		res += d - abs(li[i] - li[i + 1])
print(res)


import sys
input = sys.stdin.readline
(a, b, c, d) = map(int, input().split())
x = [a, b, c]
x.sort()
print(max(d - (x[1] - x[0]), 0) + max(d - (x[2] - x[1]), 0))


(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
d1 = l[1] - l[0]
d2 = l[2] - l[1]
final = 0
if d1 < d:
	final += d - d1
if d2 < d:
	final += d - d2
print(final)


import sys
for line in sys.stdin:
	line = line.rstrip('\n')
	line = line.split(' ')
	line = list(map(int, line))
	distance = line[3]
	Dancers = line[:3]
	Dancers.sort()
	time = 0
	if Dancers[1] - Dancers[0] < distance:
		time += distance - (Dancers[1] - Dancers[0])
	if Dancers[2] - Dancers[1] < distance:
		time += distance - (Dancers[2] - Dancers[1])
	print(time)


import sys
import math
input = sys.stdin.readline
(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
d1 = l[1] - l[0]
d2 = l[2] - l[1]
count = 0
if d1 < d:
	count += d - d1
if d2 < d:
	count += d - d2
print(count)


(a, b, c, d) = [int(x) for x in input().split()]
(a, b, c) = sorted([a, b, c])
r = 0
if b - a < d:
	r += d - (b - a)
if c - b < d:
	r += d - (c - b)
print(r)


l = input().split()
li = [int(i) for i in l]
d = li[3]
arr = li[:-1]
arr.sort()
ans = 0
ans = ans + max(0, d - (arr[2] - arr[1]))
ans = ans + max(0, d - (arr[1] - arr[0]))
print(ans)


(a, b, c, d) = map(int, input().split())
if a <= b <= c or a >= b >= c:
	print(max(d - abs(a - b), 0) + max(d - abs(c - b), 0))
elif a <= c <= b or a >= c >= b:
	print(max(d - abs(a - c), 0) + max(d - abs(c - b), 0))
else:
	print(max(d - abs(a - c), 0) + max(d - abs(a - b), 0))


(a, b, c, d) = tuple(map(int, input().split()))
places = sorted([a, b, c])
seconds = d - (places[1] - places[0]) if places[1] - places[0] < d else 0
seconds += d - (places[2] - places[1]) if places[2] - places[1] < d else 0
print(seconds)


(a, b, c, d) = [int(x) for x in input().split()]
e = sorted([a, b, c])
print(max(0, d - (e[1] - e[0])) + max(0, d - (e[2] - e[1])))


import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
sys.setrecursionlimit(100000)
input = sys.stdin.readline

def read_int():
	return int(input())

def read_int_n():
	return list(map(int, input().split()))

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

@mt
def slv(A, B, C, D):
	l = min(A, B, C)
	r = max(A, B, C)
	c = sorted((A, B, C))[1]
	ans = 0
	ans += D - abs(l - c) if D - abs(l - c) > 0 else 0
	ans += D - abs(r - c) if D - abs(r - c) > 0 else 0
	return ans

def main():
	(A, B, C, D) = read_int_n()
	print(slv(A, B, C, D))
main()


(a, b, c, d) = map(int, input().split())
ar = [a, b, c]
ar.sort()
left = max(0, d - abs(ar[1] - ar[0]))
right = max(0, d - abs(ar[2] - ar[1]))
print(left + right)


(a, b, c, d) = [int(x) for x in input().split()]
arr = sorted([a, b, c])
min1 = 0
for i in range(2):
	if abs(arr[i] - arr[i + 1]) < d:
		min1 = min1 + abs(d - abs(arr[i] - arr[i + 1]))
print(min1)


(a, b, c, d) = map(int, input().split(' '))
diff1 = abs(a - b)
diff2 = abs(b - c)
diff3 = abs(c - a)
res = []
res.append(diff1)
res.append(diff2)
res.append(diff3)
res = sorted(res)
count = 0
if res[0] < d:
	count += d - res[0]
if res[1] < d:
	count += d - res[1]
print(count)


(a, b, c, d) = [int(i) for i in input().split()]
(a, c, b) = (max(b, a, c), min(a, b, c), a + b + c - max(b, a, c) - min(a, b, c))
i = 0
if a - b < d:
	i = d - (a - b)
	a += i
if b - c < d:
	i += d - (b - c)
	c -= abs(a - b - d)
print(i)


(*a, d) = map(int, input().split())
a.sort()
print(max(0, d - a[2] + a[1]) + max(0, d - a[1] + a[0]))


a = [int(i) for i in input().split()]
b = a[0:len(a) - 1]
b = sorted(b)
if b[1] - b[0] < a[3] and b[2] - b[1] < a[3]:
	print(a[3] - (b[2] - b[1]) + (a[3] - (b[1] - b[0])))
elif b[2] - b[1] < a[3]:
	print(a[3] - (b[2] - b[1]))
elif b[1] - b[0] < a[3]:
	print(a[3] - (b[1] - b[0]))
else:
	print(0)


(a, b, c, d) = [int(x) for x in input().strip().split(' ')]
mini = min(a, b, c)
maxi = max(a, b, c)
midi = a + b + c - mini - maxi
print(max(0, d - (maxi - midi)) + max(0, d - (midi - mini)))


l = list(map(int, input().split()))
(a, b, c) = sorted(l[:3])
print(max(0, l[3] - (b - a)) + max(0, l[3] - (c - b)))


(a, b, c, d) = list(map(int, input().split()))
lst = [a, b, c]
lst.sort()
(x, y, z) = (lst[0], lst[1], lst[2])
(p, q, r) = (lst[0], lst[1], lst[2])
ans1 = 0
ans2 = 0
if y - x <= d:
	ans1 += d - (y - x)
	y = x + d
if abs(y - z) <= d:
	if y >= z:
		ans1 = ans1 + (d - (y - z))
	else:
		ans1 = ans1 + (d - (z - y))
if q - p < d:
	ans2 += d - (q - p)
	p = q - d
if abs(q - r) <= d:
	if q >= r:
		ans2 += d - (q - r)
	else:
		ans2 += d - (r - q)
if ans1 > ans2:
	print(ans2)
else:
	print(ans2)


rope = list(map(int, input().split(' ')))
d = rope[-1]
rope.remove(rope[-1])
rope.sort()
mid = rope[1]
diff_1 = rope[1] - rope[0]
t1 = d - diff_1
if t1 < 0:
	t1 = 0
diff_2 = rope[2] - rope[1]
t2 = d - diff_2
if t2 < 0:
	t2 = 0
ans = t1 + t2
print(ans)


def solve(a, b, c, d, is_ordered=False):
	if not is_ordered:
		(a, b, c) = sorted([a, b, c])
	if a == b:
		if b == c:
			return 2 * d
		else:
			return max(d - abs(c - b), 0) + d
	if b == c:
		return max(d - abs(c - a), 0) + d
	return max(d - (c - b), 0) + max(d - (b - a), 0)
t = input()
(a, b, c, d) = t.split()
print(solve(int(a), int(b), int(c), int(d)))


ax = input().split()
a = int(ax[0])
b = int(ax[1])
c = int(ax[2])
d = int(ax[3])
if a >= b and b >= c:
	l = b
	mi = c
	ma = a
elif a >= c and c >= b:
	l = c
	mi = b
	ma = a
elif b >= a and a >= c:
	l = a
	mi = c
	ma = b
elif b >= c and c >= a:
	l = c
	mi = a
	ma = b
elif c >= a and a >= b:
	l = a
	mi = b
	ma = c
elif c >= b and b >= a:
	l = b
	mi = a
	ma = c
if l - mi >= d and ma - l >= d:
	print(0)
elif l - mi < d and ma - l >= d:
	print(d - (l - mi))
elif l - mi >= d and ma - l < d:
	print(d - (ma - l))
else:
	print(d - (l - mi) + (d - (ma - l)))


(a, b, c, d) = map(int, input().split())
x = [a, b, c]
x.sort()
(d1, d2) = (abs(x[1] - x[0]), abs(x[2] - x[1]))
print(max(0, d - d1) + max(0, d - d2))


a = list(map(int, input().split(' ')))
d = a.pop()
a.sort()
r = max(0, d - (a[1] - a[0])) + max(0, d - (a[2] - a[1]))
print(r)


(a, b, c, d) = map(int, input().split())
t = [a, b, c]
t.sort()
a = t[0]
b = t[1]
c = t[2]
ans1 = 0
if a + d > b:
	ans1 += a + d - b
if max(a + d, b) + d > c:
	ans1 += max(a + d, b) + d - c
ans2 = 0
if a + d > b:
	ans2 += a + d - b
if b + d > c:
	ans2 += b + d - c
ans3 = 0
if c - d < b:
	ans3 += b - c + d
if min(c - d, b) - d < a:
	ans3 += a + d - min(c - d, b)
print(min(ans1, ans2, ans3))


arr = [int(i) for i in input().split()]
d = arr.pop()
arr.sort()
print(max(0, d - (arr[2] - arr[1])) + max(0, d - (arr[1] - arr[0])))


import sys
sys.setrecursionlimit(1000000)

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = self.next_line()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_line(self):
		return sys.stdin.readline().split()

	def next_ints(self):
		return [int(x) for x in sys.stdin.readline().split()]

	def next_int(self):
		return int(self.next())

	def solve(self):
		x = self.next_ints()
		xx = sorted(x[:3])
		print(max(0, x[-1] - xx[1] + xx[0]) + max(0, x[-1] - xx[2] + xx[1]))
Main().solve()


(a, b, c, d) = map(int, input().split())
q = sorted([a, b, c])
if q[1] != b:
	if q[1] == a:
		(b, a) = (a, b)
	else:
		(b, c) = (c, b)
print(max(0, d - abs(a - b)) + max(0, d - abs(b - c)))


(a, b, c, d) = list(map(int, input().split()))
p = sorted([a, b, c])
print(max(0, d - (p[1] - p[0])) + max(0, d - (p[2] - p[1])))


(a, b, c, d) = map(int, input().split())
a = [a, b, c]
for i in range(3):
	for j in range(3 - i - 1):
		if a[j] > a[j + 1]:
			(a[j], a[j + 1]) = (a[j + 1], a[j])
d1 = a[1] - a[0]
d2 = a[2] - a[1]
final_d = 0
if d1 < d:
	final_d += abs(d1 - d)
if d2 < d:
	final_d += abs(d2 - d)
print(final_d)


(a, b, c, d) = map(int, input().split())
f = [a, b, c]
f.sort()
k = f[1] - f[0]
l = f[2] - f[1]
print(max(d - k, 0) + max(0, d - l))


inp = input()
counter = 0
allInfo = inp.split()
dist = []
for i in range(4):
	allInfo[i] = int(allInfo[i])
dist = allInfo[:3]
neededDist = allInfo[3]
dist.sort()
if dist[1] - dist[0] < neededDist:
	counter = counter + (neededDist - (dist[1] - dist[0]))
if dist[2] - dist[1] < neededDist:
	counter = counter + (neededDist - (dist[2] - dist[1]))
print(counter)


a = list(map(int, input().split()))
b = a[:3]
d = a[3]
b = sorted(b)
ans = 0
if b[1] - b[0] < d:
	ans += d - (b[1] - b[0])
if b[2] - b[1] < d:
	ans += d - (b[2] - b[1])
print(ans)


(a, b, c, d) = list(map(int, input().split()))
arr = [a, b, c]
arr.sort()
ans = 0
if abs(arr[1] - arr[0]) < d:
	ans += d - abs(arr[1] - arr[0])
if abs(arr[2] - arr[1]) < d:
	ans += d - abs(arr[2] - arr[1])
print(ans)


(a, b, c, d) = map(int, input().split())
p = sorted([a, b, c])
ans = 0
dl = p[1] - p[0] - d
dr = p[2] - p[1] - d
ans += abs(dl) if dl < 0 else 0
ans += abs(dr) if dr < 0 else 0
print(ans)


def mp():
	return map(int, input().split())
(a, b, c, d) = mp()
(a, b, c) = (min(a, b, c), a + b + c - max(a, b, c) - min(a, b, c), max(a, b, c))
print(max(0, d - b + a) + max(0, d - c + b))


(a, b, c, d) = map(int, input().split())
people = [a, b, c]
people.sort()
t = t_avaible = 0
if people[2] - people[0] < 2 * d:
	t = 2 * d - (people[2] - people[0])
	t_avaible = t
if people[2] - people[1] < d:
	if t_avaible < d - (people[2] - people[1]):
		people[2] += t_avaible
		t_avaible = 0
	else:
		people[2] += d - (people[2] - people[1])
		t_avaible -= d - (people[2] - people[1])
if t_avaible > 0:
	people[0] -= t_avaible
mini = min(people[2] - people[1], people[1] - people[0])
if mini < d:
	t += d - mini
print(t)


lin = input()
nums = lin.split(' ')
a = int(nums[0])
b = int(nums[1])
c = int(nums[2])
d = int(nums[3])
points = [a, b, c]
points = sorted(points)
res1 = abs(points[0] - points[1])
res2 = abs(points[1] - points[2])
result = 0
if res1 < d:
	result += abs(res1 - d)
if res2 < d:
	result += abs(res2 - d)
print(result)


(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
res = 0
if abs(l[0] - l[1]) < d:
	res += abs(l[1] - d - l[0])
if l[2] - l[1] < d:
	res += abs(l[1] + d - l[2])
print(res)


(*a,) = map(int, input().split())
b = sorted(a[:-1])
print(a[-1] - min(b[1] - b[0], a[-1]) + a[-1] - min(b[2] - b[1], a[-1]))


from collections import deque
import math
import os
import random
import re
import sys

def solve(a, b, c, d):
	return max(0, d - (b - a)) + max(0, d - (c - b))

def main():
	(a, b, c, d) = map(int, input().split())
	(a, b, c) = sorted((a, b, c))
	print(solve(a, b, c, d))
main()


e = list(map(int, input().split()))
d = e[-1]
e.pop()
e.sort()
t = 0
diff1 = e[1] - e[0]
diff2 = e[2] - e[1]
if diff1 < d:
	t += d - diff1
if diff2 < d:
	t += d - diff2
print(t)


from sys import stdin
(a, b, c, d) = map(int, stdin.readline().strip().split())
s = [a, b, c]
s.sort()
a = s[0]
b = s[1]
c = s[2]
ans = 10 ** 20
b1 = max(b + d, c)
b2 = min(b - d, a)
ans = min(ans, abs(b1 - c) + abs(b2 - a))
print(ans)


import sys
(a, b, c, d) = list(map(int, sys.stdin.readline().split()))
if a > b:
	(a, b) = (b, a)
if b > c:
	(b, c) = (c, b)
if a > b:
	(a, b) = (b, a)
m = 0
if c < b + d:
	m = m + b + d - c
if a > b - d:
	m = m + a - b + d
print(m)


(a, b, c, d) = map(int, input().split())
pos = sorted([a, b, c])
(d1, d2) = (abs(pos[0] - pos[1]), abs(pos[1] - pos[2]))
moves = 0
while d1 < d or d2 < d:
	if d1 < d:
		tmp = d - d1
		d1 += tmp
		moves += tmp
	if d2 < d:
		tmp = d - d2
		d2 += tmp
		moves += tmp
print(moves)


x = input()
x = x.split()
a = x[0:3]
d = int(x[3])
for i in range(3):
	a[i] = int(a[i])
a.sort()
print(max(d - (a[1] - a[0]), 0) + max(d - (a[2] - a[1]), 0))


import math
import bisect
import heapq
from collections import defaultdict

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, x, y) = egcd(b % a, a)
		return (g, y - b // a * x, x)

def mulinv(b, n):
	(g, x, _) = egcd(b, n)
	if g == 1:
		return x % n

def isprime(n):
	for d in range(2, int(math.sqrt(n)) + 1):
		if n % d == 0:
			return False
	return True

def argsort(ls):
	return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
	if p == 1:
		return map(int, input().split())
	elif p == 2:
		return list(map(int, input().split()))
	elif p == 3:
		return list(input())
	else:
		return int(input())

class DisjointSet:

	def __init__(self, n):
		self.parent = [i for i in range(1, n + 1)]
		self.rank = [0] * (n + 1)

	def find_set(self, x):
		if self.parent[x] == x:
			return x
		else:
			self.parent[x] = self.find_set(self.parent[x])
			return self.parent[x]

	def union(self, x, y):
		set_x = self.find_set(x)
		set_y = self.find_set(y)
		if set_x != set_y:
			if self.rank[x]:
				pass

def graph(n, m, edg=False):
	edges = []
	visited = [0] * n
	g = [list() for _ in range(n + 1)]
	for i in range(m):
		(u, v) = f(1)
		g[u].append(v)
		g[v].append(u)
		if edg:
			edges.append((u, v))
	if edg:
		return (g, visited, edg)
	else:
		return (g, visited)

def bfs(g, visited):
	queue = [1]
	visited[1] = 1
	for u in queue:
		for v in g[u]:
			if visited[v] == 0:
				queue.append(v)
			visited[v] = 1

def dfs(u, g, visited):
	visited[u] = 1
	for v in g[u]:
		if visited[v] == 0:
			dfs(v, g, visited)
cl = f(2)
d = cl[-1]
del cl[-1]
cl = sorted(cl)
a = cl[0]
b = cl[1]
c = cl[2]
print(max(0, d - (b - a)) + max(0, d - (c - b)))


ll = lambda : list(map(int, input().split()))
testcases = 1
import math as m
for testcase in range(testcases):
	[a, b, c, d] = ll()
	[a, b, c] = list(sorted([a, b, c]))
	ans = 0
	ans += d - min(b - a, d)
	ans += d - min(c - b, d)
	print(ans)


gi = lambda : list(map(int, input().strip().split()))
(a, b, c, d) = gi()
l = [a, b, c]
l.sort()
ans = 0
if l[1] - l[0] < d:
	ans += d - l[1] + l[0]
if l[2] - l[1] < d:
	ans += d - l[2] + l[1]
print(ans)


(a, b, c, d) = map(int, input().split())
smallest = min(a, b, c)
biggest = max(a, b, c)
medium = a + b + c - (smallest + biggest)
d1 = medium - smallest
d1 = 0 if d1 >= d else d - d1
d2 = biggest - medium
d2 = 0 if d2 >= d else d - d2
print(d1 + d2)


(a, b, c, d) = map(int, input().split())
if (abs(a - b) >= d and abs(b - c) >= d) and abs(a - c) >= d:
	print(0)
else:
	x = [a, b, c]
	y = [a, b, c]
	y.sort()
	x.sort()
	if x[1] - x[0] < d:
		x[0] = x[0] - (d - (x[1] - x[0]))
	if x[2] - x[1] < d:
		x[2] = x[2] + d - (x[2] - x[1])
	s = 0
	for i in range(len(y)):
		s = s + abs(x[i] - y[i])
	print(s)


w = input().split(' ')
a = min(int(w[0]), int(w[1]), int(w[2]))
c = max(int(w[0]), int(w[1]), int(w[2]))
b = int(w[0]) + int(w[1]) + int(w[2]) - a - c
x = b - a
y = c - b
(t, p) = (0, 0)
if x < int(w[3]):
	t = int(w[3]) - x
if y < int(w[3]):
	p = int(w[3]) - y
print(t + p)


m = [int(x) for x in input().split()]
(a, b, c) = sorted(m[:3])
d = m[3]
t = max(0, d - abs(a - b)) + max(0, d - abs(b - c))
print(t)


def factorial(n):
	if n == 1:
		return 1
	if n == 0:
		return 1
	return n * factorial(n - 1)

def cnk(n, k):
	if k > n:
		return 0
	return factorial(n) / (factorial(n - k) * factorial(k))

def main():
	data = list(map(int, input().split()))
	d = data.pop()
	data.sort()
	result = 0
	if d > data[1] - data[0]:
		result += d - (data[1] - data[0])
	if d > data[2] - data[1]:
		result += d - (data[2] - data[1])
	print(result)
main()


a = list(map(int, input().split()))
d = a[-1]
s = 0
a.pop()
a.sort()
c = a[1] - a[0]
x = a[2] - a[1]
if x < d:
	s = d - x
if c < d:
	s += d - c
print(s)


(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted([a, b, c])
ans1 = 10 ** 10
if c - a >= 2 * d:
	ans1 = max(0, d - min(b - a, c - b))
ans2 = max(0, d - b + a) + max(0, d - c + b)
print(min(ans1, ans2))


class cinn:

	def __init__(self):
		self.x = []

	def cin(self, t=int):
		if len(self.x) == 0:
			a = input()
			self.x = a.split()
			self.x.reverse()
		return self.get(t)

	def get(self, t):
		return t(self.x.pop())

	def clist(self, n, t=int):
		l = [0] * n
		for i in range(n):
			l[i] = self.cin(t)
		return l

	def clist2(self, n, t1=int, t2=int, t3=int, tn=2):
		l = [0] * n
		for i in range(n):
			if tn == 2:
				a1 = self.cin(t1)
				a2 = self.cin(t2)
				l[i] = (a1, a2)
			elif tn == 3:
				a1 = self.cin(t1)
				a2 = self.cin(t2)
				a3 = self.cin(t3)
				l[i] = (a1, a2, a3)
		return l

	def clist3(self, n, t1=int, t2=int, t3=int):
		return self.clist2(self, n, t1, t2, t3, 3)

	def cout(self, i, ans=''):
		if ans == '':
			print('Case #' + str(i + 1) + ':', end=' ')
		else:
			print('Case #' + str(i + 1) + ':', ans)

	def printf(self, thing):
		print(thing, end='')

	def countlist(self, l, s=0, e=None):
		if e == None:
			e = len(l)
		dic = {}
		for el in range(s, e):
			if l[el] not in dic:
				dic[l[el]] = 1
			else:
				dic[l[el]] += 1
		return dic

	def talk(self, x):
		print(x, flush=True)

	def dp1(self, k):
		L = [-1] * k
		return L

	def dp2(self, k, kk):
		L = [-1] * k
		for i in range(k):
			L[i] = [-1] * kk
		return L

	def isprime(self, n):
		if n == 1 or n == 0:
			return False
		for i in range(2, int(n ** 0.5 + 1)):
			if n % i == 0:
				return False
		return True

	def factors(self, n):
		from functools import reduce
		return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

	def nthprime(self, n):
		i = 0
		s = 2
		L = []
		while i < n:
			while not self.isprime(s):
				s += 1
			L.append(s)
			s += 1
			i += 1
		return L

	def matrixin(self, m, n, t=int):
		L = []
		for i in range(m):
			p = self.clist(n, t)
			L.append(p)
		return L

	def seive(self, k):
		n = k + 1
		L = [True] * n
		L[1] = False
		L[0] = False
		for i in range(2, n):
			if L[i] == True:
				for j in range(2 * i, n, i):
					L[j] = False
		return L

	def seiven(self, n, L):
		i = 0
		for j in range(len(L)):
			if L[j] == True:
				i += 1
			if i == n:
				return j

	def matrixin2(self, m, t=int):
		L = []
		for i in range(m):
			iny = self.cin(str)
			lsmall = []
			for el in iny:
				lsmall.append(t(el))
			L.append(lsmall)
		return L
c = cinn()
x = c.cin()
y = c.cin()
z = c.cin()
d = c.cin()
L = [x, y, z]
L.sort()
mid = L[1]
right = L[2]
left = L[0]
s = 0
if right - mid < d:
	s += d - (right - mid)
if mid - left < d:
	s += d - (mid - left)
print(s)


from sys import stdin
input = stdin.readline
(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
out = 0
if abs(l[1] - l[2]) < d:
	out += d - abs(l[1] - l[2])
if abs(l[1] - l[0]) < d:
	out += d - abs(l[1] - l[0])
print(out)


(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted([a, b, c])
(l1, l2) = (d - (b - a), d - (c - b))
s = 0
if l1 > 0:
	s += l1
if l2 > 0:
	s += l2
print(s)


import sys
input = sys.stdin.buffer.readline
import os
from math import *
arr = list(map(int, input().split()))
d = arr[-1]
arr.pop()
arr.sort()
d1 = arr[1] - arr[0]
d2 = arr[2] - arr[1]
ans = 0
if d1 < d:
	ans += d - d1
if d2 < d:
	ans += d - d2
print(ans)


(a, b, c, d) = map(int, input().split())
l = []
l.append(a)
l.append(b)
l.append(c)
l = sorted(l)
ans = max(0, d - (l[1] - l[0])) + max(0, d - (l[2] - l[1]))
print(ans)


(a, b, c, d) = map(int, input().split(' '))
x = [a, b, c]
x = sorted(x)
s = 0
if abs(x[0] - x[1]) < d:
	s = d - abs(x[0] - x[1])
if abs(x[1] - x[2]) < d:
	s = s + d - abs(x[1] - x[2])
print(s)


s = input()
s = s.split(' ')
s = list(map(int, s))
d = s[-1]
s.pop(3)
s.sort()
step = 0
if s[1] - s[0] >= d:
	pass
else:
	step = d - (s[1] - s[0])
if s[2] - s[1] >= d:
	pass
else:
	step = d - (s[2] - s[1]) + step
print(step)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
import random
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)
(a, b, c, d) = LI()
rope = []
rope.append(a)
rope.append(b)
rope.append(c)
rope.sort()
counter = 0
pointer = 0
if rope[1] - rope[0] < d:
	temp = d - (rope[1] - rope[0])
	rope[0] = rope[0] - abs(d - (rope[1] - rope[0]))
	counter = counter + temp
if rope[2] - rope[1] < d:
	temp = d - (rope[2] - rope[1])
	rope[2] = rope[2] + temp
	counter = counter + temp
print(counter)


from bisect import *
from collections import *
from itertools import *
import functools
import sys
from math import *
from decimal import *
from copy import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b):
	return abs(a[0] - b[0]) + abs(b[1] - a[1])

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN(x=' '):
	return list(map(int, sys.stdin.readline().strip().split(x)))

def eld(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def lgcd(a):
	g = a[0]
	for i in range(1, len(a)):
		g = math.gcd(g, a[i])
	return g

def ms(a):
	msf = -MAX
	meh = 0
	st = en = be = 0
	for i in range(len(a)):
		meh += a[i]
		if msf < meh:
			msf = meh
			st = be
			en = i
		if meh < 0:
			meh = 0
			be = i + 1
	return (msf, st, en)

def flush():
	return sys.stdout.flush()
(a, b, c, d) = arrIN()
x = [[0, a], [1, b], [2, c]]
y = deepcopy(x)
x.sort(key=lambda x: x[1])
if x[1][1] - x[0][1] < d:
	x[0][1] = x[1][1] - d
if x[2][1] - x[1][1] < d:
	x[2][1] = x[1][1] + d
s = 0
for i in x:
	s += abs(y[i[0]][1] - i[1])
print(s)


(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
a1 = l[1] - d
c2 = l[1] + d
steps = 0 if l[0] < a1 else abs(a1 - l[0])
steps += 0 if l[2] > c2 else abs(c2 - l[2])
print(steps)


def ropes(x, d):
	ans = 0
	x.sort()
	val1 = abs(x[0] - x[1])
	val2 = abs(x[1] - x[2])
	if val1 < d:
		tmp = abs(d - val1)
		ans += tmp
	if val2 < d:
		tmp = abs(d - val2)
		ans += tmp
	return ans
(a, b, c, d) = list(map(int, input().rstrip().split()))
x = [a, b, c]
print(ropes(x, d))


(a, b, c, d) = map(int, input().split())
timer = 0
z = [a, b, c]
z.sort()
mid = z[1]
if mid - z[0] < d:
	timer = timer + (d - (mid - z[0]))
if z[2] - mid < d:
	timer = timer + (d - (z[2] - mid))
print(timer)


a = input().split()
for i in range(4):
	a[i] = int(a[i])
dist = a[3]
a.pop(3)
a.sort()
mid = a[1]
max = mid + dist
min = mid - dist
if max >= a[2] and min <= a[0]:
	time = max - a[2] + (a[0] - min)
elif max < a[2] and min > a[0]:
	time = 0
elif max < a[2]:
	time = a[0] - min
elif min > a[0]:
	time = max - a[2]
print(time)


(a, b, c, d) = map(int, input().split())
s = sorted([a, b, c])
g = s[1] - s[0]
h = s[2] - s[1]
cnt = 0
if g < d:
	cnt += d - g
if h < d:
	cnt += d - h
print(cnt)


(a, b, c, d) = map(int, input().split())
a1 = min(a, b, c)
a2 = max(a, b, c)
if a != a1 and a != a2:
	a3 = a
elif b != a1 and b != a2:
	a3 = b
else:
	a3 = c
sol = max(a3 + d - a2, 0)
sol += max(a1 - (a3 - d), 0)
print(sol)


import sys
input = sys.stdin.readline
(a, b, c, d) = map(int, input().split())
A = sorted([a, b, c])
x = A[1] - A[0]
y = A[2] - A[1]
ANS = 0
if x < d:
	ANS += d - x
if y < d:
	ANS += d - y
print(ANS)


A = list(map(int, input().split()))
(A, d) = (sorted(A[:3]), A[3])
print(max(0, d - A[1] + A[0]) + max(0, d - A[2] + A[1]))


(a, b, c, d) = list(map(int, input().split()))
t = [a, b, c]
t.sort()
k1 = abs(t[0] - t[1])
k2 = abs(t[1] - t[2])
if k1 >= d:
	k1 = 0
else:
	k1 = d - k1
if k2 >= d:
	k2 = 0
else:
	k2 = d - k2
print(k1 + k2)


(a, b, c, d) = list(map(int, input().split()))
new = sorted([a, b, c])
ans = 0
if new[1] - new[0] < d:
	ans += d - (new[1] - new[0])
if new[2] - new[1] < d:
	ans += d - (new[2] - new[1])
print(ans)


x = list(map(int, input().split()))
d = x[3]
del x[3]
x.sort()
l = [x[1] - x[0], x[2] - x[1]]
m = 0
for p in l:
	if p < d:
		m += d - p
print(m)


l = list(map(int, input().split()))
a = l[0:3]
d = l[-1]
a.sort()
c = 0
a1 = d - (a[2] - a[1])
c += max(0, a1)
a2 = d - (a[1] - a[0])
c += max(0, a2)
print(c)


(a, b, c, d) = map(int, input().split())
r = sorted([a, b, c])
an = max(0, d - (r[1] - r[0])) + max(0, d - (r[2] - r[1]))
print(an)


(a, b, c, d) = map(int, input().split())
res = 0
line = sorted([a, b, c])
if abs(line[0] - line[1]) < d:
	res += d - abs(line[0] - line[1])
if abs(line[1] - line[2]) < d:
	res += d - abs(line[1] - line[2])
print(res)


(a, b, c, d) = [int(el) for el in input().split()]

def sol_a(poses, dist):
	sorted_poses = sorted(poses)
	sum = 0
	sum += max(0, dist - sorted_poses[1] + sorted_poses[0])
	sum += max(0, dist - sorted_poses[2] + sorted_poses[1])
	return sum
print(sol_a([a, b, c], d))


mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
(a, b, c, d) = f()
(a, b, c) = sorted([a, b, c])
print(max(0, d - (b - a)) + max(0, d - (c - b)))


l = [0, 0, 0]
(l[0], l[1], l[2], d) = map(int, input().split())
l.sort()
k = 0
m = l[1] - l[0]
if m < d:
	k = k + d - m
m = l[2] - l[1]
if m < d:
	k = k + d - m
print(k)


(a, b, c, d) = list(map(int, input().split()))
l1 = min(a, min(b, c))
l3 = max(a, max(b, c))
l2 = a if a > l1 and a < l3 else b if b > l1 and b < l3 else c
cnt = l1 - min(l2 - d, l1)
cnt += max(l2 + d, l3) - l3
print(cnt)


(a, b, c, d) = map(int, input().split())
l = sorted([a, b, c])
sec = 0
d1 = l[1] - l[0]
if d1 < d:
	sec += d - d1
d2 = l[2] - l[1]
if d2 < d:
	sec += d - d2
print(sec)


abcd = input()
(a, b, c, d) = abcd.split(' ')
a = int(a)
b = int(b)
c = int(c)
d = int(d)
(a, b, c) = sorted([a, b, c])
res = 99999999999
d1 = b - a
d2 = c - b
if d1 >= d and d2 >= d:
	print(0)
elif d1 >= d or d2 >= d:
	if d2 < d:
		print(d - d2)
	elif d1 < d:
		print(d - d1)
else:
	print(2 * d - d1 - d2)


b = input()
a = b.split()
d = int(a[3])
del a[3]
a[0] = int(a[0])
a[1] = int(a[1])
a[2] = int(a[2])
a.sort()
n1 = a[0]
n2 = a[1]
n3 = a[2]
z = 0
if d > abs(n2 - n1):
	z += abs(d - abs(n2 - n1))
if d > abs(n2 - n3):
	z += abs(d - abs(n2 - n3))
print(z)


(a, b, c, n) = map(int, input().split())
(a, b, c) = sorted([a, b, c])
k = 0
if b - a < n:
	k += n - (b - a)
if c - b < n:
	k += n - (c - b)
print(k)


(a, b, c, d) = [int(x) for x in input().split()]
l = [a, b, c]
l.sort()
if l[2] - l[1] <= d and l[1] - l[0] <= d:
	print(2 * d - l[2] + l[0])
elif l[2] - l[1] > d and l[1] - l[0] > d:
	print(0)
else:
	print(max(d - l[2] + l[1], d - l[1] + l[0]))


(a, b, c, d) = map(int, input('').split(' '))
t = []
t.append(a)
t.append(b)
t.append(c)
t.sort()
count = 0
if abs(t[0] - t[1]) < d:
	p = d - abs(t[0] - t[1])
	t[0] -= p
	count += p
if abs(t[0] - t[2]) < d:
	q = d - abs(t[0] - t[2])
	t[2] += q
	count += q
if abs(t[1] - t[2]) < d:
	r = d - abs(t[1] - t[2])
	t[2] += r
	count += r
print(count)


(a, b, c, d) = map(int, input().split(' '))
l = sorted([a, b, c])
delta1 = l[1] - l[0]
if delta1 < d:
	delta1 = d - delta1
else:
	delta1 = 0
delta2 = l[2] - l[1]
if delta2 < d:
	delta2 = d - delta2
else:
	delta2 = 0
print(delta1 + delta2)


(a, b, c, d) = map(int, input().split())
p = [a, b, c]
p.sort()
t1 = max(0, d - abs(p[1] - p[0]))
t2 = max(0, d - abs(p[2] - p[1]))
print(t1 + t2)


(a, b, c, d) = map(int, input().split(' '))
dancer = [a, b, c]
dancer.sort()
counter = 0
distance = 0
while abs(dancer[0] - dancer[1]) < d or abs(dancer[1] - dancer[2]) < d:
	if abs(dancer[0] - dancer[1]) <= abs(dancer[1] - dancer[2]):
		counter += d - abs(dancer[0] - dancer[1])
		dancer[0] -= d - abs(dancer[0] - dancer[1])
	else:
		counter += d - abs(dancer[2] - dancer[1])
		dancer[2] += d - abs(dancer[2] - dancer[1])
print(counter)


(*s, d) = map(int, input().split())
s = sorted(s)
move = 0
if abs(s[0] - s[1]) < d:
	move += d - abs(s[0] - s[1])
if abs(s[1] - s[2]) < d:
	move += d - abs(s[1] - s[2])
print(move)


(a, b, c, d) = list(map(int, input().split()))
s = sorted([a, b, c])
ab = s[1] - s[0]
bc = s[2] - s[1]
if ab < d:
	if bc < d:
		print(d - s[1] + s[0] + (d - s[2] + s[1]))
	else:
		print(d - s[1] + s[0])
elif bc < d:
	print(d - s[2] + s[1])
else:
	print(0)


l = [int(i) for i in input().split()]
fl = 0
d = l[3]
del l[3]
l = sorted(l)
if l[1] - l[0] <= d:
	fl += d - (l[1] - l[0])
l = sorted(l)
if l[2] == l[0]:
	(l[2], l[0]) = (l[0], l[2])
if l[2] - l[1] <= d:
	fl += d - (l[2] - l[1])
print(fl)


import math
import sys
(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted([a, b, c])
r = 0
if b - a < d:
	r += d - (b - a)
if c - b < d:
	r += d - (c - b)
print(r)


(a, b, c, dist) = map(int, input().split())
arr = [a, b, c]
arr.sort()
a = arr[0]
b = arr[1]
c = arr[2]
if c - b >= dist and b - a >= dist:
	print(0)
else:
	shab = dist - (b - a) if b - a < dist else 0
	shbc = dist - (c - b) if c - b < dist else 0
	print(shab + shbc)


(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted((a, b, c))
f = d - (b - a)
g = d - (c - b)
m = max(0, f)
n = max(0, g)
print(m + n)


t = list(map(int, input().split(' ')))
d = t[-1]
t.remove(d)
t.sort()
a = t[0]
b = t[1]
c = t[2]
d1 = b - a
d2 = c - b
if d1 < d:
	d1 = d - d1
else:
	d1 = 0
if d2 < d:
	d2 = d - d2
else:
	d2 = 0
print(d1 + d2)


a = [int(i) for i in input().split()]
b = []
d = a[3]
t = 0
for i in range(3):
	b.append(a[i])
b.sort()
for i in range(2):
	if b[i + 1] - b[i] < d:
		t += d - (b[i + 1] - b[i])
print(t)


import math
(a, b, c, d) = map(int, input().split(' '))
dis = 2 * d
l = [a, b, c]
mi = min(a, b, c)
sl = sorted(l)
t = 0
if math.fabs(sl[2] - sl[1]) < d:
	t += math.fabs(sl[2] - (sl[1] + d))
if math.fabs(sl[0] - sl[1]) < d:
	t += math.fabs(sl[0] - (sl[1] - d))
print(int(t))


def f():
	(a, b, c, d) = list(map(int, input().split()))
	rez = 0
	if a > b:
		(a, b) = (b, a)
	if c < a:
		(a, b, c) = (c, a, b)
	elif c < b:
		(b, c) = (c, b)
	l1 = b - a
	l2 = c - b
	if l1 < d:
		rez += d - l1
	if l2 < d:
		rez += d - l2
	return rez
print(f())


arr = list(map(int, input().split()))
d = arr[3]
lst = [arr[0], arr[1], arr[2]]
lst.sort()
ans = 0
if lst[1] - lst[0] < d:
	ans += d - (lst[1] - lst[0])
if lst[2] - lst[1] < d:
	ans += d - (lst[2] - lst[1])
print(ans)


l = list(map(int, input().split()))
k = l[len(l) - 1]
del l[len(l) - 1]
l.sort()
cnt = 0
if l[2] - l[1] < k:
	cnt += k - (l[2] - l[1])
if l[1] - l[0] < k:
	cnt += k - (l[1] - l[0])
print(cnt)


input1 = input()
args = input1.split()
abc = [int(args[0]), int(args[1]), int(args[2])]
d = int(args[3])
abc.sort()
(a, b, c) = (abc[0], abc[1], abc[2])
ab = abs(a - b)
bc = abs(c - b)
abSteps = bcSteps = 0
if ab < d:
	abSteps = abs(ab - d)
if bc < d:
	bcSteps = abs(bc - d)
steps = abSteps + bcSteps
print(steps)


(a, b, c, d) = list(map(int, input().split()))
k = 0
arr = [0] * 3
arr[0] = a
arr[1] = b
arr[2] = c
arr = sorted(arr)
if arr[1] - arr[0] < d:
	k = k + d - (arr[1] - arr[0])
if arr[2] - arr[1] < d:
	k = k + d - (arr[2] - arr[1])
print(k)


arr = list(map(int, input().split()))
brr = arr[:3]
d = arr[3]
brr.sort()
(l, r) = (0, 0)
if brr[1] - brr[0] < d:
	l = brr[0] - (brr[1] - d)
if brr[2] - brr[1] < d:
	r = abs(brr[1] + d - brr[2])
print(l + r)


from random import shuffle
a = [int(i) for i in input().split()]
d = a.pop(3)
while not a[0] <= a[1] <= a[2]:
	shuffle(a)
sm1 = d - abs(a[0] - a[1])
sm2 = d - abs(a[1] - a[2])
print(sum((i for i in [sm1, sm2] if i > 0)))


str = input().split()
li = []
for i in str[:3]:
	li.append(int(i))
li.sort()
d = int(str[3])
x = abs(li[0] - li[1])
y = abs(li[1] - li[2])
if x > d:
	l = 0
else:
	l = abs(d - x)
if y > d:
	m = 0
else:
	m = abs(d - y)
print(l + m)


arr = [int(x) for x in input().split(' ')]
new = sorted(arr[:-1])
ans = 0
d1 = abs(new[0] - new[1])
d2 = abs(new[1] - new[2])
if d1 < arr[-1]:
	ans += abs(arr[-1] - d1)
if d2 < arr[-1]:
	ans += abs(arr[-1] - d2)
print(ans)


import sys
input = sys.stdin.readline
(a, b, c, d) = list(map(int, input().split()))
count = 0
s = sorted([a, b, c])
if abs(s[0] - s[1]) < d:
	count += d - abs(s[0] - s[1])
if abs(s[2] - s[1]) < d:
	count += d - abs(s[1] - s[2])
print(count)


a = list(map(int, input().split()))
k = sorted(a[:3])
o = sorted(a[:3])
if k[1] - k[0] < a[-1]:
	p = k[0] + a[-1]
if k[2] - k[1] < a[-1]:
	q = k[1] + a[-1]
try:
	k[1] = p
except:
	pass
try:
	k[2] = q
except:
	pass
l = 0
for i in range(len(k)):
	l += abs(k[i] - o[i])
print(l)


(a, b, c, d) = map(int, input().split())
r = [a, b, c]
cnt = 0
one = max(a, b, c)
r.remove(one)
two = min(a, b, c)
r.remove(two)
c = max(r)
if one - c < d:
	cnt += d - (one - c)
if c - two < d:
	cnt += d - (c - two)
print(cnt)


def main():
	(a, b, c, d) = (int(x) for x in input().split())
	(a, b, c) = sorted([a, b, c])
	return max(d - (c - b), 0) + max(d - (b - a), 0)
print(main())


(a, b, c, d) = map(int, input().split())
first = min(a, min(b, c))
last = max(a, max(b, c))
middle = a + b + c - first - last
if last - middle >= d and middle - first >= d:
	print('0')
elif middle - first < d and last - middle < d:
	print(d - (middle - first) + (d - (last - middle)))
elif middle - first >= d:
	print(d - (last - middle))
elif last - middle >= d:
	print(d - (middle - first))


(a, b, c, d) = map(int, input().split())
v = [a, b, c]
v.sort()
(a, b, c) = v
print(max(a - b + d, 0) + max(b + d - c, 0))


(a, b, c, d) = list(map(int, input().split()))
aa = min([a, b, c])
cc = max([a, b, c])
bb = a + b + c - aa - cc
print(max(0, d - cc + bb) + max(0, d - bb + aa))


import sys
input = sys.stdin.readline
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right, bisect_left
import math
(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted([a, b, c])
x = 0
if b - a < d:
	x += abs(b - a - d)
if c - b < d:
	x += abs(c - b - d)
print(x)


l = [int(x) for x in input().split()]
d = l[-1]
l = l[:-1]
l.sort()
lft = l[1] - d
req = 0
if lft < l[0]:
	req = abs(l[0] - lft)
rgt = l[1] + d
if rgt > l[2]:
	req += abs(l[2] - rgt)
print(req)


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
x = list(map(int, input().split()))
d = x.pop()
x.sort()
ans = 0
if x[1] - x[0] < d:
	ans += d - x[1] + x[0]
if x[2] - x[1] < d:
	ans += d - x[2] + x[1]
print(ans)


(a, b, c, d) = map(int, input().split())
list = [a, b, c]
list = sorted(list)
t1 = 0
t2 = 0
if list[2] - list[1] < d:
	t1 = abs(list[1] + d - list[2])
if list[1] - list[0] < d:
	t2 = abs(list[1] - d - list[0])
print(t1 + t2)


(a, b, c, d) = map(int, input().split())
s = a + b + c
x = max(a, b, c)
z = min(a, b, c)
y = s - x - z
p = 0
if y - z < d:
	p += d + z - y
if x - y < d:
	p += d + y - x
print(p)


x = list(map(int, input().split()))
y = sorted(x[:3])
a = y[1] - y[0]
b = y[2] - y[1]
hola = 0
if a >= x[3] and b < x[3]:
	y[2] += x[3] - b
	hola += x[3] - b
if a < x[3] and b >= x[3]:
	y[0] -= x[3] - a
	hola += x[3] - a
if a < x[3] and b < x[3]:
	y[2] += x[3] - b
	hola += x[3] - b
	y[0] -= x[3] - a
	hola += x[3] - a
print(hola)


import math
numbers = list(map(int, input().split()))
d = numbers[3]
L = numbers[0:3].copy()
L.sort()
a = L[0]
b = L[1]
c = L[2]
S = min(c - b - d, 0) + min(b - a - d, 0)
print(-S)


(a, b, c, d) = input().split(' ')
(a, b, c, d) = (int(a), int(b), int(c), int(d))
distances = [a, b, c]
distances.sort()
(g1, g2) = (distances[1] - distances[0], distances[2] - distances[1])
res = max(d - g1, 0) + max(d - g2, 0)
print(res)


(a, b, c, d) = map(int, input().split())
l = min(a, b, c)
r = max(a, b, c)
m = a + b + c - l - r
mini = [0, d - abs(r - m)][d > abs(r - m)]
mini += [0, d - abs(m - l)][d > abs(m - l)]
print(mini)


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
(a, b, c, d) = lmp()
ml = [a, b, c]
ml.sort()
print(max(0, d - (ml[1] - ml[0])) + max(0, d - (ml[2] - ml[1])))


def main():
	buf = input()
	buflist = buf.split()
	a = int(buflist[0])
	b = int(buflist[1])
	c = int(buflist[2])
	d = int(buflist[3])
	pos = [a, b, c]
	pos.sort()
	print(max(0, d - (pos[1] - pos[0])) + max(0, d - (pos[2] - pos[1])))
main()


(a, b, c, d) = map(int, input().split())
m = [a, b, c]
m.sort()
k = 0
if m[1] - m[0] < d:
	k = k + (d - (m[1] - m[0]))
if m[2] - m[1] < d:
	k = k + (d - (m[2] - m[1]))
print(k)


def main():
	(a, b, c, d) = map(int, input().split())
	(c, b, a) = sorted([a, b, c])
	ans = 0
	if a - b < d:
		ans += d - (a - b)
	if b - c < d:
		ans += d - (b - c)
	print(ans)
main()


s = list(map(int, input().strip().split()))
w = []
for i in range(3):
	w.append(s[i])
w.sort()
a = w[0]
b = w[1]
c = w[2]
d = abs(b - a)
if d < s[3]:
	d1 = abs(s[3] - d)
else:
	d1 = 0
x = abs(c - b)
if x < s[3]:
	y = abs(s[3] - x)
else:
	y = 0
print(d1 + y)


from math import *
from cmath import *
from itertools import *
from decimal import *
from fractions import *
from sys import *
for _ in range(1):
	(a, b, c, d) = map(int, input().split())
	k = a + +b + c - min(a, b, c) - max(a, b, c)
	print(max(0, d - (k - min(a, b, c))) + max(0, d - (max(a, b, c) - k)))


li = list(map(int, input().split()))
dist = li[-1]
li = sorted(li[:-1])
a = li[1] - li[0]
b = li[2] - li[1]
ans = 0
if a < dist or b < dist:
	ans = max(0, dist - a) + max(0, dist - b)
print(ans)


(a, b, c, d) = map(int, input().split())
mid = a + b + c - min(a, b, c) - max(a, b, c)
ans = max(0, d - (max(a, b, c) - mid)) + max(0, min(a, b, c) - (mid - d))
print(ans)


def min_duration(nums, d):
	result = 0
	for i in range(2):
		if nums[i] - nums[i + 1] < d:
			result += d - (nums[i] - nums[i + 1])
	return result
nums = list(map(int, input().split()))
(nums, d) = (nums[:-1], nums[-1])
print(min_duration(sorted(nums, reverse=True), d))


a = list(map(int, input().split()))
b = a[-1]
a.remove(a[-1])
a.sort()
answer = 0
if abs(a[1] - a[0]) < b:
	answer += b - abs(a[1] - a[0])
if abs(a[2]) - a[1] < b:
	answer += b - abs(a[2] - a[1])
print(answer)


(a, b, c, d) = map(int, input().split())

def compute(x, y, d):
	if abs(x - y) >= d:
		return 0
	return d - abs(x - y)
(a, b, c) = sorted([a, b, c])
print(compute(a, b, d) + compute(b, c, d))


(a, b, c, d) = map(int, input().split())
e = sorted([a, b, c])
f = 0
if e[1] - e[0] < d:
	f = d - e[1] + e[0]
if e[2] - e[1] < d:
	f += d - e[2] + e[1]
print(f)


x = [int(i) for i in input().split()]
y = [x[0], x[1], x[2]]
y.sort()
c = 0
for i in range(2):
	if y[i + 1] - y[i] < x[3]:
		c = c + (x[3] - (y[i + 1] - y[i]))
print(c)


t = 1
for test in range(t):
	(a, b, c, d) = map(int, input().split())
	arr = [a, b, c]
	arr.sort()
	a = arr[0]
	b = arr[1]
	c = arr[2]
	ans = 0
	if b - a < d:
		ans += d - b + a
	if c - b < d:
		ans += d - c + b
	print(ans)


l = list(map(int, input().split()))
(a, b, c) = sorted(l[:-1])
d = l[-1]
if b - a >= d and c - b >= d:
	print(0)
else:
	x = (d - (b - a) if b - a < d else 0) + (d - (c - b) if c - b < d else 0)
	print(x)


(a, b, c, d) = map(int, input().split())
list1 = []
list1.append(a)
list1.append(b)
list1.append(c)
list1.sort()
x = list1[1] - list1[0]
y = list1[2] - list1[1]
if x >= d:
	z = 0
else:
	z = d - x
if y >= d:
	z1 = 0
else:
	z1 = d - y
print(z + z1)


(a, b, c, d) = map(int, input().split())
k = 0
p = max(a, b, c)
q = min(a, b, c)
l = a + b + c - p - q
if p - l < d:
	k += d - (p - l)
if l - q < d:
	k += d - (l - q)
print(k)


(a, b, c, d) = map(int, input().split())
s = [a, b, c]
s.sort()
l = s[0]
m = s[1]
n = s[2]
q = d - (m - l)
w = d - (n - m)
if q < 0:
	q = 0
if w < 0:
	w = 0
print(q + w)


from math import fabs
(a, b, c, d) = map(int, input().split())
temp = [a, b, c]
temp.sort()
a = temp[0]
b = temp[1]
c = temp[2]
count = 0
dist_ab = int(fabs(a - b))
dist_bc = int(fabs(b - c))
if not (dist_ab >= d and dist_bc >= d):
	if dist_ab < d:
		count += d - dist_ab
	if dist_bc < d:
		count += d - dist_bc
print(count)


l = list(map(int, input().split()))
d = l[3]
l.pop()
l.sort()
x = l[1] - l[0]
y = l[2] - l[1]
a = 1
b = 1
if x >= d:
	a = 0
if y >= d:
	b = 0
ans = (d - x) * a + (d - y) * b
print(ans)


(a, b, c, d) = map(int, input().split(' '))
arr = [a, b, c]
arr.sort()
ans = max(d - abs(arr[0] - arr[1]), 0) + max(d - abs(arr[2] - arr[1]), 0)
print(ans)


l = list(map(int, input().split()))
a = l.pop()
l.sort()
c = 0
for i in range(len(l) - 1):
	if l[i + 1] - l[i] < a:
		c += abs(a - l[i + 1] + l[i])
print(c)


sorted_list = list((int(x) for x in input().split()))
d = sorted_list.pop()
count = 0
sorted_list.sort()
min = sorted_list[0]
mid = sorted_list[1]
max = sorted_list[2]
if mid - min < d:
	count += d - (mid - min)
if max - mid < d:
	count += d - (max - mid)
print(count)


l = list(map(int, input().split()))
d = l[3]
l.pop()
l.sort()
t1 = d - (l[2] - l[1])
t2 = d - (l[1] - l[0])
if t1 < 0:
	t1 = 0
if t2 < 0:
	t2 = 0
print(t1 + t2)


(a, b, c, d) = [int(s) for s in input().split(' ')]
positions = [a, b, c]
positions.sort()
print(max(0, d - (positions[2] - positions[1])) + max(0, d - (positions[1] - positions[0])))


import sys, collections, math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 1000000007
(a, b, c, d) = get_ints()
(a, b, c) = sorted((a, b, c))
print(max(0, d - (b - a)) + max(0, d - (c - b)))


A = [int(n) for n in input().split()]
(a, b, c, d) = A
abc = [a, b, c]
abc.sort()
count = 0
for i in range(2):
	diff = abc[i + 1] - abc[i]
	if d > abc[i + 1] - abc[i]:
		count += d - (abc[i + 1] - abc[i])
print(count)


arr = list(map(int, input().split()))
d = arr[-1]
arr.pop()
arr.sort()
(first, second) = (abs(arr[1] - arr[0]), abs(arr[2] - arr[1]))
seconds = 0
if first >= d and second >= d:
	print(0)
elif first >= d:
	print(d - second)
elif second >= d:
	print(d - first)
else:
	print(d - second + d - first)


a = list(map(int, input().split()))
b = [a[0], a[1], a[2]]
b = sorted(b)
a1 = abs(b[0] - b[1])
a2 = abs(b[1] - b[2])
a3 = abs(b[0] - b[2])
count = 0
if a1 < a[3]:
	count = count + a[3] - a1
	b[0] = b[0] - abs(a3 - a[3])
	a3 = abs(b[0] - b[2])
if a2 < a[3]:
	b[2] = b[2] + abs(a2 - a[3])
	count = count + a[3] - a2
	a3 = abs(b[0] - b[2])
	a2 = abs(b[1] - b[2])
if a3 < a[3]:
	b[2] = b[2] + abs(a2 - a[3])
	count = count + a[3] - a2
print(count)


(a, b, c, d) = list(map(int, input().split()))
l = [a, b, c]
l.sort()
d1 = abs(l[0] - l[1])
d2 = abs(l[1] - l[2])
if d1 >= d:
	time1 = 0
else:
	time1 = d - d1
if d2 >= d:
	time2 = 0
else:
	time2 = d - d2
print(time1 + time2)


l = list(map(int, input().split()))
d = l.pop(3)
l.sort()
dif1 = l[1] - l[0]
dif2 = l[2] - l[1]
count = 0
if dif1 < d:
	count += d - dif1
if dif2 < d:
	count += d - dif2
print(count)


import math
s = [1, 2, 3]
d = 0
k = 0
s1 = list(map(int, input().split()))
s[0] = s1[0]
s[1] = s1[1]
s[2] = s1[2]
d = s1[3]
s.sort()
if abs(s[1] - s[0]) < d:
	k = k + d - abs(s[1] - s[0])
if abs(s[2] - s[1]) < d:
	k = k + d - abs(s[2] - s[1])
print(k)


(a, b, c, d) = map(int, input().split())
[q, w, e] = sorted([a, b, c])
t = 0
d1 = e - w
d2 = w - q
if d1 < d:
	t += d - d1
if d2 < d:
	t += d - d2
print(t)


a = list(map(int, input().split(' ')))
d = a.pop()
a.sort()
m1 = a[0]
m2 = a[1]
m3 = a[2]
if m1 == m2:
	time = d
elif abs(m2 - m1) < d:
	time = d - abs(m2 - m1)
else:
	time = 0
if m2 == m3:
	time += d
elif abs(m3 - m2) < d:
	time += d - abs(m3 - m2)
else:
	time += 0
print(time)


(a, b, c, d) = map(int, input().split())
s = [a, b, c]
s.sort()
(a, b, c) = s
ans = 0
if b - a < d:
	ans += abs(d - (b - a))
if c - b < d:
	ans += abs(d - (c - b))
print(ans)


(a, b, c, d) = (int(x) for x in input().split())
li = [a, b, c]
li.sort()
(ba, cb) = (li[1] - li[0], li[2] - li[1])
output = 0
if ba < d:
	output += d - ba
if cb < d:
	output += d - cb
print(output)


(a, b, k, d) = list(map(int, input().split()))
l = [a, b, k]
l.sort()
(a, b, k) = l
t = 0
if b - a < d:
	mov = min(d, d - b + a)
	t += mov
	a -= mov
if k - b < d:
	mov = min(d, d - k + b)
	k += mov
	t += mov
print(t)


(a, b, c, d) = map(int, input().split())
e = [a, b, c]
e.sort()
max1 = max(0, d - abs(e[0] - e[1]))
max2 = max(0, d - abs(e[1] - e[2]))
print(max1 + max2)


def findMid(a, b, c):
	if a <= b <= c or c <= b <= a:
		return b
	if b <= a <= c or c <= a <= b:
		return a
	if a <= c <= b or b <= a <= c:
		return c
(a, b, c, d) = map(int, input().split())
Mid = findMid(a, b, c)
Ans = 0
if Mid == a:
	if abs(b - a) < d:
		Ans += d - abs(b - a)
	if abs(c - a) < d:
		Ans += d - abs(c - a)
elif Mid == b:
	if abs(a - b) < d:
		Ans += d - abs(b - a)
	if abs(c - b) < d:
		Ans += d - abs(c - b)
else:
	if abs(c - a) < d:
		Ans += d - abs(c - a)
	if abs(c - b) < d:
		Ans += d - abs(c - b)
print(Ans)


def get_input():
	try:
		strg = input()
		return strg
	except EOFError:
		return ''

def make_int_list(strg):
	lst = strg.split()
	int_list = []
	for i in range(0, len(lst)):
		int_list.append(int(lst[i]))
	return int_list

def find_sec(num, d):
	if num > d:
		return 0
	else:
		return d - num

def find_min_seconds(lst, d):
	num1 = lst[1] - lst[0]
	num2 = lst[2] - lst[1]
	return find_sec(num1, d) + find_sec(num2, d)
inputs = make_int_list(get_input())
a = inputs[0]
b = inputs[1]
c = inputs[2]
d = inputs[3]
lst = [a, b, c]
lst.sort()
print(find_min_seconds(lst, d))


l = list(map(int, input().split()))
d = l[-1]
l.pop()
l.sort()
if l[1] - l[0] < d:
	f = d - (l[1] - l[0])
else:
	f = 0
if l[2] - l[1] < d:
	c = d - (l[2] - l[1])
else:
	c = 0
print(f + c)


s = [int(x) for x in input().split()]
d = s[3]
s1 = []
s1.append(s[0])
s1.append(s[1])
s1.append(s[2])
ans = 0
s1.sort()
a = s1[0]
b = s1[1]
c = s1[2]
if abs(a - b) >= d and abs(b - c) >= d:
	ans = 0
elif abs(a - b) >= d:
	ans += d - abs(b - c)
elif abs(b - c) >= d:
	ans = d - abs(a - b)
else:
	ans = d - abs(a - b) + d - abs(b - c)
print(ans)


lista = input().split(' ')
a = int(lista[0])
b = int(lista[1])
c = int(lista[2])
d = int(lista[3])
v = list()
rs = int(0)
v.append(int(a))
v.append(int(b))
v.append(int(c))
t = int(d)
v = sorted(v)
d1 = int(v[1] - v[0])
d2 = int(v[2] - v[1])
if t - d1 >= 0:
	rs = rs + (t - d1)
if t - d2 >= 0:
	rs = rs + (t - d2)
print(rs)


(a, b, c, d) = input().split()
a = int(a)
b = int(b)
c = int(c)
d = int(d)
if a > b:
	t = b
	b = a
	a = t
if b > c:
	t = b
	b = c
	c = t
if a > b:
	t = b
	b = a
	a = t
d1 = b - a
d2 = c - b
c = 0
if d1 < d:
	c = c + d - d1
if d2 < d:
	c = c + d - d2
print(c)


(a, b, c, d) = input().strip().split(' ')
(a, b, c, d) = [int(a), int(b), int(c), int(d)]
r = []
x = 0
r.append(a)
r.append(b)
r.append(c)
r.sort()
if abs(r[0] - r[1]) < d:
	x += d - abs(r[0] - r[1])
	if abs(r[2] - r[1]) < d:
		x += d - abs(r[2] - r[1])
elif abs(r[2] - r[1]) < d:
	x += d - abs(r[2] - r[1])
print(x)


data = [int(i) for i in input().split()]
d = data[-1]
data = data[:-1]
data.sort()
f = data[1] - data[0]
g = data[2] - data[1]
print(max(0, d - f) + max(0, d - g))


l = list(map(int, input().split()))
k = l[:-1]
d = l[-1]
k = sorted(k)
c = 0
if k[1] - k[0] < d:
	c += d - (k[1] - k[0])
if k[2] - k[1] < d:
	c += d - (k[2] - k[1])
print(c)
1


inpt = input()
inptArr = inpt.split(' ')
dist = []
for x in inptArr:
	dist.append(int(x))
arr = dist
minVal = arr[len(arr) - 1]
arr = arr[0:len(arr) - 1]
arr.sort()
time = 0
while 1 == 1:
	check = 0
	for x in range(len(arr)):
		if x == 0:
			if arr[x + 1] - arr[x] < minVal:
				time = time + (minVal - (arr[x + 1] - arr[x]))
				arr[0] = arr[0] - (minVal - (arr[x + 1] - arr[x]))
				continue
		elif x == len(arr) - 1:
			if arr[x] - arr[x - 1] < minVal:
				time = time + (minVal - (arr[x] - arr[x - 1]))
				arr[x] = arr[x] + (minVal - (arr[x] - arr[x - 1]))
				continue
		elif arr[x] - arr[x - 1] + arr[x + 1] - arr[x] >= 2 * minVal:
			if arr[x] - arr[x - 1] < minVal and arr[x + 1] - arr[x] >= minVal + (minVal - (arr[x] - arr[x - 1] < minVal)):
				time = time + (minVal - (arr[x] - arr[x - 1] < minVal))
				arr[x] = arr[x] + (minVal - (arr[x] - arr[x - 1] < minVal))
				continue
			elif arr[x] - arr[x - 1] >= minVal + 1 and arr[x + 1] - arr[x] < minVal + (minVal - (arr[x] - arr[x - 1])):
				time = time + (minVal - (arr[x] - arr[x - 1]))
				arr[x] = arr[x] - (minVal - (arr[x] - arr[x - 1]))
				continue
		else:
			continue
		check = check + 1
	if check == len(arr):
		break
print(time)


ii = lambda : int(input())
mi = lambda : list(map(int, input().split()))
li = lambda : list(mi())
(a, b, c, d) = mi()
(a, b, c) = sorted([a, b, c])
ans = max(0, d - (b - a)) + max(0, d - (c - b))
print(ans)


l = list(map(int, input().split()))
d = l[3]
l = sorted(l[:3])
s = l[1] - l[0]
s1 = l[2] - l[1]
ans = 0
if s < d:
	ans += d - s
if s1 < d:
	ans += d - s1
print(ans)


def solve():
	(a, b, c, d) = map(int, input().split())
	A = sorted([a, b, c])
	d1 = d - (A[1] - A[0]) if A[1] - A[0] < d else 0
	d2 = d - (A[2] - A[1]) if A[2] - A[1] < d else 0
	print(d1 + d2)
solve()


(a, b, c, d) = map(int, input().split())
a1 = abs(a - b)
a2 = abs(a - c)
a3 = abs(b - c)
ans = 0
(a, b, c) = sorted([a, b, c])
print(max(d - (b - a), 0) + max(d - (c - b), 0))


a = list(map(int, input().split()))
d = a[-1]
a.pop(-1)
a.sort()
b = a[1] - a[0]
c = a[2] - a[1]
if b >= d:
	b = d
if c >= d:
	c = d
print(2 * d - (b + c))


import sys
(a, b, c, d) = map(int, input().split())
c_a = a
c_c = c
(a, c) = (min(a, b, c), max(a, b, c))
b = c_a + c_c + b - a - c
d_c = b + d - c
if d_c < 0:
	d_c = 0
a = b - a + b
d_a = b + d - a
if d_a < 0:
	d_a = 0
print(d_a + d_c)


import sys

class ARopewalkers:

	def solve(self):
		(a, b, c, d) = [int(_) for _ in input().split()]
		(a, b, c) = sorted([a, b, c])
		r = max(0, d - c + b)
		l = max(0, d - b + a)
		print(l + r)
solver = ARopewalkers()
input = sys.stdin.readline
solver.solve()


(a, b, c, d) = map(int, input().split())
s = sorted([a, b, c])
print(max(0, d - abs(s[1] - s[0])) + max(0, d - abs(s[-1] - s[1])))


(a, b, c, d) = map(int, input().split())
l = sorted([a, b, c])
res = 0
if l[1] - l[0] < d:
	res += d - l[1] + l[0]
if l[2] - l[1] < d:
	res += d - l[2] + l[1]
print(res)


a = list(map(int, input().split()))
d = a.pop()
a.sort()
f = 0
for i in range(len(a) - 1):
	if a[i + 1] - a[i] < d:
		f += d - a[i + 1] + a[i]
print(f)


a = [int(x) for x in input().split()]
b = sorted(a[:3])
minn = b[0]
midn = b[1]
maxn = b[2]
print('{}'.format(max(0, max(0, a[3] - (maxn - midn)) + max(0, a[3] - (midn - minn)))))


R = lambda : map(int, input().split())
a = list(R())
dist = a[0:3]
d = a[-1]
dist.sort()
first = max(0, d - (dist[1] - dist[0]))
second = max(0, d - (dist[2] - dist[1]))
print(first + second)


n = list(map(int, input().split()))
q = n[3]
n.remove(n[3])
c = 0
p = sorted(n)
w = abs(p[0] - p[1])
x = abs(p[1] - p[2])
y = abs(p[0] - p[2])
if w <= q:
	c += q - w
if x <= q:
	c += q - x
print(c)


a = list(map(int, input().split()))
b = []
for i in range(3):
	b.append(a[i])
b.sort()
j = a[3] - b[2] + b[1]
k = a[3] - b[1] + b[0]
x = 0
if j > 0:
	x += j
if k >= 0:
	x += k
print(x)


(a, b, c, d) = input().split()
a = int(a)
b = int(b)
c = int(c)
d = int(d)
l = [a, b, c]
l = sorted(l)
x = l[1] - l[0]
y = l[2] - l[1]
step = 0
while y != d or x != d:
	if x < d and y >= d:
		step += abs(d - x)
		break
	elif x >= d and y < d:
		step += abs(d - y)
		break
	elif x < d and y < d:
		step += abs(x - d)
		step += abs(y - d)
		break
	x = l[1] - l[0]
	y = l[2] - l[1]
	break
print(step)


import sys
IS_LOCAL = False

def readInput():
	return tuple(map(int, input().split()))

def writeResult(r):
	print(r)

def main():
	(a, b, c, d) = (5, 2, 6, 3) if IS_LOCAL else readInput()
	(a, b, c) = sorted([a, b, c])
	da = b - a
	dc = c - b
	t = max(0, d - da) + max(0, d - dc)
	writeResult(t)
if len(sys.argv) > 1 and sys.argv[1] == 'True':
	IS_LOCAL = True
main()


import math
str = input().split(' ')
a = int(str[0])
b = int(str[1])
c = int(str[2])
ar = [a, b, c]
ar.sort()
ar.reverse()
d = int(str[3])
ans = max(0, d - (ar[0] - ar[1])) + max(0, d - (ar[1] - ar[2]))
print(ans)


(a, b, c, d) = map(int, input().split())
li = [a, b, c]
li.sort()
cn = 0
a = li[0]
b = li[1]
c = li[2]
if abs(a - b) >= d:
	cn += 0
else:
	pos1 = b - d
	cn += abs(pos1 - a)
if abs(b - c) >= d:
	cn += 0
else:
	pos2 = b + d
	cn += abs(c - pos2)
print(cn)


iarr = list(map(int, input().split()))
d = iarr[3]
iarr = iarr[:3]
iarr.sort()
a = iarr[0]
b = iarr[1]
c = iarr[2]
diff1 = b - a
diff2 = c - b
if diff1 > d and diff2 > d:
	print(0)
elif diff1 > d and diff2 <= d:
	print(d - diff2)
elif diff1 <= d and diff2 > d:
	print(d - diff1)
else:
	print(2 * d - diff1 - diff2)


import math
import os
import random
import re
import sys
(*a, d) = map(int, input().split())
(a, b, c) = sorted(a)
print(max(0, d - (b - a)) + max(0, d - (c - b)))


a = [int(x) for x in input().split()]
b = sorted(a[:3])
print(2 * a[3] - min(a[3], b[2] - b[1]) - min(a[3], b[1] - b[0]))


a = []
(x, y, z, d) = map(int, input().split())
a.append(x)
a.append(y)
a.append(z)
a.sort()
ans = 0
if a[1] - a[0] < d:
	ans += d - (a[1] - a[0])
if a[2] - a[1] < d:
	ans += d - (a[2] - a[1])
print(ans)


(a, b, c, d) = map(int, input().split())
x = [a, b, c]
x.sort()
r1 = x[1] - x[0]
r2 = x[2] - x[1]
res = 0
if r1 < d:
	res += d - r1
if r2 < d:
	res += d - r2
print(res)


(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
a = arr[0]
b = arr[1]
c = arr[2]
d1 = b - a
d2 = c - b
t1 = max(0, d - d1)
t2 = max(0, d - d2)
ans = t1 + t2
print(ans)


l = [int(s) for s in input().split()]
a = l[0]
b = l[1]
c = l[2]
d = l[3]
re = []
re.append(a)
re.append(b)
re.append(c)
re.sort()
aa = re[0]
bb = re[1]
cc = re[2]
re1 = abs(aa - bb)
re2 = abs(cc - bb)
if re1 >= d:
	re1 = 0
else:
	re1 = abs(re1 - d)
if re2 >= d:
	re2 = 0
else:
	re2 = abs(re2 - d)
print(re1 + re2)


def _in():
	return int(input())

def _ia(t=int):
	return list(map(t, input().split()))
a = _ia()
d = a[-1]
a = a[:-1]
a.sort()
x = max(0, d - (a[1] - a[0])) + max(0, d - (a[2] - a[1]))
x = max(0, x)
print(x)


def getN():
	return int(input())

def getList():
	return list(map(int, input().split()))
(a, b, c, d) = getList()
nums = [a, b, c]
nums.sort()
ans = 0
(a, b, c) = nums
if b - a < d:
	ans += d - (b - a)
if c - b < d:
	ans += d - (c - b)
print(ans)


(a, b, c, d) = input().split()
(a, b, c, d) = [int(a), int(b), int(c), int(d)]
l = [a, b, c]
l.sort()
output = 0
one = abs(l[1] - l[0])
if one < d:
	output += d - one
two = abs(l[2] - l[1])
if two < d:
	output += d - two
print(output)


def main():
	(a, b, c, d) = map(int, input().split())
	abc = sorted([a, b, c])
	ans = 0
	if abc[1] - abc[0] < d:
		ans += abc[0] + d - abc[1]
	if abc[2] - abc[1] < d:
		ans += abc[1] + d - abc[2]
	print(ans)
	return 0
main()


def inputSeperator(inputStr):
	return list(map(int, inputStr.split(' ')))
counter = 0
temp = inputSeperator(input())
myList = sorted(temp[0:3])
d = temp[3]
temp = d - abs(myList[1] - myList[0])
if temp > 0:
	counter += temp
temp = d - abs(myList[2] - myList[1])
if temp > 0:
	counter += temp
print(counter)


(a, b, c, d) = map(int, input().split())
li = [a, b, c]
li = sorted(li)
ans = 0
if abs(li[0] - li[1]) >= d:
	ans += 0
else:
	ans += d - abs(li[0] - li[1])
if abs(li[1] - li[2]) >= d:
	ans += 0
else:
	ans += d - abs(li[1] - li[2])
print(ans)


(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
(x, y) = (arr[1] - arr[0], arr[2] - arr[1])
if x >= d and y >= d:
	print(0)
elif x >= d or y >= d:
	print(d - min(x, y))
else:
	print(2 * d - (x + y))


a = list(map(int, input().split()))
dist = a.pop()
a.sort()
print(max(0, dist - abs(a[1] - a[0])) + max(0, dist - abs(a[2] - a[1])))


from collections import defaultdict
from collections import OrderedDict
arr = list(map(int, input().split()))
d = arr[-1]
arr = arr[:-1]
arr.sort()
dif1 = abs(arr[1] - arr[0])
ans = 0
if dif1 < d:
	ans += d - dif1
dif2 = abs(abs(arr[2]) - abs(arr[1]))
if dif2 < d:
	ans += d - dif2
print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	(a, b, c, d) = LI()
	t = sorted([a, b, c])
	(a, b, c) = t
	r = max(0, d - (b - a))
	r += max(0, d - (c - b))
	return r
print(main())


temp = [int(i) for i in input().split()]
(pos, d) = (temp[:3], temp[-1])
pos.sort()
count = 0
if pos[1] - pos[0] < d:
	count += d - (pos[1] - pos[0])
if pos[2] - pos[1] < d:
	count += d - (pos[2] - pos[1])
print(count)


(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
time = 0
if arr[1] - arr[0] < d:
	time += d - (arr[1] - arr[0])
if arr[2] - arr[1] < d:
	time += d - (arr[2] - arr[1])
print(time)


import math
(a, b, c, d) = [int(s) for s in input().split(' ')]
p = [a, b, c]
p.sort()
ans = 0
if d > abs(p[1] - p[0]):
	ans += d - abs(p[1] - p[0])
if d > abs(p[1] - p[2]):
	ans += d - abs(p[1] - p[2])
print(ans)


(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
(a, b, c) = arr
p1 = b - a
p2 = c - b
ans = 0
if p1 < d:
	ans += d - p1
if p2 < d:
	ans += d - p2
print(ans)


(a, b, c, d) = map(int, input().split())
A = [a, b, c]
A.sort()
count = 0
if A[1] - A[0] < d:
	count = count + d - A[1] + A[0]
if A[2] - A[1] < d:
	count = count + d - A[2] + A[1]
print(count)


(a, b, c, n) = map(int, input().split())
q = []
q.append(a)
q.append(b)
q.append(c)
q.sort()
w = q[1] - q[0]
e = q[2] - q[1]
if w < n and e < n:
	ans = n - w
	ans += n - e
elif w < n:
	ans = n - w
elif e < n:
	ans = n - e
else:
	ans = 0
print(ans)


(a, b, c, d) = map(int, input().split())
a1 = min(a, b, c)
c1 = max(a, b, c)
numbers = [a, b, c]
numbers.remove(a1)
numbers.remove(c1)
b1 = numbers[0]
answer = 0
if c1 - a1 < d:
	answer += d - (c1 - a1)
	c1 += d - (c1 - a1)
if c1 - b1 < d:
	answer += d - (c1 - b1)
	c1 += d - (c1 - b1)
if b1 - a1 < d:
	answer += d - (b1 - a1)
	c1 += d - (b1 - a1)
print(answer)


x = [int(i) for i in input().split()]
a = x[0]
b = x[1]
c = x[2]
d = x[3]
y = [a, b, c]
y.sort()
a = y[0]
b = y[1]
c = y[2]
ans = 0
diff1 = abs(b - a)
diff2 = abs(c - b)
if diff1 >= d and diff2 >= d:
	print(0)
elif diff1 >= d:
	print(d - diff2)
elif diff2 >= d:
	print(d - diff1)
else:
	print(d - diff1 + d - diff2)


(a, b, c, d) = map(int, input().split())
list1 = []
list1.append(a)
list1.append(b)
list1.append(c)
list1.sort()
sum = 0
if abs(list1[1] - list1[0]) < d:
	sum += d - abs(list1[1] - list1[0])
if abs(list1[1] - list1[2]) < d:
	sum += d - abs(list1[1] - list1[2])
print(sum)


import sys, math

def read_int():
	return int(sys.stdin.readline().strip())

def read_int_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def read_string():
	return sys.stdin.readline().strip()

def read_string_list(delim=' '):
	return sys.stdin.readline().strip().split(delim)

def print_list(l):
	print(' '.join(map(str, l)))
[a, b, c, d] = read_int_list()
[a, b, c] = sorted([a, b, c])
ans = 0
if b - a < d:
	ans += d - (b - a)
if c - b < d:
	ans += d - (c - b)
print(ans)


def dists(a, b, c):
	return (abs(a - b), abs(b - c))

def main():
	(a, b, c, d) = map(int, input().split())
	tmp = [a, b, c]
	tmp.sort()
	(a, b, c) = tmp
	(d1, d2) = dists(a, b, c)
	if d1 >= d and d2 >= d:
		print(0)
	elif d1 + d2 == d * 2:
		print(abs(d - d1))
	elif d1 < d and d2 < d:
		print(abs(d - d1) + abs(d - d2))
	elif d2 >= d > d1:
		print(abs(d - d1))
	elif d1 >= d > d2:
		print(abs(d - d2))
main()


def main():
	coords = list(map(int, input().split()))
	d = coords.pop()
	coords.sort()
	d1 = coords[1] - coords[0]
	d2 = coords[2] - coords[1]
	print(max(d - d1, 0) + max(d - d2, 0))
main()


(a, b, c, d) = list(map(int, input().split()))
s = [a, b, c]
s.sort()
sec = 0
if s[1] - s[0] < d:
	sec += d - (s[1] - s[0])
if s[2] - s[1] < d:
	sec += d - (s[2] - s[1])
print(sec)


def main():
	(a, b, c, d) = map(int, input().split())
	res = 0
	arr = [a, b, c]
	arr.sort()
	if arr[0] > arr[1] - d:
		res += arr[0] + d - arr[1]
	if arr[2] < arr[1] + d:
		res += d + arr[1] - arr[2]
	print(res)
main()


(a, b, c, d) = map(int, input().split())
l = []
l.append(a)
l.append(b)
l.append(c)
l = sorted(l)
result = 0
if l[1] - l[0] < d:
	result += d - (l[1] - l[0])
if l[2] - l[1] < d:
	result += d - (l[2] - l[1])
print(result)


n = list(map(int, input().split()))
d = n.pop()
n.sort()
x = n[1] - n[0]
y = n[2] - n[1]
count1 = 0
count2 = 0
if x < d:
	count1 = count1 + d - x
if y < d:
	count2 = count2 + d - y
print(count2 + count1)


l = list(map(int, input().split()))
v = l[:3]
e = max(v)
f = min(v)
d = l[3]
v.remove(e)
v.remove(f)
g = v[0]
i = 0
if e - g < d:
	i = i + (d - (e - g))
if g - f < d:
	i = i + (d - (g - f))
print(i)


a = list(map(int, input().split()))
l = sorted(a[0:3])
d = a[3]
c = 0
if l[1] - l[0] < d:
	c += d - (l[1] - l[0])
if l[2] - l[1] < d:
	c += d - (l[2] - l[1])
print(c)


arr = list(map(int, input().split()))
d = arr[-1]
arr = arr[:-1]
arr.sort()
ans = 0
if arr[1] - arr[0] >= d:
	if arr[2] - arr[1] >= d:
		print(0)
	else:
		print(d - (arr[2] - arr[1]))
elif arr[2] - arr[1] >= d:
	print(d - (arr[1] - arr[0]))
else:
	dist = 2 * d - (arr[2] - arr[0])
	print(dist)


def solve(a, b, c, d):
	x = [a, b, c]
	x.sort()
	(a, b, c) = (x[0], x[1], x[2])
	r = 0
	if b - a < d:
		r += d - (b - a)
	if c - b < d:
		r += d - (c - b)
	return r

def main():
	(a, b, c, d) = list(map(int, input().split()))
	print(solve(a, b, c, d))
main()


(a, b, c, d) = list(map(int, input().split()))
x = min(a, b, c)
y = max(a, b, c)
(a, b, c) = (x, a + b + c - x - y, y)
x = b - a
y = c - b
print(max(0, d - x) + max(0, d - y))


import sys
from collections import Counter
(a, b, c, d) = map(int, sys.stdin.readline().split())
(a, b, c) = sorted([a, b, c])
print(max(0, d - (b - a)) + max(0, d - (c - b)))


l = [int(x) for x in input().split()]
m = l[-1]
x = l[:-1]
x.sort()
s = 0
if abs(x[1] - x[0]) < m:
	s += m - abs(x[1] - x[0])
if abs(x[2] - x[1]) < m:
	s += m - abs(x[2] - x[1])
print(s)


(a, b, c, d) = list(map(int, input().split()))
l = [a, b, c]
l.sort()
a = l[0]
b = l[1]
c = l[2]
count = 0
if abs(a - b) < d:
	count += d - abs(a - b)
if abs(b - c) < d:
	count += d - abs(c - b)
print(count)


x = str(input())
y = x.split()
a = y[0]
b = y[1]
c = y[2]
d = y[3]
a = int(a)
b = int(b)
c = int(c)
d = int(d)
e = [a, b, c]
e = sorted(e, reverse=True)
seconds = 0
if e[0] - e[1] < d:
	seconds += d - (e[0] - e[1])
if e[1] - e[2] < d:
	seconds += d - (e[1] - e[2])
print(seconds)


a = list(map(int, input().split()))
d = a.pop(-1)
a.sort()
left = d - (a[1] - a[0])
right = d - (a[2] - a[1])
if left < 0:
	left = 0
if right < 0:
	right = 0
print(left + right)


import sys
import bisect
l = list(map(int, input().split()))
d = l[-1]
del l[-1]
l.sort()
ans = 0
if l[2] - l[0] >= d * 2:
	if l[1] <= l[0] + d:
		ans += abs(l[0] + d - l[1])
		l[1] = l[0] + d
	if l[1] >= l[2] - d:
		ans += abs(l[2] - d - l[1])
		l[1] = l[2] - d
else:
	ans += max(0, l[0] + d - l[1])
	ans += max(0, l[1] + d - l[2])
print(ans)


(a, b, c, d) = input().split()
pos = [int(a), int(b), int(c)]
pos = sorted(pos)
sec = 0
if abs(pos[1] - pos[0]) < int(d):
	sec += int(d) - abs(pos[1] - pos[0])
if abs(pos[2] - pos[1]) < int(d):
	sec += int(d) - abs(pos[2] - pos[1])
print(sec)


ab = [int(c) for c in input().split()]
d = ab[3]
ab.pop(3)
ab.sort()
if d > ab[1] - ab[0]:
	kq = d - (ab[1] - ab[0])
else:
	kq = 0
if d > ab[2] - ab[1]:
	kq = kq + d - (ab[2] - ab[1])
print(kq)


from sys import stdin
(a, b, c, d) = map(int, stdin.readline().split())
l = []
l.append(a)
l.append(b)
l.append(c)
l.sort()
if l[1] - l[0] >= d and l[2] - l[1] >= d:
	print(0)
elif l[1] - l[0] >= d:
	print(d - (l[2] - l[1]))
elif l[2] - l[1] >= d:
	print(d - (l[1] - l[0]))
else:
	print(d - (l[1] - l[0]) + d - (l[2] - l[1]))


a = list(map(int, input().split()))
b = []
b.append(a[0])
b.append(a[1])
b.append(a[2])
b.sort()
a1 = b[1] - a[3]
a3 = b[1] + a[3]
su = 0
if a1 < b[0]:
	su = su + b[0] - a1
if a3 > b[2]:
	su = su + a3 - b[2]
print(su)


s = input().split()
a = []
a.append(int(s[0]))
a.append(int(s[1]))
a.append(int(s[2]))

def sort(array):
	if len(array) < 2:
		return array
	pivot = array[0]
	greater = [i for i in array[1:] if i >= pivot]
	less = [i for i in array[1:] if i < pivot]
	return sort(less) + [pivot] + sort(greater)
d = int(s[3])
a = sort(a)
sumn = 0
if a[2] - a[1] < d:
	sumn += d - a[2] + a[1]
if a[1] - a[0] < d:
	sumn += d - a[1] + a[0]
print(sumn)


ln = [int(i) for i in input().split(' ')]
d = ln[-1]
ln = ln[:-1]
ln = sorted(ln)
nm = max(d - (ln[1] - ln[0]), 0) + max(d - (ln[2] - ln[1]), 0)
print(nm)


(a, b, c, d) = map(int, input().split())
ans = 0
l = min(a, b, c)
ma = max(a, b, c)
m = a + b + c - l - ma
if d - (m - l) > 0:
	ans += d - (m - l)
if d - (ma - m) > 0:
	ans += d - (ma - m)
print(ans)


import sys

def solve(a, b, c, d):
	return max(0, d - (b - a)) + max(0, d - (c - b))

def main():
	(a, b, c, d) = map(int, input().split())
	(a, b, c) = sorted((a, b, c))
	print(solve(a, b, c, d))
main()


(*p, d) = map(int, input().split())
p.sort()
r = max(0, d - (p[1] - p[0])) + max(0, d - (p[2] - p[1]))
print(r)


data = list(map(int, input().split(' ')))
pos = sorted(data[:3])
d = data[-1]
ans = 0
if pos[2] - pos[1] < d and pos[1] - pos[0] < d:
	ans = d - (pos[2] - pos[1]) + (d - (pos[1] - pos[0]))
elif pos[2] - pos[1] < d:
	ans = d - (pos[2] - pos[1])
elif pos[1] - pos[0] < d:
	ans = d - (pos[1] - pos[0])
print(ans)


from sys import stdin, stdout
temp = list(map(int, stdin.readline().split()))
a = temp[:3]
k = temp[3]
a.sort()
ans = 0
if a[1] - a[0] < k:
	ans += k - (a[1] - a[0])
if a[2] - a[1] < k:
	ans += k - (a[2] - a[1])
print(ans)


(a, b, c, n) = [int(x) for x in input().split()]
arr = []
arr.append(a)
arr.append(b)
arr.append(c)
arr.sort()
print(max(0, n - (arr[1] - arr[0])) + max(0, n - (arr[2] - arr[1])))


lst = [int(x) for x in input().split()]
positions = lst[:3]
positions.sort()
d1 = positions[1] - positions[0]
d2 = positions[2] - positions[1]
d = lst[3]
ans = 0
if d - d1 > 0:
	ans += d - d1
if d - d2 > 0:
	ans += d - d2
print(ans)


(a, b, c, d) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
if b > c:
	(b, c) = (c, b)
if a > b:
	(a, b) = (b, a)
rez = 0
if b - a < d:
	rez += d - (b - a)
if c - b < d:
	rez += d - (c - b)
print(rez)


(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
mid = arr[1]
su = 0
if arr[2] - arr[1] < d:
	su += arr[1] + d - arr[2]
if arr[1] - arr[0] < d:
	su += arr[0] - (arr[1] - d)
print(su)


(a, b, c, d) = map(int, input().split())
dist = [a, b, c]
dist.sort()
first = d - (dist[1] - dist[0])
if first < 0:
	first = 0
second = d - (dist[2] - dist[1])
if second < 0:
	second = 0
print(first + second)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict
import math
import heapq

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
(a, b, c, d) = listIn()
li = sorted([a, b, c])
t = 0
if li[2] - li[1] < d:
	t += d - (li[2] - li[1])
	li[2] = li[1] + d
if li[1] - li[0] < d:
	t += d - (li[1] - li[0])
	li[0] = li[1] - d
print(t)


[a, b, c, d] = [int(i) for i in input().split()]
l = [a, b, c]
l.sort()
v1 = abs(l[1] - l[0])
v2 = abs(l[2] - l[1])
d1 = 0
d2 = 0
if v1 < d:
	d1 = abs(d - v1)
if v2 < d:
	d2 = abs(d - v2)
print(d1 + d2)


acrobatas = input().split(' ')
for i in range(len(acrobatas)):
	acrobatas[i] = int(acrobatas[i])
stunt = acrobatas[:-1]
ma = max(stunt)
mi = min(stunt)
stunt.remove(ma)
stunt.remove(mi)
dist = 0
if abs(ma - stunt[0]) < acrobatas[3]:
	dist += acrobatas[3] - abs(ma - stunt[0])
if abs(mi - stunt[0]) < acrobatas[3]:
	dist += acrobatas[3] - abs(mi - stunt[0])
print(dist)


(a, b, c, d) = [int(i) for i in input().split()]
l = sorted([a, b, c])
(a, b, c) = (l[0], l[1], l[2])
count = max(0, d - (b - a)) + max(0, d - (c - b))
print(count)


(a, b, c, d) = map(int, input().split())
l = [a, b, c]
l.sort()
[a, b, c] = l
if b - a >= d and c - b >= d:
	count = 0
elif b - a >= d and c - b < d:
	count = d + b - c
elif b - a < d and c - b >= d:
	count = d + a - b
else:
	count = 2 * d + a - c
print(count)


from sys import stdin
(a, b, c, d) = [int(i) for i in stdin.readline().split(' ')]
(x, y, z) = sorted([a, b, c])
print(max(d - y + x, 0) + max(d - z + y, 0))


(a, b, c, d) = [int(i) for i in input().split()]
walkers = [a, b, c]
for i in range(0, len(walkers) - 1):
	for j in range(0, len(walkers) - i - 1):
		if walkers[j] > walkers[j + 1]:
			temp = walkers[j]
			walkers[j] = walkers[j + 1]
			walkers[j + 1] = temp
minDistance = 0
if walkers[1] - walkers[0] < d:
	minDistance = minDistance + (d - (walkers[1] - walkers[0]))
if walkers[2] - walkers[1] < d:
	minDistance = minDistance + (d - (walkers[2] - walkers[1]))
print(minDistance)


(a, b, c, d) = input().split()
a = int(a)
b = int(b)
c = int(c)
d = int(d)
walkers = [a, b, c]
walkers.sort()
walkers_diff = [min(walkers[1] - walkers[0], d), min(walkers[2] - walkers[1], d)]
ans = 2 * d - sum(walkers_diff)
print(ans)


import sys
import math
import bisect

def main():
	(a, b, c, d) = map(int, input().split())
	A = [a, b, c]
	A.sort()
	ans = 0
	if A[1] - A[0] < d:
		ans += d - (A[1] - A[0])
	if A[2] - A[1] < d:
		ans += d - (A[2] - A[1])
	print(ans)
main()


(a, b, c, d) = map(int, input().split())
l = []
l.append(a)
l.append(b)
l.append(c)
l.sort()
dis1 = l[1] - l[0]
dis2 = l[2] - l[1]
ans = 0
if dis1 < d:
	ans += d - dis1
if dis2 < d:
	ans += d - dis2
print(ans)


l = list(map(int, input().strip().split()))
(a, b, c, d) = (l[0], l[1], l[2], l[3])
m = max(a, b, c)
n = min(a, b, c)
o = a + b + c - m - n
c = 0
if m - o >= d:
	pass
else:
	c += d - (m - o)
if o - n >= d:
	pass
else:
	c += d - (o - n)
print(c)


(a, b, c, d) = map(int, input().split())
m = list()
m.append(a)
m.append(b)
m.append(c)
m.sort()
a = m[0]
b = m[1]
c = m[2]
ans = 0
if b - a < d:
	ans += d - (b - a)
if c - b < d:
	ans += d - (c - b)
print(ans)


(a, b, c, d) = [int(_) for _ in input().split()]
if a > b:
	(a, b) = (b, a)
if b > c:
	(b, c) = (c, b)
if a > b:
	(a, b) = (b, a)
d1 = b - a
d2 = c - b
k1 = 0
k2 = 0
if d1 >= d and d2 >= d:
	print(0)
elif d1 - d < 0 or d2 - d < 0:
	if d1 - d < 0:
		k1 = d - d1
	if d2 - d < 0:
		k2 = d - d2
	print(k1 + k2)


(a, b, c, d) = map(int, input().split())
abc = [a, b, c]
abc = sorted(abc)
a = abc[0]
b = abc[1]
c = abc[2]
time = 0
if b - a < d:
	time += d - (b - a)
if c - b < d:
	time += d - (c - b)
print(time)


def I():
	return int(input())

def IL():
	return [int(i) for i in input().split()]

def IS():
	return input()

def IM():
	return map(int, input().split())
l = IL()
d = l[3]
l = l[:3]
l.sort()
ans = 0
if l[1] - l[0] >= d:
	pass
else:
	ans += d - l[1] + l[0]
if l[2] - l[1] >= d:
	pass
else:
	ans += d - l[2] + l[1]
print(ans)


(a, b, c, d) = list(map(int, input().split()))
arr = [a, b, c]
arr = sorted(arr)
cnt = 0
if abs(arr[1] - arr[0]) < d:
	cnt += d - abs(arr[1] - arr[0])
if abs(arr[2] - arr[1]) < d:
	cnt += d - abs(arr[2] - arr[1])
print(cnt)


(a, b, c, d) = map(int, input().split())
if a <= b and b <= c or (c <= b and b <= a):
	mid = b
elif b <= c and c <= a or (a <= c and c <= b):
	mid = c
else:
	mid = a
subOne = abs(mid - min((a, b, c)))
subTwo = abs(max((a, b, c)) - mid)
ans = 0
if subOne < d:
	ans += abs(d - subOne)
else:
	ans += 0
if subTwo < d:
	ans += abs(d - subTwo)
else:
	ans += 0
print(ans)


def countMinTime(arr, d):
	sortedArr = arr
	minDistance = d
	diff12 = arr[1] - arr[0]
	diff23 = arr[2] - arr[1]
	time12 = d - diff12
	time23 = d - diff23
	if diff12 >= d:
		time12 = 0
	if diff23 >= d:
		time23 = 0
	minTime = time23 + time12
	return minTime
ropeWalkerInput = input().split(' ')
a = int(ropeWalkerInput[0])
b = int(ropeWalkerInput[1])
c = int(ropeWalkerInput[2])
d = int(ropeWalkerInput[3])
arr = [a, b, c]
arr.sort()
print(countMinTime(arr, d))


abcd = input().split()
for i in range(len(abcd)):
	abcd[i] = int(abcd[i])
d = abcd[-1]
abc = [abcd[i] for i in range(3)]
abc = sorted(abc)
count = 0
diff1 = abs(abc[0] - abc[1]) - d
diff2 = abs(abc[1] - abc[2]) - d
if diff1 < 0:
	count += -1 * diff1
if diff2 < 0:
	count += -1 * diff2
print(count)


a = list(map(int, input().split()))
d = a.pop()
a.sort()
ans = 0
for i in range(2):
	dif = abs(a[i] - a[i + 1])
	if dif < d:
		ans += d - dif
print(ans)


from math import *
from collections import *
from itertools import *
from functools import *
from random import *

def getl(t=int):
	return [t(x) for x in input().split()]

def get(t=int):
	return t(input())
alphabet = [chr(x) for x in range(ord('a'), ord('z') + 1)]
alnum = lambda x: ord(x) - ord('a')

def main():
	l = getl()
	d = l[-1]
	x = l[:3]
	(a, b, c) = sorted(x)
	print(max(0, d - (c - b)) + max(0, d - (b - a)))
main()


pos = list(map(int, input().split()))
d = pos.pop()
pos.sort()
mov = 0
mov += max(0, d - (pos[1] - pos[0]))
mov += max(0, d - (pos[2] - pos[1]))
print(mov)


def ropewalkers(w):
	d = w[3]
	u = sorted(w[0:3])
	s = u[1] - d
	t = d + u[1]
	if t < u[2]:
		t = 0
	else:
		t = t - u[2]
	if u[0] <= s:
		s = 0
	else:
		s = d - u[1] + u[0]
	return abs(s + t)
aa = list(map(int, input().split()))
print(ropewalkers(aa))


(a, b, c, d) = [int(a) for a in input().split()]
l = [a, b, c]
l.sort()
l1 = [l[1] - l[0], l[2] - l[1], l[2] - l[0]]
l1.sort()
sum = 0
if l1[0] < d:
	sum += d - l1[0]
if l1[1] < d:
	sum += d - l1[1]
print(sum)


(a, b, c, d) = tuple(map(int, input().rstrip().split()))
rope = sorted([a, b, c])
d1 = rope[1] - rope[0]
d2 = rope[2] - rope[1]
s = 0
if d1 < d:
	s += d - d1
if d2 < d:
	s += d - d2
print(s)


a = [int(x) for x in input().split()]
s = sorted(a[:3])
a = a[3]
ans = 0
if s[1] - s[0] < a:
	ans += a - (s[1] - s[0])
if s[2] - s[1] < a:
	ans += a - (s[2] - s[1])
print(ans)


def get_max_steps(numbers):
	(*values, distance) = [int(i) for i in numbers]
	values = sorted(values)
	steps = 0
	(dis1, dis2) = (values[1] - values[0], values[2] - values[1])
	for i in [dis1, dis2]:
		if i < distance:
			steps += distance - i
	return steps
print(get_max_steps(input().split()))


z = list(map(int, input().split()))
d = z[3]
z = z[0:3]
z.sort()
a = z[0]
b = z[1]
c = z[2]
r1 = max(0, d - (b - a))
r2 = max(0, d - (c - b))
print(r1 + r2)


(a, b, c, d) = map(int, input().strip().split())
l = [a, b, c]
l.sort()
(a, b, c) = (l[0], l[1], l[2])
s = 0 if b - d >= a else abs(a - (b - d))
s += 0 if c >= b + d else abs(b + d - c)
print(s)


def polycarp(l, d):
	l.sort()
	count = 0
	if abs(l[0] - l[1]) >= d:
		pass
	else:
		count += d - abs(l[0] - l[1])
	if abs(l[1] - l[2]) >= d:
		pass
	else:
		count += d - abs(l[1] - l[2])
	return count
l = list(map(int, input().strip().split()))
print(polycarp(l[:3], l[3]))


import math
from decimal import Decimal

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)
(a, b, c, d) = map(int, input().split())
s = sorted([a, b, c])
if s[1] - s[0] >= d:
	a1 = 0
else:
	a1 = d - s[1] + s[0]
if s[2] - s[1] >= d:
	a2 = 0
else:
	a2 = d - s[2] + s[1]
print(a1 + a2)


(m, b, c, d) = map(int, input().split())
a = [m, b, c]
a.sort()
ans = 0
for i in range(1, 3):
	if a[i] - a[i - 1] < d:
		ans += d - a[i] + a[i - 1]
print(ans)


from math import fabs

def u(x):
	if x <= 0:
		return 0
	return x
(a, b, c, d) = list(map(int, input().split()))
m = sorted([a, b, c])
print(int(u(d - fabs(m[1] - m[0])) + u(d - fabs(m[2] - m[1]))))


def moves(arr, d):
	movesToMake = 0
	if arr[1] - arr[0] < d:
		movesToMake += abs(arr[1] - arr[0] - d)
	if arr[2] - arr[1] < d:
		movesToMake += abs(arr[2] - arr[1] - d)
	return movesToMake
(a, b, c, d) = map(int, input().split())
print(moves(sorted([a, b, c]), d))


(a, b, c, dis) = map(int, input().split())
(a, b, c) = sorted([a, b, c])
(z, d, f) = (0, b - a, c - a)
(g, h, i) = (a - b, 0, c - b)
(j, k, l) = (abs(a - c), abs(b - c), 0)
cnt1 = 0
if d < dis:
	cnt1 += dis - d
	d = dis
if f - d < dis:
	cnt1 += dis - f + d
cnt2 = 0
if abs(g) < dis:
	cnt2 += dis - abs(g)
if i < dis:
	cnt2 += dis - i
cnt3 = 0
if k < dis:
	cnt3 += dis - k
	k = dis
if j - k < dis:
	cnt3 += dis - j + k
print(min([cnt1, cnt2, cnt3]))


from sys import *
(a, b, c, d) = map(int, stdin.readline().split())
(a, b, c) = sorted([a, b, c])
if b - a <= d and c - b <= d:
	print(d - (b - a) + d - (c - b))
elif b - a <= d:
	print(d - (b - a))
elif c - b <= d:
	print(d - (c - b))
else:
	print(0)


(*s, d) = list(map(int, input().split()))
(a, b, c) = sorted(s)
q = 0
if b - a < d:
	q = d - abs(b - a)
if c - b < d:
	q += d - abs(c - b)
print(q)


(a, b, c, d) = map(int, input().split())
tmp = [a, b, c]
tmp.sort()
(a, b, c) = tmp
x = d - (c - b)
if x < 0:
	x = 0
y = a - (b - d)
if y < 0:
	y = 0
ans = x + y
print(ans)


(a, b, c, d) = list(map(int, input().split()))
A = [a, b, c]
A.sort()
a1 = A[1] - A[0]
ans = 0
if a1 < d:
	ans += A[0] + d - A[1]
	A[1] = A[0] + d
A[2] = A[2] + ans
b1 = A[2] - A[1]
if b1 < d:
	ans += A[1] + d - A[2]
print(ans)


(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted([a, b, c])

def solve1(a, b, c):
	r1 = max(0, d - (b - a))
	b += r1
	r2 = max(0, d - (c - b))
	return r1 + r2

def solve2(a, b, c):
	r1 = max(0, d - (c - b))
	r2 = max(0, d - (b - a))
	return r1 + r2

def solve3(a, b, c):
	r1 = max(0, d - (c - b))
	b -= r1
	r2 = max(0, d - (b - a))
	return r1 + r2
r1 = solve1(a, b, c)
r2 = solve2(a, b, c)
r3 = solve3(a, b, c)
print(min(r1, r2, r3))


(a, b, c, d) = map(int, input().split())
num_list = [a, b, c]
num_list.sort()
cnt = 0
if num_list[2] - num_list[1] < d:
	cnt += d - (num_list[2] - num_list[1])
if num_list[1] - num_list[0] < d:
	cnt += d - (num_list[1] - num_list[0])
print(cnt)


X = [int(x) for x in input().split(' ')]
d = X[-1]
X = X[:-1]
X.sort()
c = 0
if X[1] - X[0] < d:
	c = c + d - (X[1] - X[0])
	X[0] = X[1] - d
if X[2] - X[1] < d:
	c = c + d - (X[2] - X[1])
	X[2] = d + X[1]
print(c)


(*a, b) = map(int, input().split())
a.sort()
z = 0
if a[1] - a[0] < b:
	z += b - (a[1] - a[0])
if a[2] - a[1] < b:
	z += b - (a[2] - a[1])
print(z)


arr = [int(x) for x in input().split(' ')]
d = arr.pop(3)
arr.sort()
a = arr[0]
b = arr[1]
c = arr[2]
k = 0
if b - a < d:
	k = k + d - b + a
if c - b < d:
	k = k + d - c + b
print(k)


(a, b, c, d) = map(int, input().split())
s = [a, b, c]
ans = 0
a = min(s)
del s[s.index(a)]
c = max(s)
del s[s.index(c)]
b = s[0]
if b - a < d:
	ans += d - (b - a)
if c - b < d:
	ans += d - (c - b)
print(ans)


(a, b, c, d) = map(int, input().split())
l = []
l.append(a)
l.append(b)
l.append(c)
l.sort()
r = l[1] - l[0]
s = l[2] - l[1]
t = 0
if r <= d:
	t += d - r
if s <= d:
	t += d - s
print(t)


pos = list(map(int, input().split()))
time = 0
distance = pos[-1]
pos = sorted(pos[0:3])
d12 = abs(pos[1] - pos[0])
if d12 < distance:
	time += distance - d12
d23 = abs(pos[1] - pos[2])
if d23 < distance:
	time += distance - d23
print(time)


(a, b, c, d) = map(int, input().split())
delta = map(lambda x: max(0, d - x), sorted([abs(a - b), abs(a - c), abs(b - c)])[:-1])
print(sum(delta))


positions = list(map(int, input().split()))
d = positions.pop()
positions.sort()
min_duration = 0
positions = [abs(positions[i] - positions[1]) for i in range(len(positions))]
if positions[0] < d:
	min_duration = abs(d - positions[0])
if positions[2] < d:
	min_duration = min_duration + abs(d - positions[2])
print(min_duration)


(a, b, c, d) = list(map(int, input().split()))
su1 = 0
su2 = 0
l = [a, b, c]
l.sort()
if l[1] - l[0] < d:
	su1 = d - (l[1] - l[0])
if l[2] - l[1] < d:
	su2 = d - (l[2] - l[1])
print(su1 + su2)


(a, b, c, d) = list(map(int, input().split()))
x = [a, b, c]
x.sort(reverse=True)
if x[0] - x[1] >= d and x[1] - x[2] >= d:
	print(0)
elif x[0] - x[1] < d and x[1] - x[2] < d:
	print(2 * d - (x[0] - x[2]))
else:
	print(d - x[0] + x[1]) if x[0] - x[1] < d else print(d - x[1] + x[2])


positions = [int(ele) for ele in input().split()]
d = positions.pop()
positions.sort()
time = 0
time += max(d - positions[1] + positions[0], 0)
time += max(d - positions[2] + positions[1], 0)
print(time)


(a, b, c, diff) = [int(x) for x in input().split()]
(a, b, c) = sorted([a, b, c])
diff1 = b - a
diff2 = c - b
count = 0
if diff1 < diff:
	count += diff - diff1
if diff2 < diff:
	count += diff - diff2
print(count)


import math
import heapq
from collections import OrderedDict
from collections import deque
t = list(map(int, input().split()))
a = t[0]
b = t[1]
c = t[2]
d = t[3]
left = min(a, b, c)
right = max(a, b, c)
mid = 0
if left == a:
	if right == b:
		mid = c
	elif right == c:
		mid = b
elif left == b:
	if right == a:
		mid = c
	elif right == c:
		mid = a
elif left == c:
	if right == a:
		mid = b
	elif right == b:
		mid = a
diff1 = abs(left - mid)
diff2 = abs(right - mid)
if diff2 >= d and diff1 >= d:
	print(0)
elif diff1 >= d:
	print(abs(d - diff2))
elif diff2 >= d:
	print(abs(d - diff1))
else:
	print(abs(d - diff1) + abs(d - diff2))


from sys import stdin, stdout

def input():
	return stdin.readline().strip()
MOD = 1000000007
(a, b, c, d) = map(int, input().split())
x = [a, b, c]
x.sort()
(a, b, c) = x
print(max(0, d - abs(a - b)) + max(0, d - abs(b - c)))


(*a1, d) = map(int, input().split())
(a, b, c) = sorted(a1)
dis1 = abs(c - b)
dis2 = abs(b - a)
res = 0
if dis2 < d:
	res += abs(d - dis2)
if dis1 < d:
	res += abs(d - dis1)
print(res)


(a, b, c, d) = list(map(int, input().split()))
A = [a, b, c]
A.sort()
(a, b, c) = A
Ans = 0
if abs(a - b) < d:
	Ans += d - abs(a - b)
if abs(b - c) < d:
	Ans += d - abs(b - c)
print(Ans)


(a, b, c, d) = map(int, input().split())
maxn = max(a, b, c)
minn = min(a, b, c)
m = [a, b, c]
m.remove(maxn)
m.remove(minn)
midn = m.pop(0)
r0 = midn - minn
r1 = maxn - midn
if r0 >= d and r1 >= d:
	print(0)
elif r0 >= d and r1 < d:
	print(d - r1)
elif r0 < d and r1 >= d:
	print(d - r0)
else:
	print(d - r0 + (d - r1))


(*l, d) = map(int, input().split())
l.sort()
print(max(d - (l[1] - l[0]), 0) + max(d - (l[2] - l[1]), 0))


(a, b, c, k) = map(int, input().split())
arr = [a, b, c]
arr.sort()
(a, b, c, ans) = (arr[0], arr[1], arr[2], 0)
if a > b - k:
	ans += abs(a - (b - k))
if c < b + k:
	ans += abs(b + k - c)
print(ans)


(a, b, c, d) = map(int, input().split())
aa = [a, b, c]
aa.sort()
(a, b, c) = (aa[0], aa[1], aa[2])
r = 0
if b - a < d:
	r += d - b + a
if c - b < d:
	r += d - c + b
print(r)


arr = [0] * 3
(arr[0], arr[1], arr[2], d) = map(int, input().split())
arr.sort()
print(max(d - (arr[1] - arr[0]), 0) + max(0, d - (arr[2] - arr[1])))


(a, b, c, d) = [int(x) for x in input().split(' ')]
m = list()
m.append(a)
m.append(b)
m.append(c)
m.sort()
count = 0
if m[1] - m[0] < d:
	count = count + d - (m[1] - m[0])
if m[2] - m[1] < d:
	count = count + d - (m[2] - m[1])
print(count)


def main():
	[a, b, c, d] = list(map(int, input().split()))
	ar = [a, b, c]
	[first, second, third] = sorted(ar)
	result = 0
	if abs(first - second) < d:
		result += abs(abs(first - second) - d)
	if abs(second - third) < d:
		result += abs(abs(second - third) - d)
	print(result)
main()


(a, b, c, d) = input().split()
a = int(a)
b = int(b)
c = int(c)
d = int(d)
s = (a, b, c)
s = list(s)
s.sort()
a = s[0]
b = s[1]
c = s[2]
min = 0
if abs(a - b) < d:
	min += d - abs(a - b)
	a -= d - abs(a - b)
if abs(b - c) < d:
	min += d - abs(b - c)
	c += d - abs(b - c)
if abs(a - c) < d:
	min += d - abs(a - c)
print(min)


(a, b, c, d) = list(map(int, input().split()))
List = [a, b, c]
List.sort()
t = 0
if List[1] - List[0] < d:
	t += d - (List[1] - List[0])
if List[2] - List[1] < d:
	t += d - (List[2] - List[1])
print(t)


def canat(lst, d):
	result = 0
	if lst[1] - lst[0] < d:
		result += d - lst[1] + lst[0]
	if lst[2] - lst[1] < d:
		result += d - lst[2] + lst[1]
	return result
(a, b, c, D) = [int(i) for i in input().split()]
z = sorted([a, b, c])
print(canat(z, D))


(a, b, c, d) = [int(i) for i in input().split()]
l = [a, b, c]
l.sort()
print((d - l[1] + l[0] if d > l[1] - l[0] else 0) + (d - l[2] + l[1] if d > l[2] - l[1] else 0))


(a, b, c, d) = [int(x) for x in input().split()]
points = [a, b, c]
pos = max(points)
points.remove(pos)
neg = min(points)
points.remove(neg)
mid = points.pop()
pos_diff = abs(pos - mid)
go_pos = 0
if pos_diff < d:
	go_pos = d - pos_diff
neg_diff = abs(neg - mid)
go_neg = 0
if neg_diff < d:
	go_neg = d - (mid - neg)
print(str(go_pos + go_neg))


(a, b, c, d) = list(map(int, input().split()))
spisok = [a, b, c]
spisok.sort()
e = abs(spisok[1] - spisok[0])
k = abs(spisok[2] - spisok[1])
if e < d:
	e = abs(d - e)
else:
	e = 0
if k < d:
	k = abs(d - k)
else:
	k = 0
sum = e + k
print(sum)


def rint():
	return int(input())

def rints():
	return list(map(int, input().split()))
(a, b, c, d) = rints()
(a, b, c) = sorted([a, b, c])
print(max(d - (b - a), 0) + max(d - (c - b), 0))


s = [int(i) for i in input().split()]
d = s.pop(3)
(a, b, c) = sorted(map(int, s))
h = 0
if b - a < d:
	h = d - (b - a)
if c - b < d:
	h += d - (c - b)
print(h)


(a, b, c, d) = map(int, input().split())
dat = [a, b, c]
dat.sort()
(a, b, c) = dat
res = 0
x = d - (b - a)
if x < 0:
	x = 0
res += x
x = d - (c - b)
if x < 0:
	x = 0
res += x
print(res)


(a, b, c, d) = map(int, input().strip().split(' '))
inputs = [a, b, c]
inputs.sort()
result = 0
if inputs[2] - inputs[1] < d:
	result += d - (inputs[2] - inputs[1])
if inputs[1] - inputs[0] < d:
	result += d - (inputs[1] - inputs[0])
print(result)


(a, b, c, d) = map(int, list(input().split(' ')))
pos = sorted([a, b, c])
result = 0
a = pos[0]
b = pos[1]
c = pos[2]
if b - a < d:
	result += d - (b - a)
if c - b < d:
	result += d - (c - b)
print(result)


(a, b, c, d) = list(map(int, input().split()))
s = [a, b, c]
s.sort()
print(max(0, d - s[1] + s[0]) + max(0, d - s[2] + s[1]))


(a, b, c, n) = map(int, input().split())
k = []
k.append(a)
k.append(b)
k.append(c)
k.sort()
ans = 0
if abs(k[0] - k[1]) < n:
	ans += n - abs(k[0] - k[1])
if abs(k[2] - k[1]) < n:
	ans += n - abs(k[1] - k[2])
print(ans)


(a, b, c, d) = map(int, input().split())
A = list()
A.append(a)
A.append(b)
A.append(c)
A.sort()
c = 0
if A[1] - A[0] < d:
	c += d - (A[1] - A[0])
if A[2] - A[1] < d:
	c += d - (A[2] - A[1])
print(c)


import math
(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
ans = 0
ans += max(0, d - arr[1] + arr[0])
ans += max(0, d - arr[2] + arr[1])
print(ans)


from sys import stdin
(pos_a, pos_b, pos_c, dist) = [int(n) for n in stdin.readline().strip().split(' ')]
(left, center, right) = sorted([pos_a, pos_b, pos_c])
movements_left = dist - (center - left) if center - left < dist else 0
movements_right = dist - (right - center) if right - center < dist else 0
print(movements_left + movements_right)


(a, b, c, d) = map(int, input().split())
m = [a, b, c]
m.sort()
s = (0 if m[1] - m[0] >= d else d - m[1] + m[0]) + (0 if m[2] - m[1] >= d else d - m[2] + m[1])
print(s)


li = list(map(int, input().split(' ')))
d = li[-1]
del li[-1]
ab = bc = 0
li.sort()
if d - (li[1] - li[0]) > 0:
	ab = d - (li[1] - li[0])
if d - (li[2] - li[1]) > 0:
	bc = d - (li[2] - li[1])
print(ab + bc)


(a, b, c, d) = map(int, input().split())
l = []
count = 0
l.append(a)
l.append(b)
l.append(c)
l.sort()
if l[1] - l[0] >= d:
	pass
else:
	count = count + d - (l[1] - l[0])
	l[0] = l[0] - (d - (l[1] - l[0]))
if l[2] - l[1] >= d:
	pass
else:
	count = count + d - (l[2] - l[1])
	l[2] = l[2] + (d - (l[2] - l[1]))
print(count)


l = [*map(int, input().split())]
l1 = [l[0], l[1], l[2]]
l1.sort()
k = 0
if l1[1] - l1[0] < l[3]:
	k += l[3] - (l1[1] - l1[0])
if l1[2] - l1[1] < l[3]:
	k += l[3] - (l1[2] - l1[1])
print(k)


(*l, d) = map(int, input().split())
l.sort()
m = 0
if l[1] - l[0] < d:
	m += d - (l[1] - l[0])
else:
	pass
if l[2] - l[1] < d:
	m += d - (l[2] - l[1])
else:
	pass
print(m)


(a, b, c, d) = map(int, input().strip().split())
(a, b, c) = sorted([a, b, c])
count = 0
d1 = b - a
if d1 < d:
	count += d - d1
d2 = c - b
if d2 < d:
	count += d - d2
print(count)


li = list(map(int, input().split()))
d = li[-1]
li.pop()
li.sort()
c = 0
if li[1] - li[0] >= d:
	c = 0
else:
	c += d - (li[1] - li[0])
if li[2] - li[1] >= d:
	c += 0
else:
	c += d - (li[2] - li[1])
print(c)


(a, b, c, d) = [int(x) for x in input().split()]
(a, c, b1) = (min(a, b, c), max(a, b, c), a + b + c)
b = b1 - a - c
p1 = d - (b - a)
p2 = d - (c - b)
if p1 > 0 and p2 > 0:
	print(p1 + p2)
elif p1 > 0 and p2 <= 0:
	print(p1)
elif p1 <= 0 and p2 > 0:
	print(p2)
else:
	print(0)


a = list(map(int, input().split()))
d = a[3]
l = a[:3]
l.sort()
if l[1] - l[0] >= d:
	x = 0
else:
	x = d - (l[1] - l[0])
if l[2] - l[1] >= d:
	y = 0
else:
	y = d - (l[2] - l[1])
print(x + y)


import sys
string = sys.stdin.readline()
(a, b, c, d) = map(int, string.split())
minimum = min(min(a, b), c)
maximum = max(max(a, b), c)
mid = a + b + c - minimum - maximum
counter = 0
if mid - minimum < d:
	counter += d - (mid - minimum)
if maximum - mid < d:
	counter += d - (maximum - mid)
print(counter)


x = [int(i) for i in input().split()]
d = x[-1]
x.pop()
x.sort()
a = x[0]
b = x[1]
c = x[2]
s = 0
if b - a < d:
	s += d - b + a
if c - b < d:
	s += d - c + b
print(s)


l = list(map(int, input().split()))
d = l[3]
l = l[:3]
l.sort()
ans = 0
for i in range(2, 0, -1):
	d1 = l[i] - l[0]
	if d1 < d:
		ans += d - d1
		l[0] -= d - d1
for i in range(0, 2):
	d1 = l[2] - l[i]
	if d1 < d:
		ans += d - d1
		l[2] += d1
print(ans)


x = [int(x) for x in input().split()]
y = sorted(x[0:3])
x1 = y[1] - y[0]
x2 = y[2] - y[1]
count = 0
if x1 < x[3]:
	count += x[3] - x1
if x2 < x[3]:
	count += x[3] - x2
print(count)


(A, B, C, d) = map(int, input().split())
arr = [A, B, C]
arr.sort()
ret = 0
temp = abs(arr[1] - arr[0])
if temp < d:
	ret += d - temp
temp = abs(arr[2] - arr[1])
if temp < d:
	ret += d - temp
print(ret)


(a, b, c, r) = [int(x) for x in input().split()]
l = []
l.append(a)
l.append(b)
l.append(c)
l.sort()
d = []
d.append(abs(l[0] - l[1]))
d.append(abs(l[0] - l[2]))
d.append(abs(l[1] - l[2]))
s = 0
g = f = 0
if d[0] < r:
	d1 = r - d[0]
	d2 = 0
	if d[1] < r:
		d2 = r - d[1]
	s += max(d1, d2)
if d[2] < r:
	d1 = r - d[2]
	d2 = 0
	if d[1] < r:
		d2 = r - d[1]
	s += max(d1, d2)
print(s)


ar = list(map(int, input().split()))
ar1 = sorted(ar[0:3])
count = 0
if ar1[1] - ar1[0] <= ar[3]:
	count += abs(ar[3] - (ar1[1] - ar1[0]))
if ar1[2] - ar1[1] <= ar[3]:
	count += abs(ar[3] - (ar1[2] - ar1[1]))
print(count)


l = list(map(int, input().split()))
d = l[3]
del l[3]
l.sort()
t = 0
if l[2] - l[1] < d:
	t = t + (d - (l[2] - l[1]))
if l[1] - l[0] < d:
	t = t + (d - (l[1] - l[0]))
print(t)


a = list(map(int, input().split()))
x = a[3]
del a[3]
a.sort()
print(abs(min(0, a[1] - x - a[0])) + abs(max(0, a[1] + x - a[2])))


a = list(map(int, input().split()))
d = a[len(a) - 1]
a.pop(len(a) - 1)
a.sort()
c = 0
if abs(a[1] - a[0]) < d:
	c += d - (a[1] - a[0])
if abs(a[2] - a[1]) < d:
	c += d - (a[2] - a[1])
print(c)


def answer():
	ans = 0
	z = [int(x) for x in input().split()]
	a = z[0]
	b = z[1]
	c = z[2]
	e = [a, b, c]
	e.sort()
	d = z[3]
	f = [e[1] - e[0], e[2] - e[1]]
	ans += max(0, d - f[0]) + max(0, d - f[1])
	print(ans)
answer()


(a, b, c, d) = map(int, input().split())
new = []
new.append(a)
new.append(b)
new.append(c)
new = sorted(new)
count_ = 0
if new[1] - new[0] < d:
	count_ += d - (new[1] - new[0])
if new[2] - new[1] < d:
	count_ += d - (new[2] - new[1])
print(count_)


import atexit
import io
import sys
_I_B = sys.stdin.read().splitlines()
input = iter(_I_B).__next__
_O_B = io.StringIO()
sys.stdout = _O_B

@atexit.register
def write():
	sys.__stdout__.write(_O_B.getvalue())

def main():
	l = list(map(int, input().split()))
	d = l.pop()
	l.sort()
	(a, b, c) = l
	t = 0
	if b - a < d:
		t += d - (b - a)
	if c - b < d:
		t += d - (c - b)
	print(t)
main()


x = list(map(int, input().split()))
d = x[3]
x.pop(3)
x.sort()
a = x[0]
b = x[1]
c = x[2]
count = 0
if b - a < d:
	count = count + d - (b - a)
if c - b < d:
	count = count + b + d - c
print(count)


a = [int(i) for i in input().split()]
b = sorted(a[:3])
d1 = b[2] - b[1]
d2 = b[1] - b[0]
d = a[3]
if d1 >= d and d2 >= d:
	print(0)
elif d1 < d and d2 < d:
	print(2 * d - (d1 + d2))
elif d1 >= d and d2 < d:
	if d1 + d2 > 2 * d:
		print(d - d2)
	else:
		print(d - d2)
elif d1 + d2 > 2 * d:
	print(d - d1)
else:
	print(d - d1)


(a, b, c, d) = [int(i) for i in input().split()]
res = 0
if a >= b and a <= c or (a >= c and a <= b):
	x = abs(a - b)
	y = abs(a - c)
elif b >= a and b <= c or (b >= c and b <= a):
	x = abs(a - b)
	y = abs(b - c)
else:
	x = abs(a - c)
	y = abs(b - c)
res += d - x if d - x > 0 else 0
res += d - y if d - y > 0 else 0
print(res)


(a, b, c, d) = input().split(' ')
(a, b, c, d) = (int(a), int(b), int(c), int(d))
x = [a, b, c]
x.sort()
tcount = 0
d1 = abs(int(x[0]) - int(x[1]))
d2 = abs(int(x[1]) - int(x[2]))
t = 0
if d1 >= d:
	if d2 >= d:
		tcount = 0
	else:
		tcount = d - d2
elif d2 >= d:
	tcount = d - d1
else:
	tcount = d - d1 + (d - d2)
print(tcount)


(a, b, c, d) = map(int, input().split())
(small, medium, big) = sorted([a, b, c])
time = 0
if medium - small < d:
	time += d - (medium - small)
if big - medium < d:
	time += d - (big - medium)
print(time)


(a, b, c, d) = map(int, input().split())
s = [a, b, c]
s.sort()
x = 0
y = 0
if abs(s[0] - s[1]) < d:
	x = d - abs(s[0] - s[1])
if abs(s[1] - s[2]) < d:
	y = d - abs(s[1] - s[2])
print(x + y)


(a, b, c, d) = map(int, input().split())
dau = min(a, b, c)
cuoi = max(a, b, c)
giua = a + b + c - dau - cuoi
if d - (cuoi - giua) >= 0:
	x = d - (cuoi - giua)
else:
	x = 0
if d - (giua - dau) >= 0:
	y = d - (giua - dau)
else:
	y = 0
print(x + y)


(a, b, c, d) = map(int, input().split())
(a, b, c) = (min(a, b, c), a + b + c - max(a, b, c) - min(a, b, c), max(a, b, c))
print(d * 2 - min(b - a, d) - min(c - b, d))


(a, b, c, d) = [int(i) for i in input().split()]
S = sorted([a, b, c])
k = 0
if S[1] - S[0] < d:
	k += d - S[1] + S[0]
if S[2] - S[1] < d:
	k += d - S[2] + S[1]
print(k)


a = map(int, input().split())
b = list(a)
c = []
for i in range(0, 3):
	c.append(b[i])
c = sorted(c)
p = c[1] - c[0]
q = c[2] - c[1]
if b[3] > p:
	d = b[3] - p
else:
	d = 0
if b[3] > q:
	e = b[3] - q
else:
	e = 0
print(d + e)


import math
(a, b, c, d) = map(int, input().split())
(a, b, c) = sorted([a, b, c])
print(int(max(d - math.fabs(a - b), 0) + max(d - math.fabs(c - b), 0)))


from math import *
(x, y, z, a) = [int(i) for i in input().split()]
x = [x, y, z]
x.sort()
k = a - abs(x[0] - x[1])
if k < 0:
	k = 0
l = a - abs(x[1] - x[2])
if l < 0:
	l = 0
b = l + k
print(b)


l = list(map(int, input().split()))
r = l.pop()
l.sort()
print(max(0, r - (l[1] - l[0])) + max(0, r - (l[-1] - l[1])))


import math
import os
import random
import re
import sys

def dif(Sor_arr, tar):
	for i in range(len(Sor_arr)):
		for j in range(len(Sor_arr)):
			t = 0
			if i != j:
				if abs(Sor_arr[i] - Sor_arr[j]) >= tar:
					continue
				else:
					t = abs(Sor_arr[i] - Sor_arr[j])
					diff = tar - t
					Sor_arr[j] = Sor_arr[j] + diff
			else:
				continue
	return Sor_arr
n = input().split()
ar = [int(n[0]), int(n[1]), int(n[2])]
tar = int(n[3])
zz = sorted(ar)
Sor_arr = sorted(ar)
New_Arr = dif(Sor_arr, tar)
Far = []
for i in range(len(Sor_arr)):
	t = abs(zz[i] - New_Arr[i])
	Far.append(t)
print(max(Far))


(*v, d) = map(int, input().split())
(a, b, c) = sorted(v)
print(d - min(b - a, d) + d - min(c - b, d))


(a, b, c, d) = map(int, input().split())
e = []
e.append(a)
e.append(b)
e.append(c)
e = sorted(e)
f = e[1] - e[0]
g = e[2] - e[1]
if f < d:
	h = d - f
else:
	h = 0
if g < d:
	i = d - g
else:
	i = 0
print(h + i)


aa = input()
bb = aa.split()
a = int(bb[0])
b = int(bb[1])
c = int(bb[2])
d = int(bb[3])
l = []
l.append(a)
l.append(b)
l.append(c)
l.sort()
d1 = l[1] - l[0]
d2 = l[2] - l[1]
ccc = 0
if d1 < d:
	ccc += d - d1
if d2 < d:
	ccc += d - d2
print(ccc)


l = list(map(int, input().split()))
d = l[3]
l.remove(l[3])
l.sort()
ans = 0
if l[1] - l[0] < d:
	ans = ans + d - (l[1] - l[0])
if l[2] - l[1] < d:
	ans = ans + d - (l[2] - l[1])
print(ans)


import sys
import math
fileoperation = 0
if fileoperation:
	orig_stdout = sys.stdout
	orig_stdin = sys.stdin
	inputfile = open('W:/Competitive Programming/input.txt', 'r')
	outputfile = open('W:/Competitive Programming/output.txt', 'w')
	sys.stdin = inputfile
	sys.stdout = outputfile
mod = 1000000007

def nospace(l):
	ans = ''.join((str(i) for i in l))
	return ans
t = 1
for tt in range(t):
	(a, b, c, d) = map(int, sys.stdin.readline().split(' '))
	ans = 1000000000000
	l = [a, b, c]
	l.sort()
	x = l[0]
	y = l[1]
	z = l[2]
	m = abs(x - y)
	n = abs(y - z)
	a1 = max(d - m, 0)
	currb = max(x + d, y)
	a1 += max(0, d - (z - currb))
	a2 = m + d
	a2 += max(0, d - (z - x))
	a3 = max(0, d - (y - x))
	a3 += max(0, d - (z - y))
	a4 = max(0, d - n)
	currb = y - a4
	a4 += max(0, d - (currb - x))
	a5 = n + d
	a5 += max(0, d - (z - x))
	print(min(a1, a2, a3, a4, a5))
if fileoperation:
	sys.stdout = orig_stdout
	sys.stdin = orig_stdin
	inputfile.close()
	outputfile.close()


def solve(arr, d):
	result = 0
	temp1 = arr[1] - arr[0]
	temp2 = arr[2] - arr[1]
	if temp1 < d:
		result += d - temp1
	if temp2 < d:
		result += d - temp2
	return result
(a, b, c, d) = map(int, input().split())
arr = [a, b, c]
arr.sort()
print(solve(arr, d))


T_ON = 0
DEBUG_ON = 1
MOD = 998244353

def solve():
	(a, b, c, d) = read_ints()
	X = [a, b, c]
	X.sort()
	(a, b, c) = X
	total = max(0, d - abs(a - b)) + max(0, d - abs(c - b))
	print(total)

def main():
	T = read_int() if T_ON else 1
	for i in range(T):
		solve()

def debug(*xargs):
	if DEBUG_ON:
		print(*xargs)
from collections import *
import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def read_int():
	return int(input())

def read_ints():
	return list(map(int, input().split()))

def print_nums(nums):
	print(' '.join(map(str, nums)))

def YES():
	print('YES')

def Yes():
	print('Yes')

def NO():
	print('NO')

def No():
	print('No')

def First():
	print('First')

def Second():
	print('Second')

def fib(n):
	(a, b) = (0, 1)
	for _ in range(n):
		(a, b) = (b, a + b)
	return a

def fib_ns(n):
	assert n >= 1
	f = [0 for _ in range(n + 1)]
	f[0] = 0
	f[1] = 1
	for i in range(2, n + 1):
		f[i] = f[i - 1] + f[i - 2]
	return f

def fib_to_n(n):
	f = []
	(a, b) = (0, 1)
	while a <= n:
		f.append(a)
		(a, b) = (b, a + b)
	return f

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def xgcd(a, b):
	(x0, x1, y0, y1) = (0, 1, 1, 0)
	while a != 0:
		((q, a), b) = (divmod(b, a), a)
		(y0, y1) = (y1, y0 - q * y1)
		(x0, x1) = (x1, x0 - q * x1)
	return (b, x0, y0)

def lcm(a, b):
	d = gcd(a, b)
	return a * b // d

def is_even(x):
	return x % 2 == 0

def is_odd(x):
	return x % 2 == 1

def modinv(a, m):
	(g, x, _) = xgcd(a, m)
	if g != 1:
		raise Exception('gcd(a, m) != 1')
	return x % m

def mod_add(x, y):
	x += y
	while x >= MOD:
		x -= MOD
	while x < 0:
		x += MOD
	return x

def mod_mul(x, y):
	return x * y % MOD

def mod_pow(x, y):
	if y == 0:
		return 1
	if y % 2:
		return mod_mul(x, mod_pow(x, y - 1))
	p = mod_pow(x, y // 2)
	return mod_mul(p, p)

def mod_inv(y):
	return mod_pow(y, MOD - 2)

def mod_div(x, y):
	return mod_mul(x, mod_inv(y))

def is_prime(n):
	if n == 1:
		return False
	for i in range(2, int(n ** 0.5) + 1):
		if n % i:
			return False
	return True

def gen_primes(n):
	P = [True for _ in range(n + 1)]
	P[0] = P[1] = False
	for i in range(int(n ** 0.5) + 1):
		if P[i]:
			for j in range(2 * i, n + 1, i):
				P[j] = False
	return P
main()


x = 0
stepdiff = 0
inp = input().split()
a = int(inp[0])
b = int(inp[1])
c = int(inp[2])
d = int(inp[3])
nums = sorted([a, b, c])
middleval = nums[1]
leftval = nums[0]
rightval = nums[2]
midright = abs(middleval - rightval)
midleft = abs(leftval - middleval)
while midright < d or midleft < d:
	midright = abs(middleval - rightval)
	midleft = abs(leftval - middleval)
	if midright < d:
		stepdiff += abs(midright - d)
		rightval += abs(midright - d)
	if midleft < d:
		stepdiff += abs(d - midleft)
		leftval -= abs(d - midleft)
	x += 1
	if x == 10:
		exit()
print(stepdiff)


(*a, d) = map(int, input().split())
a = sorted(a)
print(max(0, d - (a[1] - a[0])) + max(0, d - (a[2] - a[1])))


(a, b, c, d) = map(int, input().split(' '))
ls = [a, b, c]
ls = sorted(ls)
ans = 0
d1 = abs(ls[1] - ls[0])
if d1 < d:
	ans = d - d1
if abs(ls[2] - ls[1]) < d:
	ans = ans + d - abs(ls[2] - ls[1])
print(ans)


def find_second_max(list_of_numbers):
	max = list_of_numbers[0]
	second_max = list_of_numbers[1]
	if second_max > max:
		(second_max, max) = (max, second_max)
	for i in range(2, len(list_of_numbers)):
		if list_of_numbers[i] >= max:
			second_max = max
			max = list_of_numbers[i]
		elif list_of_numbers[i] > second_max:
			second_max = list_of_numbers[i]
	return second_max
list_of_numbers = list(map(int, input().split(' ')))
d = list_of_numbers.pop()
second_max = find_second_max(list_of_numbers)
list_of_numbers.remove(second_max)
sum = 0
for i in list_of_numbers:
	value = abs(i - second_max)
	if value > d:
		sum = sum + 0
	else:
		sum = sum + d - value
print(sum)


(a, b, c, k) = list(map(int, input().split()))
e = [a, b, c]
e.sort()
l = 0
if e[1] - e[0] >= k:
	l += 0
else:
	l += k - (e[1] - e[0])
if e[2] - e[1] >= k:
	l += 0
else:
	l += k - (e[2] - e[1])
print(l)