#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, a1 = 0, b1 = 0, c1 = 0, an = 0;
int main() {
  cin >> a >> b >> c >> d;
  a1 = min(a, min(b, c));
  c1 = max(a, max(b, c));
  if (a1 == a) {
    if (c1 == c) {
      b1 = b;
    }
    if (c1 == b) {
      b1 = c;
    }
  }
  if (a1 == b) {
    if (c1 == c) {
      b1 = a;
    }
    if (c1 == a) {
      b1 = c;
    }
  }
  if (a1 == c) {
    if (c1 == a) {
      b1 = b;
    }
    if (c1 == b) {
      b1 = a;
    }
  }
  if (b1 - a1 <= d) {
    an += d - b1 + a1;
  }
  if (c1 - b1 <= d) {
    an += d + b1 - c1;
  }
  cout << an << endl;
  return 0;
}
