#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 10;
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
bool prime[10000];
void sieve() {
  for (int p = 2; p * p <= 10000; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= 10000; i += p) prime[i] = false;
    }
  }
}
int main() {
  int a[3], d;
  cin >> a[0] >> a[1] >> a[2] >> d;
  sort(a, a + 3);
  int cnt = 0;
  cnt = max(0, d - (a[1] - a[0])) + max(0, d - (a[2] - a[1]));
  cout << cnt << endl;
}
