#include <bits/stdc++.h>
using namespace std;
const long double pi = 2 * acos(0.0);
template <class T>
bool umin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool umax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class TT>
bool pal(T a, TT n) {
  int k = 0;
  for (int i = 0; i <= n / 2; i++) {
    if (a[i] != a[n - i - 1]) {
      k = 1;
      break;
    }
  }
  return k ? 0 : 1;
}
long long a[33];
int main() {
  long long d;
  cin >> a[1] >> a[2] >> a[3] >> d;
  sort(a + 1, a + 4);
  long long ans = 0;
  if (a[2] - a[1] < d) ans += d - a[2] + a[1];
  if (a[3] - a[2] < d) ans += d - a[3] + a[2];
  cout << ans;
  getchar();
  getchar();
  return 0;
}
