#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> p(3);
  int d;
  cin >> p[0] >> p[1] >> p[2] >> d;
  sort(p.begin(), p.end());
  if (p[1] - p[0] >= d && p[2] - p[1] >= d)
    cout << 0;
  else if (p[1] - p[0] >= d) {
    cout << (d - (p[2] - p[1]));
  } else if (p[2] - p[1] >= d) {
    cout << (d - (p[1] - p[0]));
  } else {
    cout << (d - (p[1] - p[0]) + d - (p[2] - p[1]));
  }
}
