#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, d, cnt = 0;
  cin >> a >> b >> c >> d;
  vector<long long> v;
  v.push_back(a);
  v.push_back(b);
  v.push_back(c);
  sort(v.rbegin(), v.rend());
  for (int i = 0; i < v.size() - 1; i++) {
    if ((v[i] - v[i + 1]) < d) cnt += (d - (v[i] - v[i + 1]));
  }
  cout << cnt << endl;
}
