#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<long long int> v;
  long long int d;
  for (long long int i = 0; i < 3; i++) {
    long long int temp;
    scanf("%lld", &temp);
    v.push_back(temp);
  }
  scanf("%lld", &d);
  sort(v.begin(), v.end());
  long long int diff1 = v[1] - v[0];
  long long int diff2 = v[2] - v[1];
  diff1 = d - diff1;
  diff2 = d - diff2;
  printf("%lld",
         max(diff1, (long long int)(0)) + max(diff2, (long long int)(0)));
}
