#include <bits/stdc++.h>
using namespace std;
pair<long long int, long long int> v[200005];
priority_queue<pair<double, long long int>,
               vector<pair<double, long long int> >,
               greater<pair<double, long long int> > >
    q;
long long int visited[100005];
deque<long long int> dq;
void mini(string& s, string t) {
  if (s.size() == t.size()) {
    s = min(s, t);
  } else if (s.size() > t.size()) {
    s = t;
  } else
    s = s;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k, l, ans = 0;
  long long int a[3];
  long long int d;
  cin >> a[0] >> a[1] >> a[2] >> d;
  sort(a, a + 3);
  ans = max((long long int)0, d - (a[1] - a[0])) +
        max((long long int)0, d - (a[2] - a[1]));
  cout << ans;
  return 0;
}
