#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  unsigned long long t = 1;
  while (t--) {
    vector<unsigned long long> v(3);
    for (unsigned long long i = 0; i < 3; i++) cin >> v[i];
    unsigned long long d;
    cin >> d;
    sort((v).begin(), (v).end());
    unsigned long long c = 0;
    if (v[1] - v[0] < d) c += d - (v[1] - v[0]);
    if (v[2] - v[1] < d) c += d - (v[2] - v[1]);
    cout << c;
  }
}
