#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int a, b, c, d, s, abc[100], s1, s2, r;
  while (scanf("%lld%lld%lld%lld", &abc[0], &abc[1], &abc[2], &d) != EOF) {
    s1 = 0, s2 = 0;
    sort(abc, abc + 3);
    if (abc[1] - abc[0] < d) {
      s1 = d - (abc[1] - abc[0]);
    }
    if (abc[2] - abc[1] < d) {
      s2 = d - (abc[2] - abc[1]);
    }
    r = s1 + s2;
    printf("%lld\n", r);
  }
}
