#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  vector<int> v = {a, b, c};
  sort(v.begin(), v.end());
  int otv = 0;
  if ((v[1] - v[0]) < d) {
    otv += d - (v[1] - v[0]);
  }
  if ((v[2] - v[1]) < d) {
    otv += d - (v[2] - v[1]);
  }
  cout << otv;
  return 0;
}
