#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b) { return a < b; }
int main() {
  int a[3], d;
  cin >> a[0] >> a[1] >> a[2] >> d;
  sort(a, a + 3, cmp);
  cout << max(0, d - (a[1] - a[0]) + max(0, d - (a[2] - a[1])));
  return 0;
}
