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