#include <bits/stdc++.h>
using namespace std;
int d = 0, a[3], g = 0;
int main() {
  cin >> a[0] >> a[1] >> a[2] >> d;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
      if (a[j] > a[j + 1]) {
        g = a[j];
        a[j] = a[j + 1];
        a[j + 1] = g;
      }
    }
  }
  g = 0;
  if (a[1] - a[0] < d) {
    g += abs(d - (a[1] - a[0]));
  }
  if (a[2] - a[1] < d) {
    g += abs(d - (a[2] - a[1]));
  }
  cout << g;
  return 0;
}
