#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int arr[3], d;
  scanf("%lld %lld %lld %lld", &arr[0], &arr[1], &arr[2], &d);
  sort(arr, arr + 3);
  long long int ans = 0;
  if (arr[1] - arr[0] <= d) ans = arr[0] + d - arr[1];
  if (arr[2] - arr[1] <= d) ans += arr[1] + d - arr[2];
  printf("%lld\n", ans);
}
