#include <bits/stdc++.h>
using namespace std;
int main() {
  long int a, b, c, d, k = 0, e[3];
  cin >> e[0] >> e[1] >> e[2] >> d;
  sort(e, e + 3);
  a = e[0];
  b = e[1];
  c = e[2];
  if ((b - a) < d)
    k = d - (b - a);
  else
    k = 0;
  if ((c - b) < d) k = k + d - (c - b);
  cout << k << endl;
  return 0;
}
