#include <bits/stdc++.h>
long long mod = 1e9 + 7;
void fail() {
  std::cout << "OVERFLOW!!!\n";
  exit(0);
}
using namespace std;
int32_t main() {
  long long a[3], d, ans = 0;
  cin >> a[0] >> a[1] >> a[2] >> d;
  sort(a, a + 3);
  if (a[2] - a[1] < d) {
    ans = a[1] + d - a[2];
  }
  cerr << "ans"
       << " is " << ans << '\n';
  ;
  if (a[1] - a[0] < d) {
    long long first = a[1] - d;
    ans += (a[0] - first);
  }
  cout << ans << '\n';
  return 0;
}
