#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d, e, f, g, h, i, j = 0, k = 0;
  cin >> a >> b >> c >> d;
  e = max(a, b);
  f = max(e, c);
  g = min(a, b);
  h = min(g, c);
  if (a != f && a != h)
    i = a;
  else if (b != f && b != h)
    i = b;
  else
    i = c;
  if (i - h <= d) j = d - (i - h);
  if (f - i <= d) k = d - (f - i);
  cout << j + k << endl;
}
